heun mthod 1st order ode

조회 수: 6 (최근 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 CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by