heun mthod 1st order ode
Show older comments
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
More Answers (0)
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!