{"id":2687,"date":"2013-10-10T09:03:58","date_gmt":"2013-10-10T14:03:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=2687"},"modified":"2018-06-21T11:46:16","modified_gmt":"2018-06-21T16:46:16","slug":"from-symbolic-to-simulink","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2013\/10\/10\/from-symbolic-to-simulink\/","title":{"rendered":"From Symbolic to Simulink"},"content":{"rendered":"<p>I am recently visited the <a href=\"https:\/\/www.mathworks.com\/\">MathWorks Japan office<\/a> and learned about interesting work done by my colleague <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/39309\">Hitoshi Takeshita<\/a>.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/www.mathworks.com\/matlabcentral\/profiles\/1892040.jpg\" alt=\"Hitoshi Takeshita\" \/><\/p>\r\n\r\n<p>In his MATLAB Central submission <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42196-euler-lagrange-equation\">Euler\u2013Lagrange equation<\/a>, Hitoshi shows how the <a href=\"https:\/\/www.mathworks.com\/help\/symbolic\/index.html\">Symbolic Math Toolbox<\/a> can be used to easily obtain the equations of motion of a system by simply defining the energies involved. Once the equations of motion are obtained, he then uses the <a href=\"\">code generation capability of the Symbolic Math Toolbox<\/a> to create a Simscape component and simulate the system in Simulink.<\/p>\r\n\r\n<p>I had never used this workflow before, but it can be very useful in applications like plant modeling and for engineering students learning systems dynamics.<\/p>\r\n\r\n<p>Let's look at an example using a double mass-spring system:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/mass_spring_system2.png\" alt=\"Double mass spring system\" \/><\/p>\r\n\r\n<p><strong>Modeling a dynamic system in MuPAD<\/strong><\/p>\r\n\r\n<p><a href=\"https:\/\/www.mathworks.com\/discovery\/mupad.html\">MuPAD<\/a> is the engine of the Symbolic Math Toolbox. If you never used it, I recommend going through the <a href=\"https:\/\/www.mathworks.com\/help\/symbolic\/getting-started-with-mupad.html\">Getting Started<\/a> section of the documentation, or read some of the posts in the <a href=\"https:\/\/blogs.mathworks.com\/loren\/category\/symbolic\/\">Symbolic category of Loren's blog<\/a>.<\/p>\r\n\r\n<p>In the MuPAD Notebook provided by Hitoshi, all you need to do is define the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kinetic_energy\">kinetic energy<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Potential_energy\">potential energy<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Lagrangian_mechanics#Dissipation_function\">dissipation function<\/a>:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/energy_in_mupad.png\" alt=\"Defining energies\" \/><\/p>\r\n\r\n<p>The rest of the notebook implements the Euler Lagrange equation:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/lagrange_equation.png\" alt=\"Euler Lagrange Equation\" \/><\/p>\r\n\r\n<p>So all you need to do is evaluate the notebook:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/mupad_evaluate_all.png\" alt=\"evaluate the MuPAD notebook\" \/><\/p>\r\n\r\n<p>and you should obtain the equations of motion of the system:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/mupad_final_equations.png\" alt=\"Equations of motion\" \/><\/p>\r\n\r\n<p><strong>From MuPAD to Simulink<\/strong><\/p>\r\n\r\n<p>Now that we have the equations of motion of the system, we want to bring them into Simulink. For that, we use the Simscape language.<\/p>\r\n\r\n<p>In the MuPAD notebook, it is possible to generate Simscape equations using the <a href=\"\"><tt>generate<\/tt><\/a> function:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/mupad_generate.png\" alt=\"Generating Simscape language in MuPAD\" \/><\/p>\r\n\r\n<p>We can then copy and paste this code in the <a href=\"https:\/\/www.mathworks.com\/help\/physmod\/simscape\/lang\/equations.html\">equations<\/a> section of a <a href=\"https:\/\/www.mathworks.com\/help\/physmod\/simscape\/lang\/about-simscape-files.html\">Simscape file<\/a>. We let you write the rest of the Simscape so you can integrate the equations the way you want. In my case, I created a component with two ports, keeping the motion of the middle mass internal to my component. I also modified the names of a few variables auto-generated by MuPAD:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/massSpringSimscape.png\" alt=Using code generated from MuPAD in a Simscape component\" \/><\/p>\r\n\r\n<p>I can then execute <a href=\"https:\/\/www.mathworks.com\/help\/physmod\/simscape\/ref\/ssc_build.html\"><tt>ssc_build<\/tt><\/a> on this file and obtain a block that I can use in Simulink:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/massSpringModel.png\" alt=\"Simulink model of a double mass spring system\" \/><\/p>\r\n\r\n<p>Using the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/28184-simscape-simulation-results-explorer\">Simscape Simulation Results Explorer<\/a> I can see that the two masses are moving as expected.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/springMassresults.png\" alt=\"results from Simulink model of a double mass spring system\" \/><\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Using this method for a simple system like this one is probably not the most efficient way to quickly get a working simulation.<\/p>\r\n\r\n<p>However, I think this method can be very interesting for plant modelers analyzing complex systems and for engineering students trying to get familiar with Lagrange mechanics and equations of motion.<\/p>\r\n\r\n<p>Do you need to combine symbolic manipulations and Simulink? Is the approach highlighted here relevant for your applications? Let us know what you think by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=2687&#comment\">comment here<\/a>.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2013Q3\/springMassresults.png\" onError=\"this.style.display ='none';\" \/><\/div><p>I am recently visited the MathWorks Japan office and learned about interesting work done by my colleague Hitoshi Takeshita.\r\n\r\n\r\n\r\nIn his MATLAB Central submission Euler\u2013Lagrange equation, Hitoshi... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2013\/10\/10\/from-symbolic-to-simulink\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,29,39],"tags":[342,105,343],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2687"}],"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\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=2687"}],"version-history":[{"count":34,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2687\/revisions"}],"predecessor-version":[{"id":7666,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/2687\/revisions\/7666"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=2687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=2687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=2687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}