I need help with a taylor series approximation of e^x
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Thomas MacDowell
el 14 de Jul. de 2018
Respondida: sparsh mehta
el 19 de Mayo de 2021
Please help!
I have a portion of a code I am trying to write that isn't co-operating. This function is written to approximate e^x by a taylor series expansions using a set number of terms. Earlier on in the code I successfully evaluated the function at 3 and 5 terms. My third task is to figure out how many terms it takes to get an evaluation within 0.000001 error.
%%Necessary Terms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
errMax = 0.000001;
ex = 1;
nMax = 1000000;
for nInitial=0:nMax
while abs((exp(x)-ex)/exp(x))*100 >= errMax
ex = ex + x.^nInitial/factorial(nInitial);
end
end
fprintf(['The number of terms necessary for the function to be within the allowed error of 0.0001 is ' num2str(nInitial) '.\n']);
fprintf(['The true value of e^3 is ' num2str(exp(3)) '.\n']);
end
Please help!
0 comentarios
Respuesta aceptada
Walter Roberson
el 14 de Jul. de 2018
for nInitial=0:nMax
if abs((exp(x)-ex)/exp(x))*100 < errMax
break;
end
ex = ex + x.^nInitial/factorial(nInitial);
end
4 comentarios
Walter Roberson
el 14 de Jul. de 2018
Your calculation is off by 1.0. Initialize ex to 0 instead of 1.
Más respuestas (1)
sparsh mehta
el 19 de Mayo de 2021
or nInitial=0:nMax
if abs((exp(x)-ex)/exp(x))*100 < errMax
break;
end
ex = ex + x.^nInitial/factorial(nInitial);
end
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!