Help creating an array to hold approx error from a taylor series
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have a taylor series function for sin(x) and want to hold each value for approx error at each index value. Can anyone explain how I would go about doing this? Here is my while loop:
while index<100 && a_err>1
% while loop specifies that if the power of the taylor series is below 100 and the approximite
% error is greater than 1% the the program will loop through and create the
% next iteration of the taylor series
t = taylor(f,'Order',index, 'ExpansionPoint', xi)
% The taylor function is used to create the taylor series of increasing
% order
t_array(index) = vpa(subs(t,x,xi+h))
% An array is created to hold the values of each iteration of taylor
% series
if index>1
% The if statement keeps the program from stoping early since
% the aproximate error on the first iteration is not valid
a_err = abs((t_array(index)-vpa(subs(f,x,xi)))/vpa(subs(f,x,xi)))*100
% Calculates the approx. error
end
index = index+1
% Increase the index
end
0 comentarios
Respuestas (1)
James Tursa
el 7 de Feb. de 2020
Editada: James Tursa
el 7 de Feb. de 2020
Generally, just index into your variable inside the loop:
a_values(index) = a_err;
If the indexing could be large, you would also consider pre-allocating the a_values variable.
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!