MATLAB: How to calculate an infinite series using a function file with for-end loops

for end loopfunctionseries

Given f(x)= [(x^2)/37]+[(x^3)/3!]+[(x^4)/4!]+[(x^5)/5!]+…
Using a function file (function fn=my_fun(x,n)) with for-end loops for several test files.
I'm just having issues defining the function file using the for-end loop. What I have so far is:
function [xval,nterms]=my_series(x,n)

Best Answer

hi gordon,
you function returns a scalar or vector values ? is this exp(x) Taylor series ?
you can try this light version :
function [xval]=your_series(x,n)
% X is vector input
% n number of iteration
xval=zeros(size(x));
order=0:n;
for ii=1:length(x)
for t=1:length(order)
xval(ii)=xval(ii)+(x(ii)^t)/factorial(t);
end
end
This function approximates well the EXP FUNCTION, replace the term ( x(ii)^t/t! ) with your general Formula .