MATH 267 Use of MATLAB to solve ODEs MATLAB contains very easy to use commands for solving initial value problems for ordinary differential equations. I. Single equations. Suppose we wish to obtain a numerical solution of the single first order equation. y 0 = f (t, y) y(t0 ) = y0 This can be done with the following command1 : >>[t y] = ode45(f,tspan,y0); where the inputs f,tspan,y0 and outputs t,y are as follows. f: defines the function in the ODE. For example to get f (t, y) = t2 − y 3 , give the MATLAB command 2 >>f=inline(’tˆ2-yˆ3’,’t’,’y’) tspan: defines the endpoints of the interval on which you want the solution, e.g. >> tspan=[1 3] to get the solution for 1 ≤ t ≤ 3. The first entry should be t0 . y0: the initial value, e.g, for y0 = 4 give the MATLAB command >> y0=4 t: a vector of t values t1 , t2 . . . tn at which the approximate solution is given. y: a vector of y values y1 , y2 . . . yn where yj is the approximate solution at tj . The command >>plot(t,y) tells MATLAB to plot the solution, and you can save the figure in a convenient format for printing by pulling down the file menu in the figure window. II. Systems. For the case of systems, the procedure is similar. The command >> [t,y]=ode45(F,tspan,y0); returns the solution of the initial value problem for the system defined by the vector function F on the interval defined in tspan with initial conditions defined by y0. If the system has n unknown component functions then the output array y contains n columns, one for each component. The inline command can be used to define F as in the case of the single ODE. Suppose for example that the problem of interest is x01 = tx2 + sin (x1 x2 ) x02 = x1 e−tx2 1 x1 (1) = 3 x2 (1) = −2 (1) (2) Here and below >> is the MATLAB prompt, and the semicolon at the end of any line is optional, telling MATLAB not to display on the screen the results of the computation it has just carried out 2 Alternatively, you may define f by means of an m-file, if you know how to do this, in which case the calling command should be >>[t y] = ode45(’f’,tspan,y0) or >>[t y] = ode45(@f,tspan,y0) 1 This means that the system may be written in vector form as x0 = F(t, x) where tx2 + sin (x1 x2 ) F(t, x) = x1 e−tx2 A MATLAB command which will define this vector function is >> F=inline(’[t*x(2)+sin(x(1)*x(2));x(1)*exp(-t*x(2))]’,’t’,’x’) The input tspan defines the interval of t values on which the solution is to be found, as in the single equation case, with initial conditions given by y0, which in this case should be >> y0=[3;-2] (Why the semicolon in y0? It forces y0 to be a column vector. Without the semicolon, as in the definition of tspan, you are defining a row vector. The semicolon also occurs in the definition of F because it needs to be a column vector, but tspan can be either a row or column vector.) To plot the solution, give the command3 >> plot(t,y(:,1));shg which will create a plot of the first component of the solution. Replace 1 by 2 to get the second component. You can get both curves on the same plot with the command >> plot(t,y(:,1),t,y(:,2));shg HOMEWORK 1. Use the above procedures to numerically solve y 0 = t2 − y 3 y(1) = 4 on the interval 1 ≤ t ≤ 3 and plot the result. (Hand in the plot only, you should not print out a listing of all of the yn values!) 2. Numerically solve y0 = y + t y(0) = 1 on the interval 0 ≤ t ≤ 1 and plot the result. Find the exact solution and compare the numerical solution to the exact solution at t = 12 and t = 1. 3. Obtain the numerical solution of the system (1)-(2) above for 1 ≤ t ≤ 4. Plot both components of the solution on the same axes. 4. Transform the problem t y 00 = 3y − sin (y 0 − ) 2 0 < t < 2 y(0) = 1 y 0 (0) = −2 into an equivalent problem for a first order system and solve numerically using ode45. Plot the solution. (Remember the solution is a single function in this case, not two functions.) 3 The shg command causes the graph window to be come visible, if it isn’t already. 2