Taylor series for e^x with loop

조회 수: 16 (최근 30일)
Allison
Allison 2022년 7월 18일
편집: Torsten 2022년 7월 19일
How would I solve this problem and only compute the first 12 interations?
  댓글 수: 6
Allison
Allison 2022년 7월 19일
My bad I should've taken more time to think through the problem does this seem on the right track?
Torsten
Torsten 2022년 7월 19일
편집: Torsten 2022년 7월 19일
Works, I guess.
%x = input('Enter value of x');
x = 2;
value=0;
for i=0:12
value = value + x^i/factorial(i);
end
fprintf('Computed Value: %f', value)
Computed Value: 7.389055
value
value = 7.3891
exp(2)
ans = 7.3891

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

답변 (1개)

Lateef Adewale Kareem
Lateef Adewale Kareem 2022년 7월 19일
%% This is more efficient
x = 2;
v = 1;
n = 1;
d = 1;
for i = 1:20
n = n*x;
d = d*i;
v = v + n/d;
end
fprintf('Computed Value: %f', v)
Computed Value: 7.389056
  댓글 수: 1
Torsten
Torsten 2022년 7월 19일
편집: Torsten 2022년 7월 19일
x = 2;
v = 1;
s = 1;
for i = 1:20
s = s * x/i;
v = v + s;
end
fprintf('Computed Value: %f', v)
Computed Value: 7.389056

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

카테고리

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