For loops and taylor series
31 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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...
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!