Is the following program correct

s=0;
n=input("no of terms");
x=input("put the value of x");
for i=0:n-1
s=(-1).^i*(x.^i)/(factorial(x));
end
disp(s)
;

댓글 수: 1

Stephen23
Stephen23 2021년 9월 26일
@Deepak Singh: your code does not add to s, instead your code just returns the result of the last division.
You need to have something like s = s + ...

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

답변 (2개)

Voss
Voss 2021년 12월 18일

0 개 추천

s=0;
n=input("no of terms");
x=input("put the value of x");
for i=0:2:2*(n-1)
s=s+(-1).^(i/2)*(x.^i)/(factorial(i));
end
disp(s)
Torsten
Torsten 2021년 12월 18일

0 개 추천

To avoid overflow in the x^i and factorial(i) terms, it is advisable to code it this way:
n = input("no of terms");
x = input("put the value of x");
summand = 1.0;
summe = 1.0;
for i = 1:n-1
summand = -summand * x/(2*i-1) * x/(2*i);
summe = summe + summand
end

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2021년 9월 26일

답변:

2021년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by