0% found this document useful (0 votes)
236 views4 pages

Determination of The State-Space Form of A Differential Equation & Solving It Using MATLAB's Ode45-Solver

This document discusses determining the state-space form of a differential equation and solving it using MATLAB's ode45 solver. It provides an example of transforming a single degree-of-freedom oscillator equation into state space form. It then shows how to define the state space form as a MATLAB function and use ode45, along with setting solver options, to solve the differential equation numerically.

Uploaded by

DeepshikhaSingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views4 pages

Determination of The State-Space Form of A Differential Equation & Solving It Using MATLAB's Ode45-Solver

This document discusses determining the state-space form of a differential equation and solving it using MATLAB's ode45 solver. It provides an example of transforming a single degree-of-freedom oscillator equation into state space form. It then shows how to define the state space form as a MATLAB function and use ode45, along with setting solver options, to solve the differential equation numerically.

Uploaded by

DeepshikhaSingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Determination of the State-Space Form of a

Differential Equation & Solving It Using


MATLAB’s ode45-solver
Jousef Murad
www.engineered-mind.com

January 5, 2020

1
1 Determining the State-Space Form
The target of transforming a given differential equation of n-th order into its state form is
to transform it into n differential equations of first order.

Figure 1: A single mass oscillator system

With the given differential equation of a single degree-of-freedom oscillator:

mÿ + dẏ + cy = F (1)


   
x1 y
we can use the state vector x = = to determine the state form.
x2 ẏ
   
ẋ1 ẏ
For this purpose we derive the state vector once, giving us ẋ = = .
ẋ2 ÿ
We can see that

ẋ1 = x2 (2)
Note:

1. Explicit Representation: We sort the differential equation so that the highest deriva-
tive stands alone on one side

2. Implicit Representation: We sort the differential equation so that everything from


the equation is on one side and a zero is on the other side

Furthermore, we can solve equation (1) for ÿ, giving us

1
ÿ = ∗ (F − dẏ − cy) (3)
m

2
to determine the second entry in the derived state vector. Inserting (2) and (3) into the
derived state vector and replacing y and with the corresponding entries in the state vector
results in the state form needed to solve the differential equation in MATLAB using the
ode45-solver:

     
ẋ1 ẏ x2
ẋ = = = ẋ = 1 (4)
ẋ2 ÿ m
∗ (F − dx2 − cx1 )

As you can see, no derivative of a higher order than 1 is left in the equation!

2 Defining a state form function in MATLAB


The ode45-solver expects the state form to be delivered in the form a function that returns
the derived state vector as a function of the equations we determined before. The solver
requires the input parameters to be ordered in a defined way: first you need to insert the
time, then the state vector, then the parameters needed to specify the equation (random
order).

A very common mistake is to mix up the variable order which leads to confusing results (and
often long debugging sessions)!
Listing 1: MATLAB Function of the State-Space Form
1 function [ xp ] = StateSpaceForm ( t ,x ,m ,c ,d , f )
2 % State space form for single mass oscillator
3 xp =[ x (2) ; (1/ m ) *( f - d * x (2) -c * x (1) ) ];
4
5 end

x(1) and x(2) call the entries of the state vector, x1 and x2 . The order of parameters in
the end (Mass, damping ratio, spring stiffness and force) is not important.

The function should be saved in another script file called StateSpaceForm.m - note
that the file should be saved in a separate file and that the file name should ideally
be identical to the function name!

3 Solving the differential equation using the ode45-


solver
Before we can solve the equation, we should first set some tolerance parameters. For that,
we define a variable, for example:

3
1 solverOptions = odeset ( ' RelTol ' , 1e -5 , ' AbsTol ' , 1e -5)

By that, we set the relative and absolute tolerance of the numerical solver to 10− 5 each. This
allows the solver to choose its time steps autonomously. After this, we can finally solve the
differential equation by calling the function

1 [T , Y ] = ode45 (@ StateSpaceForm , tspan , y0 , solverOptions , m , d ,


c, f);

ˆ [T,Y] are the output parameters. The solver writes the time steps itself (or the ones
you explicitly forced him to do by fixing a certain step width in the t vector) into the
vector T and the state vectors for each time step into the matrix Y. The state vectors
are written horizontally each which is very important to note in case you want to
concatenate the matrices later on. You can double click the variables in the workspace
to have a look yourself

ˆ @StateSpaceForm inputs the state form you determined before into the solver

ˆ tspan is either a vector containing only a start and an end time or a vector containing
all the time steps, forcing the solver to use the time steps you chose
 
y0
ˆ y0 declares the initial conditions of the system in state vector form: y~0 =
ẏ0
ˆ solverOptions passes the tolerances we set earlier on to the solver

ˆ The equation parameters are stated afterwards. They must be mentioned in the
same order as defined in the StateSpaceForm function!

Congratulations! You successfully solved the differential equation and you’re on your way
to become a MATLAB master! ;-)

In case you have any questions you can post questions below my MATLAB videos on my
YouTube channel or contact me via social media - I prefer the first option though! :-P

P.S.: You can find all the code needed in my repository as well as a detailed explanation
and walk through of the code on my YouTube Page

You might also like