Help creating an array to hold approx error from a taylor series
    1 次查看(过去 30 天)
  
       显示 更早的评论
    
 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 个评论
回答(1 个)
  James Tursa
      
      
 2020-2-7
        
      编辑:James Tursa
      
      
 2020-2-7
  
      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 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

