Heun's method for Euler's solution

13 Ansichten (letzte 30 Tage)
Sachintha Alwis Weerasinghe
Sachintha Alwis Weerasinghe am 23 Mär. 2017
Kommentiert: Jan am 23 Mär. 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

Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by