home
PDF (letter size)
PDF (legal size)

Using Matlab ode45 to solve differential equations

Nasser M. Abbasi

May 30, 2012   Compiled on May 20, 2020 at 9:24pm

Contents

1 download examples source code
2 description
3 Simulation
4 Using ode45 with piecewise function
5 Listing of source code

1 download examples source code

  1. first_order_ode.m.txt
  2. second_order_ode.m.txt
  3. engr80_august_14_2006_2.m.txt
  4. engr80_august_14_2006.m.txt
  5. ode45_with_piecwise.m.txt

2 description

This shows how to use Matlab to solve standard engineering problems which involves solving a standard second order ODE. (constant coefficients with initial conditions and nonhomogeneous).

A numerical ODE solver is used as the main tool to solve the ODE’s. The matlab function ode45 will be used. The important thing to remember is that ode45 can only solve a first order ODE. Therefore to solve a higher order ODE, the ODE has to be first converted to a set of first order ODE’s. This is possible since an \(n\) order ODE can be converted to a set of \(n\) first order ODE’s.

Gives a first order ODE \[ \frac{dx}{dt}=f(x,t) \]

An example of the above is \(\frac{dx}{dt}=3 e^{-t}\) with an initial condition \(x(0)=0\). Here is the result of solving this ODE in Matlab. Source code is first_order_ode.m.txt

pict

To solve a second order ODE, using this as an example. \[ \frac{d^{2} x}{dt^{2}}+5 \frac{dx}{dt}- 4 x(t) = \sin (10\ t) \]

Since ode45 can only solve a first order ode, the above has to be converted to two first order ODE’s as follows. Introduce 2 new state variables \(x_{1},x_{2}\) and carry the following derivation

\[ \left . \begin{array} [c]{c}x_{1}=x\\ x_{2}=x^{\prime }\end{array} \right \} \overset{\text{take derivative}}{\rightarrow }\left . \begin{array} [c]{c}x_{1}^{\prime }=x^{\prime }\\ x_{2}^{\prime }=x^{\prime \prime }\end{array} \right \} \overset{\text{do replacement}}{\rightarrow }\left . \begin{array} [c]{l}x_{1}^{\prime }=x_{2}\\ x_{2}^{\prime }=-5x^{\prime }+4x+\sin \left ( 10t\right ) \end{array} \right \} \rightarrow \left . \begin{array} [c]{l}x_{1}^{\prime }=x_{2}\\ x_{2}^{\prime }=-5x_{2}+4x_{1}+\sin \left ( 10t\right ) \end{array} \right \} \]

The above gives 2 new first order ODE’s. These are

\[\begin{array} [c]{l}x_{1}^{\prime }=x_{2}\\ x_{2}^{\prime }=-5x_{2}+4x_{1}+\sin \left ( 10t\right ) \end{array} \]

Now ode45 can be used to solve the above in the same way as was done with the first example. The only difference is that now a vector is used instead of a scalar.

This is the result of solving this in Matlab. The source code is second_order_ode.m.txt

pict

3 Simulation

Now ode45 is used to perform simulation by showing the solution as it changes in time.

Given a single degree of freedom system. This represents any engineering system whose response can move in only one direction. A typical SDOF (single degree of freedom) is the following mass/spring/damper system.

pict

The first step is to obtain the equation of motion, which will be the second order ODE. Drawing the free body diagram and from Newton’s second laws the equation of motion is found to be

\[ m x'' + c x' + k x = f( \omega _f t) \]

In the above, \(\omega _f\) is the forcing frequency of the force on the system in rad/sec.

The response of the system (the solution of the system, or \(x(t)\)) is simulated for different parameters.

For example, the damping \(c\) can be changed, or the spring constant (the spring stiffness) to see how \(x(t)\) changes. The forcing function frequency \(\omega _f\) can also be changed.

The following definitions are used in the Matlab code.

Natural frequency of the system \(\omega =\sqrt{\frac{k}{m}-\left (\frac{c}{2m}\right )^2}\)

Damping ratio \(\varsigma =\frac{c}{c_r}\) where \(c\) is the damping coefficient and \(c_r\) is the critical damping. \[ c_{r} = 2 \sqrt{k m} \]

When \(c>c_{r}\) the system is called over damped. When \(c<c_r\) the system is called underdamped

The following example runs a simulation showing the effect of changing the damping when the forcing function is a step function. The response to a step function is a standard method used to analyze systems.

pict

4 Using ode45 with piecewise function

ode45 can be used with piecewise function defined for the RHS. For example, given \(x''(t)-x(t)=c\) where \(c=1\) for \(0<=t<1\) and \(c=20\) for \(1<=t<2\) and \(c=3\) for \(2<=t<=3\), the following code example shows one way to implement the above.

ode45_with_piecwise.m.txt

5 Listing of source code

first_order_ode.m

second_order_ode.m

engr80_august_14_2006_2.m

engr80_august_14_2006.m

ode45_with_piecwise.m