While loop for Taylor Series to approximate e^2.7
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Elle Rae
am 5 Mär. 2021
Beantwortet: David Hill
am 5 Mär. 2021
I need to make a while loop for a Taylor series in order to approximate e^(2.7). This is what I've been trying
clear; clc
sum=0; n = 0; diff=1; x=2.7;
while (diff>= 1e-6)
terms = sum+(x^(n+1)/factorial(n+1));
Exp = sum(terms);
n=n+1;
diff = abs((exp(2.7)-Exp)/exp(2.7));
end
fprintf('My series estimate for e^2.7 is %.8f\n', Exp)
fprintf('It took %d iterations to achieve the desired accuracy\n',n)
0 Kommentare
Akzeptierte Antwort
David Hill
am 5 Mär. 2021
n = 0; diff=1; x=2.7;
while (diff>= 1e-6)
terms(n+1) = (x^(n)/factorial(n));
Exp = sum(terms);
n=n+1;
diff = abs((exp(2.7)-Exp)/exp(2.7));
end
fprintf('My series estimate for e^2.7 is %.8f\n', Exp)
fprintf('It took %d iterations to achieve the desired accuracy\n',n)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!