Advertisement
Guest User

2074HW4_1

a guest
Jul 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.42 KB | None | 0 0
  1. function [Taylor_series,Taylor_error,Taylor_order]=Chapra_4p19(x,a,tol)
  2. %% Taylor series for Chapra 4.19 -- Taylor series for Planet Coordinates
  3. %
  4. %% Input
  5. %  x   = values on interval over which approximation is evaluated (vector)
  6. %  a   = base point, x_i=a, used in the Taylor series expansion (scalar)
  7. %  tol = maximum error tolerance that approximation should not exceed
  8. %        on the interval (scalar)
  9. %
  10. %% Output
  11. %  Taylor_series = Values of f(x) for Taylor series
  12. %                  that meets 0.015 maximum absolute error (vector)
  13. %  Taylor_error  = True absolute error values for each value in vector x
  14. %                  that meet 0.015 maximum absolute error (vector)
  15. %  Taylor_order  = Lowest-order Taylor series (integer)
  16. %                  that meets tol=0.015 maximum absolute error (scalar)
  17.  
  18. %% Write your code here.
  19. f = @(x) x-1-sin(x)/2;
  20. y = f(x); %True value
  21. T = 0*f(x)+ f(a); %Taylor approximation
  22. %^^^Creates vector with as many entries as x, all filled to whatever value
  23. %f(a) is
  24.  
  25. n = 0; %Order of the approximation
  26. error = 1;
  27.  
  28. while error > tol
  29.     n = n + 1;
  30.     if n == 1
  31.         deriv = 1 - cos(a)/2;
  32.     elseif (mod(n,2) == 0)
  33.         deriv = (-1^(0.5*n+1))*sin(a)/2;
  34.     else
  35.         deriv = (-1^(0.5*(n-1)+1))*cos(a)/2;
  36.     end
  37.    
  38.     T = T + (deriv*(x-a).^n)/factorial(n);
  39.     error = max(abs(y - T));
  40. end
  41.  
  42. Taylor_series = T;
  43. Taylor_error = error;
  44. Taylor_order = n;
  45.  
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement