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
Jan 2011 年 9 月 23 日

0 投票

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
Sean Smith 2011 年 9 月 23 日
100 works fine for my purposes. I got it to run by just using the sum function instead of cumsum and when x is just 1 value. Is there a way to do it when x is a vector? x=[1; 2; -5; 4; 10];? everything I try gives me the matrix must agree error. I thought the . operates would fix that but they didn't. What am I missing? I don't understand why it won't run it for each element in the vector x.
Sean Smith
Sean Smith 2011 年 9 月 23 日
I'm suppose to use meshgrid or ndgrid and I got to this point.
n=[0:30];
x=[1; 2; -5; 4; 10];
[N,X]=ndgrid(n,x);
sine=cumsum(((-1).^ N .* X .^ (2 .* N + 1)) ./ (factorial(2 .* N + 1)));
only problem is the variable sine has all 30 values for each x. I just want the last (30th) value because that is the most accurate. Not really sure were to go from there.
Jan
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
Walter Roberson 2011 年 9 月 23 日
sine(:,end) or sine(end,:) as appropriate.
Jan
Jan 2011 年 9 月 28 日
It seems like Sean Smith has finished his homework and is not interested in this thread anymore.

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

Kai Gehrs
Kai Gehrs 2011 年 9 月 28 日

0 投票

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

質問済み:

2011 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by