フィルターのクリア

For loops and taylor series

14 ビュー (過去 30 日間)
SonOfAFather
SonOfAFather 2012 年 9 月 6 日
コメント済み: Dillan Masellas 2016 年 4 月 8 日
I am having issued with my for loop taking the variable that i have set to be [1:1:n] but when i run my script it turns my answer into a scular in stead of a matrix.
here is what i have any help would be great thanks.
clc;
clear;
close all;
n = input('Please give number for the total number of terms in the taylor series: ');
x = input('Please give a value for "x": ');
approxValue = 0;
% Initial value of approxValue.
for k = [0:1:n];
approxVakue = (approxValue + k);
approxValue = sum(x.^k/factorial(k));
% Gives the approx value of e^x as a taylor series
end
disp('approxValue =')
disp((approxValue))
disp('e^x =')
disp(exp(x))
  1 件のコメント
Dillan Masellas
Dillan Masellas 2016 年 4 月 8 日
How can I perform this operation without using the power function?

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 6 日
編集済み: Matt Fig 2012 年 9 月 6 日
Use this loop instead:
for k = 0:n
approxValue = (approxValue + x.^k/factorial(k));
% Gives the approx value of e^x as a taylor series
end
I don't know what approxVakue is supposed to be doing in your code??
And what do you expect to be a matrix? Are you trying to save each term? If so:
approxValue = zeros(1,n+1);
for k = 0:n
approxValue(k+1) = x.^k/factorial(k);
end
approxValuesum = sum(approxValue); % Holds the estimate
This could also be done without FOR loops...
  6 件のコメント
SonOfAFather
SonOfAFather 2012 年 9 月 6 日
thank you i understand now. It wasn't that i needed to know that the value of k it was that it didn't do what i thought it would do. i thought that the value of k would still stay in vector form, but your explaination corrected my thought process. thank you.
SonOfAFather
SonOfAFather 2012 年 9 月 12 日
In the end i was told that"n" should have been "n-1" in
for k = 0:n should have read k = 0:n.
the explaination i was given was that each time to loop processed through it needed to be one less.
thanks for you help. just thought i would tell you how i was corrected.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by