Introduction to Matlab |
The final application is the solution of ordinary differential equations.
Suppose we want to find the solution u(t) of the ordinary differential
equation
| (3) |
| (4) |
function u_dot = disease( t , u ) % Place this code in % % file disease.m % Evaluates the rhs of the ode k = .2 ; u_dot = k * u * ( 1 - u ) ;
(Although t is not used in calculating u_dot, ode45 requires the function disease to have two input arguments (in the correct order). Use help ode45 for more information.)
Having set up the problem, the differential equation is solved and graphed in just one Matlab one line!
[ t, u ] = ode45( 'disease', [0,100] , 1/10000 ); plot( t,u,'x')
The function ode45 also has parameters which can be adjusted to control the accuracy of the computation. Mostly, these are not necessary for just plotting the solution to ordinary problems. However ode45 does not guarantee its accuracy, and it is always a good idea to check our original solution by resolving the problem with higher accuracy requirements. For example, we can use ode45 so that the estimated absolute and relative errors are kept less than 1×10-9. Also to ensure a very smooth graph, values of the solution are computed every half day. This is done with the commands
options = odeset('abstol',1.e-9,'reltol', 1.e-9) ; [tt,uu] = ode45('disease', [0:.5:100], 1/10000, options); plot( t,u,'x', tt,uu,'-' )
We immediately see that the solution computed in the first call to ode45 lies on top of the more accurate solution curve. That is the two solutions coincide to graphical accuracy.
For this simple model it is possible to find an analytic formula for u(t).
In fact
| (5) |
t0 = (0:.5:100)' ; c = 10000-1 ; k = .2 ; u0 = 1 ./ ( 1 + c*exp( -k*t0 ) ) ; plot( t,u,'x', tt,uu,'-', t0,u0,'--' )
The exact formula gives a curve that is the same as the numerical solution. In fact the difference between the two solutions is at most 2.5×10-7. So we can be confident in using ode45 to solve the problems below.
(a.) Check that (5) is a solution to (3). That is, when we differentiate this formula for u(t) we get the right side of (3). And this formula satisfies (4)
(b.)From the graph of u(t), estimate how many days are needed for half the population to be infected? (Hint: The command grid will make this easier to answer.)
(c.) Suppose now that after the infection is noted at day t = 0 , an inoculation program is started from day 5, and 200 of the
uninfected animals are inoculated per day. After t days, the proportion of
animals inoculated per day is 200 (t-5)+/10000. ((t-5)+ means max{t-5,0}. This could be coded in Matlab as t5 = max([t-5,0]) for
example.) These inoculated animals cannot be infected, an new infections
occur when an uninfected, uninoculated animal meets an infected animal. Thus
the rate of increase of u is now modelled by the differential equation
|
(d.) How many animals would be uninfected if only 100 animals could be inoculated each day from day 5? How many animals would have been saved if 100 animals per day were inoculated from day 0?
This simple model problem also has a formula for the exact solution. However in Matlab it is very easy to get a numerical solution; and this can be done even in complicated problems when there is no simple formulae for the solution. Using Matlab we can simply explore the model by altering the parameters and plotting the solutions!
As a second example, consider the ordinary differential equation
|
|
|
|
|
|
Thus to solve (6) and plot u(t) against t we place the following code in the file F.m
function Y_dot = F( t,Y ) % % Note Y(2) is the Matlab % RHS for the 2nd order ODE % notation for the second % % compontent of Y. It does % % not mean Y(t) at time t=2 Y_dot = [ Y(2) ; -sin( Y(1) ) ] ;
Then we use the command
[t,Y] = ode45( 'F', [0:.25:20], [pi/4;0] ); % Solve ODE and plot u = Y(:,1) ; plot( t, u,'-' ) % u(t).
Note that ode45 stores the first and second components of the solution in the first and second columns of the output.
(a.) From the graph, what is the period of the pendulum?
(b.) Draw a graph of the pendulum's path in the (x,y) plane.
(c.) Often when the pendulum swings through small angles, the
equation (6) is approximated by
|
|