Project
taylor2.m
function [t,w] = taylor2(f,a,b,w0,N)
%
% Taylor's method of order 2 for y' = f(t,y) with y(a) = w0.
%
% Input:
% f function given in the above differential equation
% a left-end of [a,b]
% b right-end of [a,b]
% w0 initial value
% N number of steps
% Output:
% t vector of arguments
% w vector of approximate values of solution at t
h = (b - a)/N;
t = zeros(1,N+1);
w = zeros(1,N+1);
t(1) = a;
w(1) = w0;
for j=1:N,
tj = t(j);
yj = w(j);
D = feval('d2f',tj,yj);
w(j+1) = yj + h*(D(1)+h*(D(2)/2));
t(j+1) = a + h*j;
end
d2f.m
% Define and store the function d1 = f(t,y)
% and a formula for the first derivative
% of f(t,y) in the M-file d2f.m
function z = d2f(t,y)
d1 = y-t^2;
d2 = y-t^2-2*t;
z = [d1 d2];
taylor4.m
function [t,w] = taylor4(f,a,b,w0,N)
% Taylor's method of order 4 for y' = f(t,y) with y(a) = w0.
%
% Input:
% f function given in the above differential equation
% a left-end of [a,b]
% b right-end of [a,b]
% w0 initial value
% N number of steps
% Output:
% t vector of arguments
% w vector of approximate values of solution at t
h = (b - a)/N;
t = zeros(1,N+1);
w = zeros(1,N+1);
t(1) = a;
w(1) = w0;
for j=1:N,
tj = t(j);
yj = w(j);
D = feval('df',tj,yj);
w(j+1) = yj + h*(D(1)+h*(D(2)/2+h*(D(3)/6+h*D(4)/24)));
t(j+1) = a + h*j;
end
df.m
% Define and store the function d1 = f(t,y)
% and formulas for the next three derivatives
% of f(t,y) in the M-file df.m
function z = df(t,y)
d1 = y-t^2;
d2 = y-t^2-2*t;
d3 = y-t^2-2*t-2;
d4 = y-t^2-2*t-2;
z = [d1 d2 d3 d4];