Spring 2020

Math 151B: Applied Numerical Methods

Discussion Section 1A

Wonjun Lee



Week5

Tuesday

About HW4.

Link to the video

Enter password:

Predictor-Corrector Matlab code
% parameters

h = 0.1;

% endpoints
a = 0; b = 2;

% initial condition
% (u1(t), u2(t)) = (y(t), y'(t))
% (u1(0), u2(0)) = (y(0), y'(0))
u0 = [0;0];

% define f

f = @(t,u) [0,1;4,0] * u + [0;6*exp(-t)];


% predictor-corrector method to approximate u(1) = (y(1), y'(1))

wi = u0; % set the initial value

for i = 1:(b-a)/h
    ti = (i-1)*h;
    wi_bar = predictor(f, wi, ti, h);
    wi     = corrector(f, wi, wi_bar, ti, h);
end

function val = predictor(f, wi, ti, h)
    val = wi + h * f(ti + h/2, wi + h/2 * f(ti,wi)); % midpoint method
end

function val = corrector(f, wi, wi_bar, ti, h)
    val = wi + h/2 * f(ti,wi) + h/2 * f(ti+h, wi_bar);
end