heun mthod 1st order ode

5 ビュー (過去 30 日間)
dulanga
dulanga 2019 年 4 月 1 日
回答済み: Jan 2019 年 4 月 1 日
Is this code correct and how do i find which integer time t is the solution y(t) closest to -0.2
y0 = -1; % Initial Condition
h = 0.1;
t = 0:h:100;
y_heun = zeros(size(t)); % Preallocate array (good coding practice)
% Initial condition gives solution at t=0.
y_heun(1) = y0;
% Solving the equation via Heun's method
for i=1:(length(t)-1)
k2 = sqrt(t(i)+(y_heun(i)^2))-sqrt(t(i)) % Previous approx for y gives approx for derivative
y_heun(i+1) = y_heun(i) + h*k2;
k3 = sqrt(t(i+1)+(y_heun(i+1)^2))-sqrt(t(i+1)));
y_heun(i+1) = y_heun(i) + (h/2)*(k3+k2); % Approximate solution for next value of y
end

採用された回答

Jan
Jan 2019 年 4 月 1 日
"Using intervals of delta t=1" does not mean
h = 0.1;
t = 0:h:100;
but h=1. Then the 2nd question is easy:
[value, index] = min(abs(y_heun - (-0.2)))
Remember, that the time starts at 0, but the index at 1.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by