Transcript Differential Equations - University of Michigan
Slide 1
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 2
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 3
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 4
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 5
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 6
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 7
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 8
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 9
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 10
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 11
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 12
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 13
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 14
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 15
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 16
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 17
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 18
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 19
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 20
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 21
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 2
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 3
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 4
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 5
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 6
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 7
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 8
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 9
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 10
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 11
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 12
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 13
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 14
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 15
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 16
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 17
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 18
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 19
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 20
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21
Slide 21
Math Review with Matlab:
Differential
Equations
Finding Solutions to
Differential Equations
S. Awad, Ph.D.
M. Corless, M.S.E.E.
D. Cinpinski
E.C.E. Department
University of Michigan-Dearborn
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Finding Solutions to
Differential Equations
Solving a First Order Differential Equation
Solving a Second Order Differential Equation
Solving Simultaneous Differential Equations
Solving Nonlinear Differential Equations
Numerical Solution of a Differential Equation
Using the ODE45 Solver
2
Differential Equations: Finding Solutions to DEs
dx ( t )
Math Review with Matlab
dt
Solving a
U of M-Dearborn ECE Department
st
1
Order DE
The Matlab command used to solve differential
equations is dsolve
Consider the differential equation:
dy
2 y 12
dt
y ( t ) 6 c1e
The general solution is given by:
Verify the solution using dsolve command
2 t
3
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Solving a Differential
Equation in Matlab
» syms y t
» ys=dsolve('Dy+2*y=12')
ys =
6+exp(-2*t)*C1
y ( t ) 6 c1e
2 t
C1 is a constant which is specified by way of the
initial condition
Dy means dy/dt and D2y means d2y/dt2 etc
4
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Verify Results
Verify results given y(0) = 9
y ( t ) 6 c1e
2 t
y (0) 9 C1 3
» ys=dsolve('Dy+2*y=12','y(0)=9')
ys =
6+3*exp(-2*t)
y (t ) 6 3e
2 t
5
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving a 2nd Order DE
Find the general solution of:
2
d y
dt
2
c y0
2
» syms c y
» ys=dsolve('D2y=-c^2*y')
ys =
C1*sin(c*t)+C2*cos(c*t)
y ( t ) C 1 sin( ct ) C 2 cos( ct )
6
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Solving Simultaneous
Differential Equations Example
Syntax for solving simultaneous differential equations is:
dsolve('equ1', 'equ2',…)
Solve the following set of differential equations:
dx
dt
3x 4 y
dy
4 x 3 y
dt
7
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
General Solution
Given the equations:
dx
3x 4 y
4 x 3 y
dt
dt
dy
The general solution is given by:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3t
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
8
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Matlab Verification
Given the
equations:
dx
General
solution is:
x ( t ) c1e cos( 4 t ) c 2 e sin( 4 t )
3x 4 y
dt
3t
dy
4 x 3 y
dt
3t
y ( t ) c1e sin( 4 t ) c 2 e cos( 4 t )
3t
3t
» syms x y t
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y')
x =
exp(3*t)*(cos(4*t)*C1+sin(4*t)*C2)
y =
-exp(3*t)*(sin(4*t)*C1-cos(4*t)*C2)
9
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Initial Conditions
Solve the previous system with the initial conditions:
x (0) 0
y (0) 1
» [x,y]=dsolve('Dx=3*x+4*y','Dy=-4*x+3*y',
'y(0)=1','x(0)=0')
x =
3t
exp(3*t)*sin(4*t)
x e sin( 4 t )
y =
3t
y e cos( 4 t )
exp(3*t)*cos(4*t)
10
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Non-Linear Differential Equation Example
Solve the differential equation:
dy
4 y
2
dt
Subject to initial condition:
y (0) 1
» syms y t
» y=dsolve('Dy=4-y^2','y(0)=1')
» y=simplify(y)
4t
y =
2 3 e 1
2*(3*exp(4*t)-1)/(1+3*exp(4*t)) y ( t )
4t
1 3e
11
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Specifying the Independent
Parameter of a Differential Equation
If another independent variable, other than t, is used, it must be
introduced in the dsolve command
Solve the differential equation:
dy
2 y 12
dx
» y=dsolve('Dy+2*y=12','x')
y =
2 x
y ( x ) 6 C 1e
6+exp(-2*x)*C1
12
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Numerical Solution Example
Not all non-linear differential equations have a closed form
solution, but a numerical solution can be found
2
Solve the differential equation:
d y
dt
Subject to initial conditions:
2
9 sin( y ) 0
y (0) 1
y (0) 0
No closed form solution exists
Use the ode45 command to get a numerical solution
13
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Rewrite Differential Equation
2
d y
dt
2
2
9 sin( y ) 0
dt
Rewrite in the
following form
d y
x1 y
x 2 y 9 sin( y )
x 2 9 sin( x1 )
2
y 9 sin( y )
x 2 y x1
x1 ( 0 ) y ( 0 ) 1
x2 (0) y (0) 0
14
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Create a New Function
Create a Matlab function evalxdot to evaluate
x1
and
x2
numerically in terms of x1 and x2.
function xdot=evalxdot(t,x)
%x=[x1, x2]
%xdot=[dx1/dt, dx2/dt];
xdot=[x(2); -9*sin(x(1))];
15
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
ODE45
ODE45 is used to solve non-stiff differential equations
[T,Y] = ODE45('F',TSPAN,Y0)
T
= Time vector
Y
= Output corresponding to time vector
F
= Function name
TSPAN
= Simulation duration
Y0
= Initial conditions
If the left hand side [T,Y]of the output arguments is
omitted, Matlab solves it and plots it
16
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Returning t, y and dy/dt
Run the solver with the input and output
arguments specified
»
»
»
»
»
»
[t,y]=ode45('evalxdot',10,[1 0]);
plot(t,y)
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
17
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Plot of Solution
Y
Y
18
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Omit Output Arguments
We can run the solver again without output
arguments
Omitting the output arguments causes Matlab to
plot the results
»
»
»
»
»
ode45('evalxdot',10,[1 0]);
xlabel('Time (sec)');
ylabel('Amplitude');
title('Numerical Solution');
legend('Y','dY/dt')
19
dx ( t )
dt
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
U of M-Dearborn ECE Department
Plot of Results
Y
Y
20
dx ( t )
Differential Equations: Finding Solutions to DEs
Math Review with Matlab
dt
U of M-Dearborn ECE Department
Summary
The symbolic toolbox can be used to find the
closed form solutions for differential
equations where they exist
The symbolic toolbox can be simultaneously
solve a system of differential equations
Other Matlab commands can be used to
numerically solve systems of differential
equations if closed forms do not exist
21