In: Advanced Math
An improvement to the Forward Euler method is Heun’s method, a
“predictor-corrector” approach that uses the predicted values at
the next time step to create a second improved (“corrected”)
approximation. Given the first order ODE y′ = f (x, y), and a point on the solution curve (xn , yn), we want to estimate the next point at a step size h later. We make a first prediction of the next value, y*, using the forward Euler approach: xn+1 = xn + h y*n+1 = yn + h f (xn , yn) But then this predicted value is used to improve our estimate of the slope f . We compute the approximate slope at the end of our timestep as f (xn+1, y*n+1). We then go back and produce our better estimate of yn+1 using the average of the two slopes. i.e., yn+1 = yn + h *(1/2)*[ f (xn , yn) + f (xn+1, y*n+1)]. This is Heun's method. It is also identified as one version of the popular and powerful Runge Kutta methods. Let y(x) be the solution to the following initial value problem, y′ = (3/10)*[ y + sin(xy)], y (0) = 1 Use Heun's method with h = .001 to estimate the value of y(5). |
clear all
close all
%Function for which solution have to do
f=@(x,y) (3/10)*(y+sin(x*y));
h=0.001; %amount of
intervals
x=0;
% initial x
y=1;
% initial y
x_eval=5; % at what point
we have to evaluate
n=(x_eval-x)/h; %Number of steps
x_heun(1)=x;
y_heun(1)=y;
for i=1:n
%improved Eular steps
m1=double(f(x,y));
m2=double(f((x+h),(y+h*m1)));
y=y+double(h*((m1+m2)/2));
x=x+h;
y_heun(i+1)=y;
x_heun(i+1)=x;
end
plot(x_heun,y_heun)
title('x vs. y plot using Heun method')
xlabel('x')
ylabel('y(x)')
fprintf('Using Heun method with h=0.001 the
estimated value y(%1.0f)=%f\n',x_heun(end),y_heun(end))
%%%%%%%%%%% End of Code %%%%%%%%%%