Taylor series for e^x with loop

19 Ansichten (letzte 30 Tage)
Allison
Allison am 18 Jul. 2022
Bearbeitet: Torsten am 19 Jul. 2022
How would I solve this problem and only compute the first 12 interations?
  6 Kommentare
Allison
Allison am 19 Jul. 2022
My bad I should've taken more time to think through the problem does this seem on the right track?
Torsten
Torsten am 19 Jul. 2022
Bearbeitet: Torsten am 19 Jul. 2022
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Lateef Adewale Kareem
Lateef Adewale Kareem am 19 Jul. 2022
%% 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 Kommentar
Torsten
Torsten am 19 Jul. 2022
Bearbeitet: Torsten am 19 Jul. 2022
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by