{"id":60,"date":"2009-07-14T20:24:02","date_gmt":"2009-07-14T20:24:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/07\/14\/refining-the-output-of-a-simulation\/"},"modified":"2009-07-14T20:25:29","modified_gmt":"2009-07-14T20:25:29","slug":"refining-the-output-of-a-simulation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/07\/14\/refining-the-output-of-a-simulation\/","title":{"rendered":"Refining the Output of a Simulation"},"content":{"rendered":"<p>Today I am pleased to share a post from regular guest\r\nblogger, <a\r\nhref=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/31651\">Guy\r\nRouleau<\/a>.\u00a0 Enjoy!<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/guy_rouleau.png\" alt=\"Guy Rouleau - the servo controller\"><\/p>\r\n\r\n<p>I often use Simulink to model the dynamics of simple\r\nsystems. In most cases, the Simulink default settings provide a good tradeoff\r\nbetween accuracy and simulation speed. These settings usually allow me to\r\nobserve the signals I am interested in.<\/p>\r\n\r\n\r\n\r\n<p>I recently ran into situations where I needed to change\r\nthe default Simulink settings to observe the signals I was expecting.<\/p>\r\n\r\n\r\n\r\n<p>Let\u2019s begin by a very simple case: Simulating a 100Hz\r\nsine wave of amplitude 1 for 10 seconds<\/p>\r\n\r\n\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/SineWaveModel.png\" alt=\"The Simplest Simulink model, just a Sine Wave\"><\/p>\r\n\r\n\r\n\r\n<p>With the default Simulink settings, the Sine wave observed\r\non the scope is the following:<\/p>\r\n\r\n\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/SineScope1.png\" alt=\"The Scope shows only the data that is sampled, in this case, all zeros.\"><\/p>\r\n\r\n\r\n\r\n<p>Before screaming \u201cSimulink is broken!\u201d, let\u2019s look at the\r\nwarning displayed at the MATLAB command prompt when playing this model. <\/p>\r\n\r\n\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/cmdWarning.png\" alt=\"Warnings in the command window.\"><\/p>\r\n\r\n\r\n\r\n<p>The first warning mentions that the selected solver\r\n\"ode45\" is replaced by the Variable-Step Discrete solver. The second warning\r\nsays that Simulink will use a default step size of 0.2 sec. <\/p>\r\n\r\n\r\n\r\n<p>The combination of these two warnings results in Simulink\r\nevaluating the Sine wave at time [ 0 \u00a00.2 \u00a00.4 \u00a0\u2026\u00a0 9.8 \u00a010 ] where it\u2019s value\r\nis always zero. To observe the Sine wave properly, a very useful option is to\r\nrefine the output:<\/p>\r\n\r\n\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/SineRefineFactor.png\" alt=\"Configuration Parameters refine factor\"><\/p>\r\n\r\n\r\n\r\n<p>By setting an appropriate value for the refine factor, it\r\nis possible to observe the expected Sine wave. This is the result with a refine\r\nfactor of 250:<\/p>\r\n\r\n\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/SineRefine250.png\" alt=\"Sine wave with a refine factor of 250\"><\/p>\r\n\r\n\r\n\r\n<p>Now you might think, \"Interesting, but I never create\r\nmodels with only a Sine Wave.\" I agree, so let\u2019s look at a more realistic\r\nsimulation and model a servo motor commanded by a discrete controller. Our\r\nSimulink model looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/ServoExampleModel.png\" alt=\"Servo motor example\"><\/p>\r\n\r\n<p>Using the default settings, the Scope block displays the\r\nposition, velocity and acceleration as:<\/p>\r\n\r\n\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/ServoScopeRefine1.png\" alt=\"Servo Scope, no refine factor\"><\/p>\r\n\r\n<p>Attentive eyes probably noticed that after 0.2 second,\r\nthe acceleration signal seems constant around a value of 14, while the velocity\r\nsignal is also constant at 1. Since the slope of the input ramp is 1, we can conclude\r\nthat Simulink computes the velocity value accurately, but why isn\u2019t the acceleration\r\nzero as one could expect?<\/p>\r\n\r\n\r\n\r\n<p>Maybe we should refine the output. Setting the refine\r\nfactor to 4 provides a better resolution in time and shows the real behavior of\r\nthe system.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q3\/ServoScopeRefine4.png\" alt=\"Servo scope with a refine factor of 4\"><\/p>\r\n\r\n<p>In that case, it shows that the servo motor acceleration\r\nsignal oscillates when submitted to the 100Hz discrete steps of the command\r\ncoming from the computer.<\/p>\r\n\r\n\r\n\r\n<p><strong>What else should we know about the refine factor?<\/strong><\/p>\r\n\r\n<p>\r\n    <ul>\r\n    <li>To get smoother output and have a better time resolution, it is\r\n    much faster to change the refine factor instead of reducing the step size. <\/li>\r\n    \r\n    <li>When the refine factor is changed, the solvers generate\r\n    additional points by evaluating a continuous extension formula at those points.<\/li>\r\n    \r\n    <li>The refine factor applies to variable-step solvers and is most\r\n    useful when you are using ode45.<\/li>\r\n    \r\n    <li>Usually a value of 4 produces much smoother results.<\/li>\r\n    <\/ul>\r\n<\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n\r\n<p>Do you ever use refine factor?\u00a0 Leave a <a\r\nhref=\"https:\/\/blogs.mathworks.com\/seth\/?p=60&amp;#comment\">comment here<\/a> and\r\nshare your experience.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Today I am pleased to share a post from regular guest\r\nblogger, Guy\r\nRouleau.\u00a0 Enjoy!\r\n\r\n\r\n\r\nI often use Simulink to model the dynamics of simple\r\nsystems. In most cases, the Simulink default... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/07\/14\/refining-the-output-of-a-simulation\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[76,29,33],"tags":[98,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/60"}],"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=60"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}