You are on page 1of 1

%%

MATH 134 : Improved Euler Method for 1D autonomous system1s

%%
INPUTS
% ---------------------------------------------%
fx = the function f(x) in x'=f(x)
%
t0 = initial value of t
%
tf = final value
%
x0 = initial value of x
%
N = number of steps
%%
OUTPUTS
% ---------------------------------------------%
time = this is given as a vector with all the values of t
%
xpred= the x approximation at each value of t in the 'time' vector
%%
function [time,xpred] = improvedeuler(fx,t0,Dt,x0,N)
%%

INITIALIZATION
time = zeros(N+1,1);
xpred = zeros(N+1,1);

Initial conditions from input (these are defined in example3.m)


t = t0;
time(1) = t;
x = x0;
xpred(1) = x;

%
%% LOOP FOR IMPROVED EULER METHOD
for i = 1:N
xeul = Dt*feval(fx,x); % Step 1: Euler's method
xhat = Dt*feval(fx,x+xeul); % Step 2: Derivative of predictor point
x = x + (xeul+xhat)/2;
% Step 3: Updated guess using improved Euler
method
% Update time:
t = t + Dt;
% Store values in time and xpred vectors:
time(i+1) = t;
xpred(i+1) = x;
end

You might also like