% PEC implementation of Predictor-Corrector method

clear all;

h = 0.025;
N = 40;

f = @(t,x) -2*t*x^2;

exact = @(t) 1/(1+t^2);

t(1) = 0;
x(1) = 1;
y(1) = exact(t(1));
f1 = f(t(1),x(1));

t(2) = h;
x(2) = exact(t(2));
y(2) = exact(t(2));
f2 = f(t(2),x(2));

for i=2:N
    
    % Predictor
    x(i+1) = x(i) + h*( (3/2)*f2 - (1/2)*f1 );
    t(i+1) = t(i) + h;
    
    % Evaluation
    f3 = f(t(i+1),x(i+1));
    
    % Corrector
    x(i+1) = x(i) + h*( (5/12)*f3 + (8/12)*f2 - (1/12)*f1 );
    
    y(i+1) = exact(t(i+1));
    f1 = f2;
    f2 = f3;
    
end