Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Seth on Simulink

May 23rd, 2008

How to draw ODEs in Simulink

I remember while learning Simulink, drawing ordinary differential equations was one of the early challenges. Eventually I discovered a few steps that make it easier. First, rewrite the equations as a system of first order derivatives. Second, add integrators to your model, and label their inputs and outputs. Third, connect the terms of the equations to form the system.

Example: Mass-Spring-Damper

The mass-spring-damper system provides a nice example to illustrate these three steps. Let’s look at the equation for this system:

Spring Mass Damper Equation

The position of the mass is , the velocity is , and the acceleration is .

Express the system as first order derivatives

To rewrite this as a system of first order derivatives, I want to substitute for , and for . Then I can identify my two states as position and velocity . The equation becomes

And this is rewritten at two first derivatives:


Velocity and position are the states of my system. When thinking about ODEs, states equal integrator blocks.

Add one integrator per state, label the input and output

I always make a point to write the equations as an annotation on my diagram. I refer to this as I add blocks to the canvas. Here are the two integrators for the mass-spring-damper system.

Integrator blocks

I draw signals from the ports and label inputs as the derivative (), and the output is the state variable.

Connect the terms to form the system

The first connection is easy, , so I connect the output of the velocity integrator to the input of the position integrator. When this happens, aligning the integrators in the diagram shows that you have a second order system.

Integrator blocks, connected

To implement the second equation, I add gains and sums to the diagram and link up the terms.

Final spring mass damper system

The final step, initial conditions

Modeling differential equations require initial conditions for the states in order to simulate. The initial states are set in the integrator blocks. Think of these as the initial value for v and x at time 0. The ODE solvers compute the derivatives at time zero using these initial conditions and then propagate the system forward in time. I used an annotation to record these initial conditions, v0 = 0, and x0 = 10.

Simulating the model for 50 seconds produces the following trace for x (blue) and v (red).

Spring mass damper scope

Over time, I have become more comfortable in converting from equations to models, and I do not always rewrite the states. I think that fundamentally I still follow the same process:

  1. Re-express the system in terms of state derivatives
  2. Add integrators and label the inputs and outputs
  3. Connect up the equations
  4. Set initial conditions

Now it’s your turn

Is this the same process you learned when you started using Simulink? Do you have any special tips on how to draw ODEs? Share your ideas and leave a comment here.

6 Responses to “How to draw ODEs in Simulink”

  1. Daniel replied on :

    Hey Seth I’ve been trying to learn some Simulink and was glad MathWorks now has a Simulink blog!

    In relation to your post, what you recommend if my ‘kx’ is substituted with a ‘f(x)’ where we have non-linear stiffness. Currently I have it to substitute the gain block with a look up table. Are there other alternatives or suggestions? Should I be using the time varying transfer fcn blocks instead?

  2. wei replied on :

    Hi Daniel,
    There are several choices from “user-defined functions” group of the Simulink library.

  3. Wenting replied on :

    Hi Seth, I am trying to find a saddle path backwards from the equilibrium as it is very difficult for me to know the exact initial values on the saddle path for my four dimensional ODE system. I tried to use the code tspan=[T 0] instead of [0 T] (T could be any number) when I draw the graph.And I did not change the ODE system. The result turned out to be strange as it seemed that the system always stayed at the equilibrium point while it supposed to be a saddle point. My question is that do I need to add “-”(minus) in front of my ODE system as well? Or is there other advice? Many thanks!

  4. Muralidhar M replied on :

    Hi Seth,

    The post on ODE was excellent…can we have a similar post on “Difference equations” and also on “partial differential equation”.

    Thanks!
    Murali

  5. Mapa Ustka replied on :

    Nice Blog!Very good Blog!

  6. RAMYA replied on :

    sir,
    am pursuing my masters in communication engineering. i am finding some difficult in implementing a block given in my paper, using simulink. Dynamic simulation of MEMS switches using Simulink® I VE ATTACHED THE PAPER FOR UR CONVENIENCE. PLEASE HELP ME IN IMPLEMENTING THE EQUATIONS AND BLOCKS IN SIMULINK. EXPECTING A POSITIVE REPLY FROM YOU AT THE EARLIEST SIR..
    D. J. Combes, K. M. Brunson and R. J. T. Bunyan
    QinetiQ Malvem, St Andrews Road, Malvem, Worcestershire, WR14 3PS
    ABSTRACT:
    A nodal model is implemented in Simulink and used to investigate the dynamics of electrostatically actuated MEMS switches. The model is based on reduced order 4DOF beam elements, with electrostatic forces based on simple parallel plate approximations at each node. Hard stops and contact stiction conditions are implemented. Using this model, the role of dynamic forces in overcoming contact stiction in MEMS switches is investigated. For an example MEMS switch, the relationship between the design of the switch, the strain energy under actuation and the dynamic restoring force are discussed. Dynamic forces are found to play a highly significant role in overcoming contact stiction.
    Keywords: MEMS switch, Simulink, Dynamic model, Stiction force
    GOD… AM UNABLE TO COPY THE BLOCK SETH…

Leave a Reply


Seth Popinchalk is an Application Engineer for The MathWorks. He writes here about Simulink and other MathWorks tools used in Model-Based Design.
  • ljp: very good
  • Tyler: Seth, I find this discussion helpful, and I would like to ask for further elaboration on Doug’s question...
  • David Vargas: Hi, I need help with Simulink. I’m trying to make a model of an object falling from the sky. It...
  • Amin: Seth: Thanks for your attention.
  • Jun: Hi Seth, I used ’sim’ command to run simulink simulations quite often before. But this time, it kept...
  • KMR: The initial condition block may not be the best thing to use if you need to generate code from your diagram and...
  • wei: Hi Seth, For some build-in blocks (for example, the constant block), there are multiple tabs (”Main”...
  • John Reilly: Seth: Thanks for your reply. I have constant block values that depend upon parameter values. In order to...
  • Amin: Hi Seth! Is it necessary to rest the position integral in the bouncing ball demo? I get same result without...
  • parth damani: This was very helpful. I want one help about if you have any idea. I want to generate .mdl file from .m...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics