sum funtion problem

2 visualizzazioni (ultimi 30 giorni)
Sean Smith
Sean Smith il 23 Set 2011
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)));

Risposte (2)

Jan
Jan il 23 Set 2011
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 Commenti
Walter Roberson
Walter Roberson il 23 Set 2011
sine(:,end) or sine(end,:) as appropriate.
Jan
Jan il 28 Set 2011
It seems like Sean Smith has finished his homework and is not interested in this thread anymore.

Accedi per commentare.


Kai Gehrs
Kai Gehrs il 28 Set 2011
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

Community Treasure Hunt

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

Start Hunting!

Translated by