Heun's method for Euler's solution

18 次查看(过去 30 天)
This code solves y'(t)=-2*y(t)+t with y(0)=1 using Euler method
% Initial Condition
y0 = 1;
h = 0.2;
% t goes from 0 to 3 seconds.
t = 0:h:3;
% Preallocate array (good coding practice)
y_euler = zeros(size(t));
% Initial condition gives solution at t=0.
y_euler(1) = y0;
% Solving the equation via Euler's method
for i=1:(length(t)-1)
k1 = -2*y_euler(i)+t(i); % Previous approx for y gives approx for derivative
y_euler(i+1) = y_euler(i) + h*k1; % Approximate solution for next value of y
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by