{"id":2937,"date":"2024-12-11T09:33:44","date_gmt":"2024-12-11T14:33:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/matlab\/?p=2937"},"modified":"2024-12-11T09:33:44","modified_gmt":"2024-12-11T14:33:44","slug":"solving-higher-order-odes-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/matlab\/2024\/12\/11\/solving-higher-order-odes-in-matlab\/","title":{"rendered":"Solving Higher-Order ODEs in MATLAB"},"content":{"rendered":"<div class = rtcContent><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><a href = \"https:\/\/blogs.mathworks.com\/matlab\/?p=1589#reply_2616897\"><span>In a comment<\/span><\/a><span> to last year's introduction to the new ODE solution framework in MATLAB, Ron asked if I could provide an example of using it to solve a 2nd order ODE since most tutorials don't deal with the new syntax. Absolutely I can! <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>So let's start at the beginning. An <\/span><span style=' font-style: italic;'>ordinary differential equation<\/span><span> (ODE) contains one or more derivatives of a dependent variable, <\/span><span style=' font-style: italic;'>y<\/span><span>, with respect to a single independent variable, <\/span><span style=' font-style: italic;'>t<\/span><span>, usually referred to as time. The notation used here for representing derivatives of <\/span><span style=' font-style: italic;'>y<\/span><span> with respect to <\/span><span style=' font-style: italic;'>t<\/span><span> is <\/span><span style=' font-style: italic;'>y<\/span><span>\u2032 for a first derivative, <\/span><span style=' font-style: italic;'>y<\/span><span>\u2032\u2032 for a second derivative, and so on. The <\/span><span style=' font-style: italic;'>order<\/span><span> of the ODE is equal to the highest-order derivative of <\/span><span style=' font-style: italic;'>y<\/span><span> that appears in the equation.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>This equation then is second order<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;my&lt;\/mi&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;mo&gt;+&lt;\/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;ky&lt;\/mi&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;mn&gt;0&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-5px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><mi mathvariant=\"normal\">my<\/mi><mo>\u2032<\/mo><mo>\u2032<\/mo><mo>+<\/mo><mi mathvariant=\"normal\">ky<\/mi><mo>=<\/mo><mn>0<\/mn><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>MATLAB's current crop of numerical ODE solvers only solve first order equations so we have work to do before we can proceed. It turns out that <\/span><span style=' font-weight: bold;'>you can write any higher-order ODE as a system of first-order ODEs<\/span><span>. <\/span><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span style=' font-weight: bold;'>Calculating the system of ODEs by hand<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>You first make the following definitions<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-6px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>1<\/mn><\/mrow><\/msub><mo>=<\/mo><mi mathvariant=\"italic\">y<\/mi><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-6px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>2<\/mn><\/mrow><\/msub><mo>=<\/mo><msup><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mo>\u2032<\/mo><\/mrow><\/msup><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Then we take their derivatives<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-6px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><msup><mrow><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>1<\/mn><\/mrow><\/msub><\/mrow><mrow><mo>\u2032<\/mo><\/mrow><\/msup><mo>=<\/mo><msup><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mo>\u2032<\/mo><\/mrow><\/msup><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-6px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><msup><mrow><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>2<\/mn><\/mrow><\/msub><\/mrow><mrow><mo>\u2032<\/mo><\/mrow><\/msup><mo>=<\/mo><msup><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mo>\u2032<\/mo><mo>\u2032<\/mo><\/mrow><\/msup><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Do substitutions using our definitions and our original second order equation<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-6px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><msup><mrow><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>1<\/mn><\/mrow><\/msub><\/mrow><mrow><mo>\u2032<\/mo><\/mrow><\/msup><mo>=<\/mo><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>2<\/mn><\/mrow><\/msub><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot; display=&quot;block&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;\/mrow&gt;&lt;\/msup&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;mo&gt;-&lt;\/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;k&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;m&lt;\/mi&gt;&lt;\/mrow&gt;&lt;\/mfrac&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;italic&quot;&gt;y&lt;\/mi&gt;&lt;\/mrow&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;\/mn&gt;&lt;\/mrow&gt;&lt;\/msub&gt;&lt;\/mrow&gt;&lt;\/math&gt;\" style=\"vertical-align:-15px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\" display=\"block\"><mrow><msup><mrow><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>2<\/mn><\/mrow><\/msub><\/mrow><mrow><mo>\u2032<\/mo><\/mrow><\/msup><mo>=<\/mo><mo>-<\/mo><mfrac><mrow><mi mathvariant=\"italic\">k<\/mi><\/mrow><mrow><mi mathvariant=\"italic\">m<\/mi><\/mrow><\/mfrac><msub><mrow><mi mathvariant=\"italic\">y<\/mi><\/mrow><mrow><mn>1<\/mn><\/mrow><\/msub><\/mrow><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>and we have our system of first order ODEs. We write this system as the following function<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span style=\"color: #008013;\">% Define the system of first-order ODEs<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >k = 10; <\/span><span style=\"color: #008013;\">% Spring Constant<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >m = 1; <\/span><span style=\"color: #008013;\">% Mass<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >odeSystem = @(t, y) [y(2); -k\/m * y(1)];<\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>So far, nothing is new. This is the exact same method you've always had to follow for doing this in MATLAB.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We plug <\/span><span style=' font-family: monospace;'>odeSystem <\/span><span>into the new framework as follows, also stating our initial conditions which are <\/span><span style=' font-family: monospace;'>y=1<\/span><span> and <\/span><span style=' font-family: monospace;'>y'=0<\/span><span> at <\/span><span style=' font-family: monospace;'>t=0<\/span><span>. This is the only step that's different from any tutorials you might have seen before.<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >shm = ode(ODEFcn=odeSystem,InitialTime=0,InitialValue=[1;0]);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >shmSol = solve(shm,-5,5);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '>&nbsp;<\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >plot(shmSol.Time,shmSol.Solution,<\/span><span style=\"color: #a709f5;\">'-o'<\/span><span >);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >legend([<\/span><span style=\"color: #a709f5;\">\"y\"<\/span><span >,<\/span><span style=\"color: #a709f5;\">\"y'\"<\/span><span >])<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >title(<\/span><span style=\"color: #a709f5;\">\"Solution of the 2nd order system and first derivative\"<\/span><span >)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >xlabel(<\/span><span style=\"color: #a709f5;\">\"t\"<\/span><span >)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" uid=\"0D152FD7\" prevent-scroll=\"true\" data-testid=\"output_0\" tabindex=\"-1\" style=\"width: 1145px;\"><div class=\"figureElement eoOutputContent\" role=\"article\" aria-roledescription=\"Use Browse Mode to explore \" aria-description=\"figure output \"><img decoding=\"async\" class=\"figureImage figureContainingNode\" src=\"http:\/\/blogs.mathworks.com\/matlab\/files\/2024\/12\/HighODE2_1.png\" style=\"width: 560px;\"><\/div><div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer navigationFocusLayer doNotExport\" aria-hidden=\"false\" tabindex=\"-1\" role=\"application\" aria-label=\"figure output \"><\/div><\/div><\/div><\/div><\/div><h3  style = 'margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span>Making use of a solutionFcn<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Something to point out here is that our starting point is at <\/span><span style=' font-family: monospace;'>t=0 <\/span><span>and that since we asked for a range from <\/span><span style=' font-family: monospace;'>t=-5<\/span><span> to <\/span><span style=' font-family: monospace;'>t=5<\/span><span> we are integrating in both directions automatically, allowing the solver to chose whatever values of <\/span><span style=' font-family: monospace;'>t<\/span><span> it deems are best. As you can see from the plot above, the solver used a lot of points around <\/span><span style=' font-family: monospace;'>t=0<\/span><span> in both directions. <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We can get more control by defining a <\/span><a href = \"https:\/\/uk.mathworks.com\/help\/matlab\/ref\/ode.solutionfcn.html\"><span style=' font-family: monospace;'>solutionFcn<\/span><\/a><span>. Then we can see the solution at a set of uniform points in time<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >y = solutionFcn(shm,-5,5,OutputVariables=1);  <\/span><span style=\"color: #008013;\">% Represents the solution of our system<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >t = linspace(-5,5,100);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >plot(t,y(t),<\/span><span style=\"color: #a709f5;\">'-o'<\/span><span >)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >title(<\/span><span style=\"color: #a709f5;\">\"Solution of the 2nd order system\"<\/span><span >)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >xlabel(<\/span><span style=\"color: #a709f5;\">\"t\"<\/span><span >)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" uid=\"074E2955\" prevent-scroll=\"true\" data-testid=\"output_1\" tabindex=\"-1\" style=\"width: 1145px;\"><div class=\"figureElement eoOutputContent\" role=\"article\" aria-roledescription=\"Use Browse Mode to explore \" aria-description=\"figure output \"><img decoding=\"async\" class=\"figureImage figureContainingNode\" src=\"http:\/\/blogs.mathworks.com\/matlab\/files\/2024\/12\/HighODE2_2.png\" style=\"width: 560px;\"><\/div><div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer navigationFocusLayer doNotExport\" aria-hidden=\"false\" tabindex=\"-1\" role=\"application\" aria-label=\"figure output \"><\/div><\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>To recover the derivative, use the second output of the <\/span><span style=' font-family: monospace;'>solutionFcn,<\/span><span> <\/span><span style=' font-family: monospace;'>y<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >[yt,ypt] = y(t);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >plot(t,yt,<\/span><span style=\"color: #a709f5;\">'-o'<\/span><span >,t,ypt,<\/span><span style=\"color: #a709f5;\">'-o'<\/span><span >)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >legend([<\/span><span style=\"color: #a709f5;\">\"y\"<\/span><span >,<\/span><span style=\"color: #a709f5;\">\"y'\"<\/span><span >])<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >title(<\/span><span style=\"color: #a709f5;\">\"Solution of the 2nd order system and first derivative\"<\/span><span >)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >xlabel(<\/span><span style=\"color: #a709f5;\">\"t\"<\/span><span >)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" uid=\"EA39B27C\" prevent-scroll=\"true\" data-testid=\"output_2\" tabindex=\"-1\" style=\"width: 1145px;\"><div class=\"figureElement eoOutputContent\" role=\"article\" aria-roledescription=\"Use Browse Mode to explore \" aria-description=\"figure output \"><img decoding=\"async\" class=\"figureImage figureContainingNode\" src=\"http:\/\/blogs.mathworks.com\/matlab\/files\/2024\/12\/HighODE2_3.png\" style=\"width: 560px;\"><\/div><div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer navigationFocusLayer doNotExport\" aria-hidden=\"false\" tabindex=\"-1\" role=\"application\" aria-label=\"figure output \"><\/div><\/div><\/div><\/div><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span style=' font-weight: bold;'>Calculating the system of ODEs using Symbolic Toolbox<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I'm not going to lie to you, I don't trust it when I do mathematics by hand. Even for something as simple as this I make mistakes and for a more complex equation you can bet good money that I'll mess it up. I much prefer it when I can get the computer to do the heavy lifting for me. That's what they are for! <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I started off by asking development why we don't 'just' support higher order ODEs directly in the new framework. Once I put my dollar in the 'Just Jar', they told me that its something they've thought about off and on for a while. Could I get more user stories where people would value such functionality? So here I am asking, dear reader, would such direct support be useful for you and, if so, what's your application?<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>In the meantime, I found a page in the documentation that <\/span><a href = \"https:\/\/uk.mathworks.com\/help\/symbolic\/solve-differential-equation-numerically-1.html\"><span>shows how to use symbolic toolbox to do the transformation to a system of ODEs for us<\/span><\/a><span> using the <\/span><a href = \"https:\/\/uk.mathworks.com\/help\/symbolic\/odetovectorfield.html\"><span style=' font-family: monospace;'>odeToVectorField<\/span><\/a><span> function. Let's apply it to the problem here. Recall, this is the problem we want to solve<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span mathmlencoding=\"&lt;math xmlns=&quot;http:\/\/www.w3.org\/1998\/Math\/MathML&quot;&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;my&lt;\/mi&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;mo&gt;\u2032&lt;\/mo&gt;&lt;mo&gt;+&lt;\/mo&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;ky&lt;\/mi&gt;&lt;mo&gt;=&lt;\/mo&gt;&lt;mn&gt;0&lt;\/mn&gt;&lt;\/math&gt;\" style=\"vertical-align:-5px\"><math xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"><mi mathvariant=\"normal\">my<\/mi><mo>\u2032<\/mo><mo>\u2032<\/mo><mo>+<\/mo><mi mathvariant=\"normal\">ky<\/mi><mo>=<\/mo><mn>0<\/mn><\/math><\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We set up some symbolic variables<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 4px; padding: 6px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >syms <\/span><span style=\"color: #a709f5;\">y(t) m k <\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>and pass our equation to <\/span><a href = \"https:\/\/uk.mathworks.com\/help\/symbolic\/odetovectorfield.html\"><span style=' font-family: monospace;'>odeToVectorField<\/span><\/a><span> using the Symbolic Toolbox function <\/span><span style=' font-family: monospace;'>diff<\/span><span> to handle the derivatives<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 4px 4px 0px 0px; padding: 6px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >[V] = odeToVectorField(m*diff(y, 2) + k*y ==0)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"symbolicElement\" data-previous-available-width=\"1108\" data-previous-scroll-height=\"88\" data-hashorizontaloverflow=\"false\" style=\"max-height: 261px;\"><div class=\"embeddedOutputsVariableElement\">V&nbsp;=&nbsp;<\/div><span class=\"MathEquation\" style=\"vertical-align: -25px;\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/blogs.mathworks.com\/matlab\/files\/2024\/12\/HighODE2_4.png\" width=\"53\" height=\"62\"><\/span><\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>That result looks familiar! It's the same system I derived by hand earlier. We need to convert it to a MATLAB function so I can pass it to the numerical ODE framework<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 4px 4px 0px 0px; padding: 6px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >M = matlabFunction(V,<\/span><span style=\"color: #a709f5;\">'vars'<\/span><span >,{<\/span><span style=\"color: #a709f5;\">'t'<\/span><span >,<\/span><span style=\"color: #a709f5;\">'Y'<\/span><span >,<\/span><span style=\"color: #a709f5;\">'m'<\/span><span >,<\/span><span style=\"color: #a709f5;\">'k'<\/span><span >})<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsVariableStringElement\" uid=\"354B015F\" prevent-scroll=\"true\" data-testid=\"output_4\" tabindex=\"-1\" style=\"width: 1145px; white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\"><div class=\"textElement eoOutputContent\" tabindex=\"-1\" data-previous-available-width=\"1108\" data-previous-scroll-height=\"35\" data-hashorizontaloverflow=\"false\" role=\"article\" aria-roledescription=\"Use Browse Mode to explore \" aria-description=\"variable output \" style=\"max-height: 261px; white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\"><div style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\"><span class=\"variableNameElement\" style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\">M = <span class=\"headerElement\" style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\">function_handle with value:<\/span><\/span><\/div><div style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\">    @(t,Y,m,k)[Y(2);-(k.*Y(1)).\/m]\r\n<\/div><\/div><\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Now I need to sort out the parameters <\/span><span style=' font-family: monospace;'>m<\/span><span> and <\/span><span style=' font-family: monospace;'>k<\/span><span>. The function above has four input parameters but I want one that only takes two.<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >k = 10; <\/span><span style=\"color: #008013;\">% Spring Constant<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >m = 1; <\/span><span style=\"color: #008013;\">% Mass<\/span><\/span><\/div><\/div><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >odeSystemFromSymbolic = @(t, y) M(t,y,m,k)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsVariableStringElement\" uid=\"7BD1111D\" prevent-scroll=\"true\" data-testid=\"output_5\" tabindex=\"-1\" style=\"width: 1145px; white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\"><div class=\"textElement eoOutputContent\" tabindex=\"-1\" data-previous-available-width=\"1108\" data-previous-scroll-height=\"35\" data-hashorizontaloverflow=\"false\" role=\"article\" aria-roledescription=\"Use Browse Mode to explore \" aria-description=\"variable output \" style=\"max-height: 261px; white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\"><div style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\"><span class=\"variableNameElement\" style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\">odeSystemFromSymbolic = <span class=\"headerElement\" style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\">function_handle with value:<\/span><\/span><\/div><div style=\"white-space: pre; font-style: normal; color: rgb(33, 33, 33); font-size: 12px;\">    @(t,y)M(t,y,m,k)\r\n<\/div><\/div><\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>we pass this into the ODE framework as before<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 15px 10px 0; display: inline-block\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >shm = ode(ODEFcn=odeSystemFromSymbolic,InitialTime=0,InitialValue=[1;0]);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >shmSol = solve(shm,-5,5);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '>&nbsp;<\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >plot(shmSol.Time,shmSol.Solution,<\/span><span style=\"color: #a709f5;\">'-o'<\/span><span >);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >legend([<\/span><span style=\"color: #a709f5;\">\"y\"<\/span><span >,<\/span><span style=\"color: #a709f5;\">\"y'\"<\/span><span >])<\/span><\/span><\/div><\/div><div class=\"inlineWrapper outputs\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >title(<\/span><span style=\"color: #a709f5;\">\"Solution of the 2nd order system\"<\/span><span >)<\/span><\/span><\/div><div  style = 'color: rgb(33, 33, 33); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% \/ auto padding-box border-box; font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; overflow-x: hidden; line-height: 17.234px; '><div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" uid=\"8DFDFA7E\" prevent-scroll=\"true\" data-testid=\"output_6\" tabindex=\"-1\" style=\"width: 1145px;\"><div class=\"figureElement eoOutputContent\" role=\"article\" aria-roledescription=\"Use Browse Mode to explore \" aria-description=\"figure output \"><img decoding=\"async\" class=\"figureImage figureContainingNode\" src=\"http:\/\/blogs.mathworks.com\/matlab\/files\/2024\/12\/HighODE2_5.png\" style=\"width: 560px;\"><\/div><div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div><div class=\"outputLayer navigationFocusLayer doNotExport\" aria-hidden=\"false\" tabindex=\"-1\" role=\"application\" aria-label=\"figure output \"><\/div><\/div><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>This gives the identical result but I didn't need to any algebra by hand, which is always a good thing! Of course, we are also free to use any of the other machinery in the new ODE framework including <\/span><span style=' font-family: monospace;'>solutionFcn<\/span><span> but I won't show that again for the sake of space.<\/span><\/div>\r\n<\/div><script type=\"text\/javascript\">var css = '.eoOutputWrapper { width: calc(90vw - 10px) !important; } \/* Styling that is common to warnings and errors is in diagnosticOutput.css *\/.embeddedOutputsErrorElement {    min-height: 18px;    max-height: 550px;} .embeddedOutputsErrorElement .diagnosticMessage-errorType {    overflow: auto;} .embeddedOutputsErrorElement.activeOutput .eoOutputContent {    user-select: text;    -webkit-user-select: text;} .embeddedOutputsErrorElement.activeOutput .eoOutputContent button {    user-select: none;    -webkit-user-select: none;} .embeddedOutputsErrorElement .eoOutputContent ::selection {} .embeddedOutputsErrorElement.inlineElement {} .embeddedOutputsErrorElement.rightPaneElement {} \/* Styling that is common to warnings and errors is in diagnosticOutput.css *\/.embeddedOutputsWarningElement {    min-height: 18px;    max-height: 550px;} .embeddedOutputsWarningElement .diagnosticMessage-warningType {    overflow: auto;} .embeddedOutputsWarningElement.activeOutput .eoOutputContent {    user-select: text;    -webkit-user-select: text;} .embeddedOutputsWarningElement .eoOutputContent ::selection {} .embeddedOutputsWarningElement.inlineElement {} .embeddedOutputsWarningElement.rightPaneElement {} \/* Copyright 2015-2023 The MathWorks, Inc. *\/\/* In this file, styles are not scoped to rtcContainer since they could be in the Dojo Tooltip *\/.diagnosticMessage-wrapper {    font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace;    font-size: 12px;} .diagnosticMessage-wrapper.diagnosticMessage-warningType {    \/*This fallback value will be used for appdesigner warnings*\/    color: var(--rtc-warning-output-color, var(--mw-color-matlabWarning));} .diagnosticMessage-wrapper.diagnosticMessage-warningType a {    \/*This fallback value will be used for appdesigner warnings*\/    color: var(--rtc-warning-output-color, var(--mw-color-matlabWarning));    text-decoration: underline;} .rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-warningType,.rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-warningType a {    color: var(--mw-color-matlabWarning) !important;} .diagnosticMessage-wrapper.diagnosticMessage-errorType {    \/*This fallback value will be used in appdesigner error tooltip text*\/    color: var(--rtc-error-output-color, var(--mw-color-matlabErrors));} .diagnosticMessage-wrapper.diagnosticMessage-errorType a {    \/*This fallback value will be used in appdesigner error tooltip text*\/    color: var(--rtc-error-output-color, var(--mw-color-matlabErrors));    text-decoration: underline;} .rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-errorType,.rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-errorType a {    color: var(--mw-color-matlabErrors) !important;} .diagnosticMessage-wrapper .diagnosticMessage-messagePart,.diagnosticMessage-wrapper .diagnosticMessage-causePart {    white-space: pre-wrap;} .diagnosticMessage-wrapper .diagnosticMessage-stackPart {    white-space: pre;} .embeddedOutputsTextElement,.embeddedOutputsVariableStringElement {    white-space: pre;    word-wrap:  initial;    min-height: 18px;    max-height: 550px;} .embeddedOutputsTextElement .textElement,.embeddedOutputsVariableStringElement .textElement {    overflow: auto;} .embeddedOutputsTextElement.activeOutput .eoOutputContent,.embeddedOutputsVariableStringElement.activeOutput .eoOutputContent {    user-select: text;    -webkit-user-select: text;} \/*embeddedOutputsTextElement has a different dom structure than embeddedOutputsVariableStringElement.Unlike variableString, the text output has both TEXT nodes and elements as children. Hence we needa selector for each.*\/.embeddedOutputsTextElement .eoOutputContent::selection,.embeddedOutputsTextElement .eoOutputContent ::selection,.embeddedOutputsVariableStringElement .eoOutputContent ::selection {} .textElement,.rtcDataTipElement .textElement {    padding-top: 2px;} .embeddedOutputsTextElement.inlineElement,.embeddedOutputsVariableStringElement.inlineElement {} .inlineElement .textElement {} .embeddedOutputsTextElement.rightPaneElement,.embeddedOutputsVariableStringElement.rightPaneElement {    min-height: 16px;} .rightPaneElement .textElement {    padding-top: 2px;    padding-left: 9px;}'; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/matlab\/files\/2024\/12\/HighODE2_3.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>In a comment to last year's introduction to the new ODE solution framework in MATLAB, Ron asked if I could provide an example of using it to solve a 2nd order ODE since most tutorials don't deal with... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/matlab\/2024\/12\/11\/solving-higher-order-odes-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":176,"featured_media":2967,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[54,85],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts\/2937"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/users\/176"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/comments?post=2937"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts\/2937\/revisions"}],"predecessor-version":[{"id":2979,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts\/2937\/revisions\/2979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/media\/2967"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/media?parent=2937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/categories?post=2937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/tags?post=2937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}