Heun's method for Euler's solution

12 vues (au cours des 30 derniers jours)
Sachintha Alwis Weerasinghe
Commenté : Jan le 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
  1 commentaire
Jan
Jan le 23 Mar 2017
Fine. What is your question?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by