sum funtion problem
古いコメントを表示
I'm not sure how to get this function to work. Its the taylor series for a sin function I believe.
sin(x)= E(n=0 underneath, inf on top) (-1)^n*x^(2n+1)/(2n+1)!
the E is the greek symbol. x=[1; 2; -5; 4; 10]
I am lost on what to do about the n variable. I basically want it to output the 6 values for those x's. This is what I have so far. Any ideas? Thanks
x=[1; 2; -5; 4; 10];
n=?
sin=@(x) cumsum(((-1).^n*x.^(2*n+1))/(factorial(2*n+1)));
回答 (2 件)
Jan
2011 年 9 月 23 日
The variable n does not have to run until Inf, because the result of the sum has converged to DOUBLE precision long before.
You need to use .* and ./ operators in addition to perform the elementwise operations. The anonymous function is not needed:
x = 1;
n = 0:100; % Not 0:Inf
cumsum(((-1).^ n .* x .^ (2 .* n + 1)) ./ (factorial(2 .* n + 1)))
Note: I assume, this is a homework question. But you have shown, what you have done so far and I've inserted the dots for the elementswise operation only, after you have done this partially by your own. The @(x) is not needed and anonymous functions can be confusing.
I let the sum run until 100. Is this useful? Would another limit be better?
5 件のコメント
Sean Smith
2011 年 9 月 23 日
Sean Smith
2011 年 9 月 23 日
Jan
2011 年 9 月 23 日
No, 100 does not work "fine". Check the single terms.
You can use dyadic products ([Nx1]*[1xM]=>[NxM]) and BSXFUN to run this for a vector. But this would be much more complicated and slower.
Usually a FOR loop is applied to create the different terms for n.
Walter Roberson
2011 年 9 月 23 日
sine(:,end) or sine(end,:) as appropriate.
Jan
2011 年 9 月 28 日
It seems like Sean Smith has finished his homework and is not interested in this thread anymore.
Kai Gehrs
2011 年 9 月 28 日
Hi,
just an additional comment: you can use the function SYMSUM from the Symbolic Math Toolbox to compute closed form representations of symbolic sums. Of course, this does not work in general, since algorithms for symbolic summation are limited, but it may be useful.
Here is a somehow unrelated example from the doc:
>> syms k
>> symsum(1/k^2,1,Inf)
ans =
pi^2/6
Maybe this helps to address future issues.
Best regards,
-- Kai
カテゴリ
ヘルプ センター および File Exchange で Expansion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!