Trying to go from Euler to Heun's Method
1 view (last 30 days)
Show older comments
We have done Euler's method, and now I need to solve this ODE: y' = 2x +y, y(0)=16 using Heun's method. Is this right?
h = 0.01;
X = 20;
N = round(X/h);
x = zeros(1,N+1);
y = zeros(1,N+1);
x(1) = 0;
y(1) = 16;
for n = 1:N
x(n+1) = x(n) + h;
w = y(n)+ h*(2*x(n)+y(n))
y(n+1) = y(n) + h*0.5*((2*x(n)+y(n))+w);
%yH(n+1) = y(n) +h*0.5*(yH(n)+ 2*x(n)+y(n));
end
0 Comments
Answers (1)
James Tursa
on 1 Dec 2020
No, but you are close. You basically need to use the average of y' at the current state with y' at the Euler estimated next step state. I.e., you take an Euler step as usual and calculate the y' at that point and average it with the y' at the starting point. E.g.,
x at the current state is x(n)
y at the current state is y(n)
y' at the current state is 2*x(n) + y(n)
x at the Euler estimated next state is x(n+1) = x(n) + h
y at the Euler estimated next state is w = y(n) + h*(2*x(n) + y(n))
y' at the Euler estimated next state is 2*x(n+1) + w
Taking the next step with an average of the two y' values:
y(n+1) = y(n) + h*0.5*((2*x(n) + y(n)) + (2*x(n+1) + w))
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!