필터 지우기
필터 지우기

Trying to write a script that solves a series

조회 수: 1 (최근 30일)
steve guy
steve guy 2013년 2월 21일
The series goes 1-1/3!+1/5!-1/7!+.....20 terms
This is my first guess on how to solve it. Don't have software available to me at the moment. This is a start but I have a feeling something isn't right here.
a0=1/factorial(0); %first term
an=1/factorial(38); %final term
n=0; Initialize
sumf=0; %initialize sum of series
sign=1; %initialize sign
for tn=a0: -1 / factorial(n+2):an ; %step size negative due to decreasing values.
sumf = (sumf + tn) . * sign;
sign= (sign .* -1); %update sign
n=n+2; %The n is update within the factorial increment
end
any problems with this? can you update an increment?
[EDITED, Jan, code formatted]

답변 (1개)

José-Luis
José-Luis 2013년 2월 21일
편집: José-Luis 2013년 2월 21일
A few problems, mostly regarding numerics
  1. A factorial can only be represented accurately in double precision up to 21. Same applies for the inverse
  2. Your different terms will span many orders of magnitude. Adding terms like that kills accuracy.
If you want accurate results and have the symbolic math toolbox you could look into vpa().
  댓글 수: 1
steve guy
steve guy 2013년 2월 21일
Hi Jose. I'm not concerned with the accuracy just that the code itself. If I assign an increment value of -1/factorial(2) is that really just a redundant way of saying -1/2?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by