## Welcome to the World of Modelling and Simulation

### What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ... ### Explicit Euler Method by MATLAB to Solve an ODE

In this example, an implementation of the Explicit Euler approach by MATLAB program to solve an ordinary differential equation (ODE) is presented. Let's consider a differential equation, which is defined as,

dv/dt = p(t) v + q(t)

Where,

p(t) = 5(1+t)

and,

q(t) = (1+t)e^-t

The initial value is, v(0) = 1;, and the time period is 0 < t < 10.

The implementation of Explicit Euler scheme may be represented as,

v_n+1 = v_n (1 - ph) + hq

The following MATLAB program implements this scheme:

close all;
clc;
y_initial = 1; % Defines initial condition, v(0)=1
h = 1.1; % Defines time step
[ t, v ] = expliciteuler( @f_ode, [ 0.0,10 ], y_initial, h );
plot(t,v,'--*')
hold on

function f_vt = f_ode ( t, v )
% This function defines the differential equation
% t is the independent variable
% v is the dependent variable
% f_vt represents the dv/dt
p= 5*(1+t);
q=(1+t)*exp(-t);
f_vt = p*v+q;

function [x, y] = expliciteuler( f_ode, xRange, y_initial, h )
% This function uses Euler’s explicit method to solve the ODE
% dv/dt=f_ode(t,v); x refers to t and y refers to v
% f_ode defines the differential equation of the problem
% xRange = [x1, x2] where the solution is sought on
% y_initial = column vector of initial values for y at x1
% numSteps = number of equally-sized steps to take from x1 to x2
% x = row vector of values of x
% y = matrix whose k-th column is the approximate solution at x(k)
x(1) = xRange(1);
numSteps = ( xRange(2) - xRange(1) ) /h ;
y(:,1) = y_initial;
for k = 1 : numSteps
x(1,k+1) = x(1,k) + h;
y(:,k+1) = y(:,k) + h * f_ode( x(k), y(:,k) );

end

Program Output:

The following plot shows the value of the solution with respect of time increment.