Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [Taylor_series,Taylor_error,Taylor_order]=Chapra_4p19(x,a,tol)
- %% Taylor series for Chapra 4.19 -- Taylor series for Planet Coordinates
- %
- %% Input
- % x = values on interval over which approximation is evaluated (vector)
- % a = base point, x_i=a, used in the Taylor series expansion (scalar)
- % tol = maximum error tolerance that approximation should not exceed
- % on the interval (scalar)
- %
- %% Output
- % Taylor_series = Values of f(x) for Taylor series
- % that meets 0.015 maximum absolute error (vector)
- % Taylor_error = True absolute error values for each value in vector x
- % that meet 0.015 maximum absolute error (vector)
- % Taylor_order = Lowest-order Taylor series (integer)
- % that meets tol=0.015 maximum absolute error (scalar)
- %% Write your code here.
- f = @(x) x-1-sin(x)/2;
- y = f(x); %True value
- T = 0*f(x)+ f(a); %Taylor approximation
- %^^^Creates vector with as many entries as x, all filled to whatever value
- %f(a) is
- n = 0; %Order of the approximation
- error = 1;
- while error > tol
- n = n + 1;
- if n == 1
- deriv = 1 - cos(a)/2;
- elseif (mod(n,2) == 0)
- deriv = (-1^(0.5*n+1))*sin(a)/2;
- else
- deriv = (-1^(0.5*(n-1)+1))*cos(a)/2;
- end
- T = T + (deriv*(x-a).^n)/factorial(n);
- error = max(abs(y - T));
- end
- Taylor_series = T;
- Taylor_error = error;
- Taylor_order = n;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement