How do I calculate an infinite series using a function file with for-end loops?

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)

댓글 수: 3

hi is the code below fine? i mean your input is : vector X and order N , so as for every point in X we sum N terms defined with general formula .
Could you confirm that the first term is divided by 37, and not factorial(2) as would match the 3! 4! 5! pattern?
that is matter of Initial conditions

댓글을 달려면 로그인하십시오.

 채택된 답변

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 .

댓글 수: 3

Function returns scalar quantity.
This is very helpful in the general procedure, but in defining the xval(ii)=... term, I'm not sure how to replace it with my formula, which has the first term divided by 37, not factorial(2). How do I generalize this formula and input it in the for-end loop for calculation?
ok, and in the input is vector ?
if it is a scalar then :
% given scalar x
S=0;
S=(x^2)/37;
for ii=3:N
S=S+((x^ii)/factorial(ii));
end

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by