필터 지우기
필터 지우기

sum funtion problem

조회 수: 3 (최근 30일)
Sean Smith
Sean Smith 2011년 9월 23일
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일
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
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일
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

카테고리

Help CenterFile Exchange에서 Expansion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by