{"id":15,"date":"2008-05-23T19:46:13","date_gmt":"2008-05-24T00:46:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2008\/05\/23\/how-to-draw-odes-in-simulink\/"},"modified":"2008-05-23T19:46:13","modified_gmt":"2008-05-24T00:46:13","slug":"how-to-draw-odes-in-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2008\/05\/23\/how-to-draw-odes-in-simulink\/","title":{"rendered":"How to draw ODEs in Simulink"},"content":{"rendered":"<p>I remember while learning Simulink, drawing ordinary\r\ndifferential equations was one of the early challenges.  Eventually I\r\ndiscovered a few steps that make it easier.  First, rewrite the equations as a\r\nsystem of first order derivatives.  Second, add integrators to your model, and\r\nlabel their inputs and outputs.  Third, connect the terms of the equations to\r\nform the system.<\/p>\r\n\r\n<p><strong>Example: Mass-Spring-Damper<\/strong><\/p>\r\n\r\n<p>The mass-spring-damper system provides a nice example to\r\nillustrate these three steps.  Let\u2019s look at the equation for this system:<\/p>\r\n\r\n<p><img decoding=\"async\" width=116 height=20 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/smdEquation.png\" alt=\"Spring Mass Damper Equation\"><\/p>\r\n\r\n<p>The position of the mass is <span style='font-size:11.0pt;line-height:100%;font-family:\"Calibri\",\"sans-serif\";position:relative;\r\ntop:4.5pt'><img decoding=\"async\" width=8 height=20 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/x.png\"><\/span>,\r\nthe velocity is <span\r\nstyle='font-size:11.0pt;line-height:100%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:8.5pt'><img width=41 height=29\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xdot.png\"><\/span>, and the acceleration is <span\r\nstyle='font-size:11.0pt;line-height:100%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:8.5pt'><img width=47 height=30\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xddot.png\"><\/span>. <\/p>\r\n\r\n<p><strong>Express the system as first order derivatives<\/strong><\/p>\r\n\r\n<p>To rewrite this as a system of first order derivatives, I\r\nwant to substitute <span style='font-size:11.0pt;line-height:115%;font-family:\r\n\"Calibri\",\"sans-serif\";position:relative;top:4.5pt'><img width=8 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/v.png\"><\/span> for <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=8 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xdot2.png\"><\/span>, and <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=8 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/vdot2.png\"><\/span> for <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=8 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xddot2.png\"><\/span>.  Then I can identify my two\r\nstates as position <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=20 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xparen.png\"><\/span> and velocity <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=21 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/vparen.png\"><\/span>.  The equation becomes<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=116 height=20 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/smdeqn2.png\"><\/span><\/p>\r\n\r\n<p>And this is rewritten at two first derivatives:<\/p>\r\n\r\n<p><span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=36 height=20 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xdoteqn.png\"><\/span><br>\r\n<span style='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\"'><img\r\nwidth=118 height=36 src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/vdoteqn.png\"><\/span><\/p>\r\n\r\n<p>Velocity and position are the states of my system.  When\r\nthinking about ODEs, states equal integrator blocks.<\/p>\r\n\r\n<p><strong>Add one integrator per state, label the input and output<\/strong><\/p>\r\n\r\n<p>I always make a point to write the equations as an\r\nannotation on my diagram.  I refer to this as I add blocks to the canvas.  Here\r\nare the two integrators for the mass-spring-damper system.<\/p>\r\n\r\n<p><img width=261 height=283 id=\"Picture 5\"\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/smd_integrators.png\" alt=\"Integrator blocks\"><\/p>\r\n\r\n<p>I draw signals from the ports and label inputs as the\r\nderivative (<span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=44 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xdoteqxd.png\"><\/span>), and the output is the state\r\nvariable.<\/p>\r\n\r\n<p><strong>Connect the terms to form the system<\/strong><\/p>\r\n\r\n<p>The first connection is easy, <span\r\nstyle='font-size:11.0pt;line-height:115%;font-family:\"Calibri\",\"sans-serif\";\r\nposition:relative;top:4.5pt'><img width=36 height=20\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/xdoteqn.png\"><\/span>, so I connect the output of the\r\nvelocity integrator to the input of the position integrator.  When this\r\nhappens, aligning the integrators in the diagram shows that you have a second\r\norder system.<\/p>\r\n\r\n<p><img width=375 height=209 id=\"Picture 6\"\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/smd_integrators2.png\" alt=\"Integrator blocks, connected\"><\/p>\r\n\r\n<p>To implement the second equation, I add gains and sums to\r\nthe diagram and link up the terms.<\/p>\r\n\r\n<p><img width=406 height=301 id=\"Picture 7\"\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/smd_final.png\" alt=\"Final spring mass damper system\"><\/p>\r\n\r\n<p><strong>The final step, initial conditions<\/strong><\/p>\r\n\r\n<p>Modeling differential equations require initial conditions\r\nfor the states in order to simulate.  The initial states are set in the\r\nintegrator blocks.  Think of these as the initial value for v and x at time 0. \r\nThe ODE solvers compute the derivatives at time zero using these initial\r\nconditions and then propagate the system forward in time.  I used an annotation\r\nto record these initial conditions, v0 = 0, and x0 = 10.<\/p>\r\n\r\n<p>Simulating the model for 50 seconds produces the following\r\ntrace for x (blue) and v (red).<\/p>\r\n\r\n<p><img width=432 height=332 id=\"Picture 8\"\r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2008Q2\/smd_scope.png\" alt=\"Spring mass damper scope\"><\/p>\r\n\r\n<p>Over time, I have become more comfortable in converting from\r\nequations to models, and I do not always rewrite the states.  I think that\r\nfundamentally I still follow the same process:<\/p>\r\n\r\n<ol>\r\n<li>Re-express the system in terms of state derivatives<\/li>\r\n<li>Add integrators and label the inputs and outputs<\/li>\r\n<li>Connect up the equations<\/li>\r\n<li>Set initial conditions<\/li>\r\n<\/ol>\r\n\r\n<p><strong> Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>Is this the same process you learned when you started using\r\nSimulink?  Do you have any special tips on how to draw ODEs?  Share your ideas\r\nand <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=15&amp;#comment\">leave a\r\ncomment here<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>I remember while learning Simulink, drawing ordinary\r\ndifferential equations was one of the early challenges.  Eventually I\r\ndiscovered a few steps that make it easier.  First, rewrite the equations... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2008\/05\/23\/how-to-draw-odes-in-simulink\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,29],"tags":[32,441,31],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/15"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}