Using MATLAB`s Differential Equation Solver

advertisement
Using MATLAB’s Ordinary Differential Equation Solver
This tutorial goes along with Example 2.1 in the textbook (pages 22-24).
Solving one ODE
dy
 ...
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:
1. Write the ODE in the form
function tempname = actualname(time, function, othervar)
tempname = …;
For Example 2.1 you might write:
function dxdt = conc(t,x,C,tau)
dxdt = (C-x)/tau;
4. Save the M-file.
5. In the command window, use the following commands to solve the ODE and
store it in a vector:
[t, y] = ode45(@actualname, [time range],initial condition, [],
other variables);
For Example 2.1 part b) you would write:
[t, x] = ode45(@conc, [0 25], 0.5, [], 0.517, 3);
The empty brackets in the above statement are necessary and tell MATLAB to
skip the parameters t and x in the M-file conc.
Type the command plottools.
On the left hand side, click where it says “2D Axes”
On the right hand side, click the button “Add Data”
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.
6.
7.
8.
9.
Solving two ODE’s simultaneously
dy
 ...
dt
For our example, the equations are
1. Write both ODE’s in the form
dV 1
 ( w1  w2  w)
dt

w
dx w1

( x1  x)  2 ( x 2  x)
dt V
V
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.
y1 y 2 y3 ...  dy1 dy 2 dy3 ...
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:
function tempname = actualname(time, vectorfunction, othervar)
tempname = [function1; function2; function3; …];
For our example you might write:
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)];
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:
[t, y] = ode45(@actualname, [time range],[initial condition1;
initial condition2;…], [], other variables);
For our example you would write:
[t,y]=ode45(@Volconc,[0 25],[2;0.5],[],400,200,700,0.4,0.75,900);
The empty brackets in the above statement are necessary and tell MATLAB to
skip the parameters t and y in the M-file Volconc.
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.
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
For more information on graphics and using plottools, use MATLAB’s help system and
select:
MATLAB>Graphics>MATLAB Plotting Tools
As always, if you still have questions, feel free to email me (weber@che.utexas.edu) or
come by my office (CPE 5.416).
Download