Determination of The State-Space Form of A Differential Equation & Solving It Using MATLAB's Ode45-Solver
Determination of The State-Space Form of A Differential Equation & Solving It Using MATLAB's Ode45-Solver
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.
ẋ1 = x2 (2)
Note:
1. Explicit Representation: We sort the differential equation so that the highest deriva-
tive stands alone on one side
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!
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
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
[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