


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



0 2
y dy 2
a t c t y a t c t y t a dt y dy 3 exp
ln( ) 3 3 2
a t t y y 3 exp 3 0
function [t,y] = odeEuler(diffeq,tn,h,y0) % odeEuler Euler's method for integration of a single, first order ODE % % Syntax: [t,y] = odeEuler(diffeq,tn,h,y0) % % Input: diffeq = anonymous function that evaluates the right % hand side of the ODE as a function of (t,y) % tn = stopping value of the independent variable % h = stepsize for advancing the independent variable % y0 = initial condition for the dependent variable % % Output: t = vector of independent variable values: t(i) = (i-1)h % y = vector of numerical solution values at the t(i) % initialize t = (0:h:tn)'; % Column vector of elements with spacing h n = length(t); % Number of elements in the t vector y = zeros(n,1); % Preallocate y for speed y(1) = y0; % compute solution using Euler scheme for i = 1:n- y(i+1) = y(i) + hdiffeq(t(i),y(i)); end
function [t,y] = odeHeun(diffeq,tn,h,y0) % odeHeun Heun method for integration of a single, first order ODE % % Syntax: [t,y] = odeHeun(diffeq,tn,h,y0) % % Input: diffeq = anonymous function that evaluates the right % hand side of the ODE as a function of (t,y) % tn = stopping value of the independent variable % h = stepsize for advancing the independent variable % y0 = initial condition for the dependent variable % % Output: t = vector of independent variable values: t(i) = (i-1)h % y = vector of numerical solution values at the t(i) t = (0 : h : tn)'; % Column vector of elements with spacing h n = length(t); % Number of elements in the t vector y = y0zeros(n,1); % Preallocate y for speed y(1) = y0; % Begin Midpoint scheme; j=1 for initial condition for i = 1:n- k1 = feval( diffeq, t(i), y(i) ); k2 = feval( diffeq, t(i)+h, y(i)+hk1 ); y(i+1) = y(i) + h(k1+k2)/2; end
t y(t) exact Euler,h=0. Euler,h=0. Heun,h=0.