heun mthod 1st order ode

2 次查看(过去 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 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by