Trying to go from Euler to Heun's Method

1 回表示 (過去 30 日間)
Justin Faienza
Justin Faienza 2020 年 12 月 1 日
回答済み: James Tursa 2020 年 12 月 1 日
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

回答 (1 件)

James Tursa
James Tursa 2020 年 12 月 1 日
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))

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by