Advertisement
Guest User

2074HW4_1

a guest
Jul 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.45 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.  
  22. T = 0*f(x)+ f(a); %Taylor approximation
  23. %Creates vector with as many entries as x, all filled to whatever value
  24. %f(a) is
  25.  
  26. n = 0; %Order of the approximation
  27. error = inf;
  28.  
  29. while error > tol
  30.     n = n + 1;
  31.     if n == 1
  32.         deriv = 1 - cos(a)/2;
  33.     elseif (mod(n,2) == 0) %If n is even
  34.         deriv = (-1^(0.5*n+1))*sin(a)/2;
  35.     else %If n is odd
  36.         deriv = (-1^(0.5*(n-1)+1))*cos(a)/2;
  37.     end
  38.    
  39.     T = T + (deriv*(x-a).^n)/factorial(n);
  40.     error = max(abs(y - T));
  41. end
  42.  
  43. Taylor_series = T;
  44. Taylor_error = error;
  45. Taylor_order = n;
  46.  
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement