Heun's method for Euler's solution

12 views (last 30 days)
Sachintha Alwis Weerasinghe
Commented: Jan on 23 Mar 2017
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

Answers (0)

Categories

Find more on Programming 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!