heun mthod 1st order ode

1 view (last 30 days)
dulanga
dulanga on 1 Apr 2019
Answered: Jan on 1 Apr 2019
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

Accepted Answer

Jan
Jan on 1 Apr 2019
"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.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!