Using MATLAB’S Differential Equation Solver

Using MATLAB’S Differential Equation Solver

<p> Using MATLAB’s Ordinary Differential Equation Solver</p><p>This tutorial goes along with Example 2.1 in the textbook (pages 22-24).</p><p>Solving one ODE</p><p> dy 1. Write the ODE in the form  ... dt dx C * x For Example 2.1, the equation is  dt  2. Create a new M-file by selecting File>New>M-File 3. Write the ODE in M-file form using the following format:</p><p> function tempname = actualname(time, function, othervar) tempname = …;</p><p>For Example 2.1 you might write:</p><p> function dxdt = conc(t,x,C,tau) dxdt = (C-x)/tau;</p><p>4. Save the M-file. 5. In the command window, use the following commands to solve the ODE and store it in a vector:</p><p>[t, y] = ode45(@actualname, [time range],initial condition, [], other variables);</p><p>For Example 2.1 part b) you would write:</p><p>[t, x] = ode45(@conc, [0 25], 0.5, [], 0.517, 3);</p><p>The empty brackets in the above statement are necessary and tell MATLAB to skip the parameters t and x in the M-file conc.</p><p>6. Type the command plottools. 7. On the left hand side, click where it says “2D Axes” 8. On the right hand side, click the button “Add Data” 9. In the window that pops up select the appropriate vectors in the drop down menus which hold the data you want to plot on the x and y axes. In our case we want to plot “t” as the X Data Source and “x” as the Y Data Source. Then click OK. 10. You can add axis labels and a title by selecting Insert> and the appropriate label.</p><p>Solving two ODE’s simultaneously</p><p> dy 1. Write both ODE’s in the form  ... dt For our example, the equations are dV 1  (w  w  w) dt  1 2 dx w w  1 (x  x)  2 (x  x) dt V 1 V 2 2. On paper, set up a vector that will contain all of the functions for which you want to solve. This vector will have a corresponding first derivative vector that holds the derivative functions from step 1.</p><p>dy1 dy2 dy3  y1 y2 y3 ...  ...  dt dt dt  For our example we will use the vectors: dV dx V x  dt dt  3. Create a new M-file by selecting File>New>M-File 4. Write the ODE’s in M-file form using the following format:</p><p> function tempname = actualname(time, vectorfunction, othervar) tempname = [function1; function2; function3; …];</p><p>For our example you might write:</p><p> function dydt = Volconc(t,y,w1,w2,w,x1,x2,rho) dydt = [(w1+w2-w)/rho; (w1*(x1-y(2))+w2*(x2-y(2)))/(y(1)*rho)];</p><p>Notice that the vector which will hold our solutions for both V and x is called y. We must therefore refer to V as y(1) and x as y(2) in the function statement. 5. Save the M-file. 6. In the command window, use the following commands to solve the ODE and store it in a vector:</p><p>[t, y] = ode45(@actualname, [time range],[initial condition1; initial condition2;…], [], other variables);</p><p>For our example you would write:</p><p>[t,y]=ode45(@Volconc,[0 25],[2;0.5],[],400,200,700,0.4,0.75,900);</p><p>The empty brackets in the above statement are necessary and tell MATLAB to skip the parameters t and y in the M-file Volconc.</p><p>7. Type the command plottools. 8. On the left hand side, click where it says “2D Axes” 9. On the right hand side, click the button “Add Data” 10. In the window that pops up select the appropriate vectors in the drop down menus which hold the data you want to plot on the x and y axes. In our case we want to plot “t” as the X Data Source and “y” as the Y Data Source. Then click OK. 11. If you don’t want all the functions plotted on the same graph you can select the data you don’t want on the graph, right click and select Cut. Then you can click “2D Axes” again to get a new set of axes to plot the other functions. 12. You can add axis labels and a title by selecting Insert> and the appropriate label.</p><p>For more information on using the ODE solver, use MATLAB’s help system and select: MATLAB>Mathematics>Differential Equations>Initial Value Problems for ODEs and DAEs</p><p>For more information on graphics and using plottools, use MATLAB’s help system and select: MATLAB>Graphics>MATLAB Plotting Tools</p><p>As always, if you still have questions, feel free to email me ([email protected]) or come by my office (CPE 5.416).</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us