{"id":104,"date":"2010-07-30T21:05:26","date_gmt":"2010-07-30T21:05:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2010\/07\/30\/did-i-just-get-a-different-answer\/"},"modified":"2010-07-30T21:05:26","modified_gmt":"2010-07-30T21:05:26","slug":"did-i-just-get-a-different-answer","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2010\/07\/30\/did-i-just-get-a-different-answer\/","title":{"rendered":"Did I just get a different answer?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>As part of any well designed Model Based Design work-flow, you need to ensure that you get the expected results from your simulation.  \r\n         You might want to evaluate the effect of a change to the model, or just need to validate that your results are consistent\r\n         between normal mode simulation and running the generated code.\r\n      <\/p>\r\n      <p>To illustrate this idea, I will compare two signals generated from two slightly different implementations of the van der Pol\r\n         equations.  To keep this post simple, I will focus only on the state x2.\r\n      <\/p>\r\n   <\/introduction>\r\n   <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">open_system(<span style=\"color: #A020F0\">'vdp_1'<\/span>)\r\nopen_system(<span style=\"color: #A020F0\">'vdp_2'<\/span>)\r\n[t1,xout1] = sim(<span style=\"color: #A020F0\">'vdp_1'<\/span>);\r\n[t2,xout2] = sim(<span style=\"color: #A020F0\">'vdp_2'<\/span>);\r\nx1 = xout1(:,2);\r\nx2 = xout2(:,2);<\/pre><h3>Quick test for equality<a name=\"2\"><\/a><\/h3>\r\n   <p>Do a quick test for equality between variables using <tt>isequal<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">isequal(x1,x2)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><p>This gives you a fast way to cycle through tons of data and turn up the subset that requires further investigation.<\/p>\r\n   <p>For a sanity check, I think it is natural to plot the data to see if the difference is visible.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(t1,x1,t2,x2)\r\nlegend(<span style=\"color: #A020F0\">'Sim1'<\/span>,<span style=\"color: #A020F0\">'Sim2'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/b104_comparingAnswers_01.png\"> <p>This is actually rarely helpful for small differences in signals. In this case, the green line is on top of the blue line,\r\n      so they must be the same, right?  Well, they must be the same at least to the resolution of my monitor.  Depending on the\r\n      reason you are comparing these signals, you might stop here, but if you need more precision, how about looking at the difference?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(t1,x1-x2)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/b104_comparingAnswers_02.png\"> <h3>Computing the Absolute Difference<a name=\"6\"><\/a><\/h3>\r\n   <p>If signals are periodic, and the error is cumulative, the difference will generally also be periodic.  I am often trying to\r\n      evaluate if the difference is growing in magnitude or shrinking. I usually look at the absolute difference between signals.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x_diff = abs(x1-x2);\r\nplot(t1,x_diff)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/b104_comparingAnswers_03.png\"> <p>If you want a difference measure, you could imagine that your solution is a vector in a high dimensional vector space.  I\r\n      imagine the difference between the solutions to be the magnitude of the difference.  To compute this, you might use the <tt>norm<\/tt><\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">nx = norm(x1-x2)<\/pre><pre style=\"font-style:oblique\">nx =\r\n  1.5108e-012\r\n<\/pre><p>Because I often see a wide range of differences in signals, I think it is better to use a logarithmic scale in the plot. \r\n      When differences enter into a system on the order of eps, and grow larger, the logarithmic scale will help draw attention\r\n      to the small magnitude differences as much as the larger magnitude differences.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">semilogy(t1,x_diff,<span style=\"color: #A020F0\">'.-'<\/span>)\r\nset(gca,<span style=\"color: #A020F0\">'YGrid'<\/span>,<span style=\"color: #A020F0\">'on'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2010Q3\/b104_comparingAnswers_04.png\"> <h3>Now it's your turn<a name=\"9\"><\/a><\/h3>\r\n   <p>When do you compare signals?  How do you do it?  Leave a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=100&#comment\">comment here<\/a> and share your technique.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f8187663c7de4929b4def326930eb3f8() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='f8187663c7de4929b4def326930eb3f8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f8187663c7de4929b4def326930eb3f8';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Seth Popinchalk';\r\n        copyright = 'Copyright 2010 MathWorks';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f8187663c7de4929b4def326930eb3f8()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\nf8187663c7de4929b4def326930eb3f8 ##### SOURCE BEGIN #####\r\n%% Did I just get a different answer?\r\n% \r\n% As part of any well designed Model Based Design work-flow, you will need a\r\n% way to test to ensure the results from your simulation agree  You might\r\n% want to evaluate the effect of a change to the model, or just need to\r\n% validate that your results are consistent between normal mode simulation\r\n% and running the generated code.\r\n%\r\n% To illustrate this idea, I will compare two signals generated from two\r\n% slightly different implementations of the Van Der Pol equations.  To keep\r\n% this post simple, I will focus only on the state x2.\r\n\r\n%%\r\nopen_system('vdp_1')\r\nopen_system('vdp_2')\r\n[t1,xout1] = sim('vdp_1');\r\n[t2,xout2] = sim('vdp_2');\r\nx1 = xout1(:,2);\r\nx2 = xout2(:,2);\r\n\r\n%% Quick test for equality\r\n% Do a quick test for equality between variables using |isequal|.\r\n\r\nisequal(x1,x2)\r\n\r\n%%\r\n% This gives you a fast way to cycle through tons of data and turn up the\r\n% subset that requires further investigation.\r\n\r\n%%\r\n% For a sanity check, I think it is natural to plot the data to see if the\r\n% difference is visible.\r\n\r\nplot(t1,x1,t2,x2)\r\nlegend('Sim1','Sim2')\r\n\r\n%%\r\n% This is actually rarely helpful for small differences in signals. In this\r\n% case, the green line is on top of the blue line, so they must be the\r\n% same, right?  Well, they must be the same at least to the resolution of\r\n% my monitor.  Depending on the reason you are comparing these signals, you\r\n% might stop here, but if you need more precision, how about looking at the\r\n% difference?\r\n\r\nplot(t1,x1-x2)\r\n\r\n%% Computing the Absolute Difference\r\n% If signals are periodic, and the error is cumulative, the difference will\r\n% generally also be periodic.  I am often trying to evaluate if the\r\n% difference is growing in magnitude or shrinking. I usually look at the\r\n% absolute difference between signals.\r\n\r\nx_diff = abs(x1-x2);\r\nplot(t1,x_diff)\r\n\r\n%%\r\n% If you want a difference measure, you could imagine that your solution is\r\n% a vector in a high dimensional vector space.  I imagine the difference\r\n% between the solutions to be the magnitude of the difference.  To compute\r\n% this, you might use the |norm|\r\n\r\nnx = norm(x1-x2)\r\n\r\n%%\r\n% Because I often see a wide range of differences in signals, I think it is\r\n% better to use a logarithmic scale in the plot.  When differences enter\r\n% into a system on the order of eps, and grow larger, the logarithmic scale\r\n% will help draw attention to the small magnitude differences as much as\r\n% the larger magnitude differences.\r\n\r\nsemilogy(t1,x_diff,'.-')\r\nset(gca,'YGrid','on')\r\n\r\n%% Now it's your turn\r\n% When do you compare signals?  How do you do it?  Leave a comment here and\r\n% share your technique.\r\n\r\n##### SOURCE END ##### f8187663c7de4929b4def326930eb3f8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      As part of any well designed Model Based Design work-flow, you need to ensure that you get the expected results from your simulation.  \r\n         You might want to evaluate the effect... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2010\/07\/30\/did-i-just-get-a-different-answer\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[67,102,76,63],"tags":[146],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/104"}],"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=104"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}