Sunteți pe pagina 1din 9

Dierential Equations in Matlab

Cheng Ly1
1

University of Pittsburgh, Department of Mathematics, Pittsburgh, Pennsylvania

15260, USA.
E-mail: chengly@math.pitt.edu

This workshop assumes you have some familiarity with ordinary (ODEs) and partial dierential equations (PDEs), and also that you are relatively comfortable with basic programming in Matlab. Matlab is a very powerful high-level computing tool with a lot of nice built in packages. It is relatively easy to learn, but lags in computation time compared to complied languages such as Fortran, C, or C++. In particular, when solving PDEs with 3 or more spatial dimensions (i.e., 4 or more when including time) Matlab is not something you should use. However, for 2 or less spatial dimensions, Matlab is fast enough these days to be successfully utilized. Numerical simulations are undoubtedly an important tool in investigating the complex behavior of many systems (presumably that is why you are here unless you only came for the free pizza in which case you should...). Mathematical analysis and ingenuity are obviously important as well, but the value of numerical tools cannot be underscored.

1. Getting Started: A Quick Review


Matlab is optimized to work with vectors and matrices. You can specify variables to be row or column vectors. For example, the command >> x=[1 2 3]; creates a 3x1 row vector with the entries 1, 2, and 3. The semicolon ; at the end hides the output (try it without a semicolon). You can also create a column vector with the command >> x=[3; 2; 1]; The semicolons between [ and ] terminates that the row. You can access parts of the vector x. To see how this works, type >> x(1:2) and 1

>> x(2:end) and >> x(1:end-1) You can also reverse the order of the entries with a command like >> x(end:-1:2) Important functions. The function zeros(n,m) and ones(n,m) creates matrices of size n x m with all 0s and 1 entries (respectively). Also, eye(N) creates the identity matrix of size N x N. You can concatenate vectors and columns easily in between brackets. Type in the following commands: >> A=[1 0 -1; 2*ones(3,1); 8 zeros(2,1)] >> B=[eye(3); A] You can also multiply and divide component-wise when you have a matrix/vector with the . operator. Type: >> x./3 to divide every entry in x by 3. The function diag(x) creates a square matrix with x on the main diagonal and 0s on the o diagonal. The matrix of size N x N where N is length of the vector x. The command length(x) will return this length: >> length(x) Type: >> C=diag(x) The function diag can also be used to create matrices with entries above or below the main diagonal. If you want to create an N x N matrix with a vector z that is of size N-1 x 1, that is one above the main diagonal, you can type: >> z=[8; 4]; >> diag(z,1) to create a 3 x 3 matrix. Or if you want z to be 1 below the main diagonal type: >> diag(z,-1) For example, can you guess what you would get when you type in >> D=diag(x) + diag(x(1:2),1) - diag(x(2:3),-1) Now what we have a better understanding of how vectors/matrices work, we can proceed. Type >> clear 2

to delete all of the variables in our workspace. In Matlab, you work mostly with function les or script les (name.m). The dierence is that in a function le, the rst lines looks like: function [y,z]=name(x1,x2) where the outputs are y and z, the inputs are x1,x2. You can run this function in the command window by typing: >> y=name(x1,x2); or >> [y,z]=name(x1,x2); A script le is just a series of commands that you would type into the command line. There is a dierence in scope. The function le has a local scope (you can create new variables in there that will be deleted when you exit) versus in a script le, you have access to all the current variables in your workspace. Any changes to the variables you do in the script le will be changed in your workspace as well.

2. ODEs
Matlab can solve large dimensional ODE systems with built-in solvers. You may want to consider using XPP if you are going to alter many parameters and do other dynamical system things (i.e., bifurcation diagrams, phaseplane analysis, etc.). Note that higher level programming is easier to implement in Matlab. Examples are complicated logical (if-else) statements in your system, weird boundary conditions, as well as look up tables [Sorry Bard]. You can use look-up tables in XPP as well, but Im not sure how well it is documented. There are various methods (see Matlabs documenation), but I will consider my favorite sti solver: ode15s. For this workshop, we will assume the ODEs are rst-order. For higher orders, we simply have to introduce auxiliary variables and enlarge the size of the system. Consider y R2 , and an ODE system of the form y1 = y1 (a1 + a2 y2 ) y2 = y1 y2 with some initial conditions. (1) (2)

Open up the le rhs.m ; notice the syntax for this le. It basically species the right-hand side of the ODE system above. The parameters come after t (independent variable) and y (the functions y1 =y(1) and y2 =y(2). Fill in the appropriate commands for y1 =dy(1) and y2 =dy(2)..... ask for help if there is something unclear. Now open the le slv ode.m. Notice the initial conditions are random. Also the parameters for the time-length (t end) and a1 and a2 are already lled in (you can play with these later). Ignore the command options=..; you can explore this later by typing help ode15s in the command lind. You will write the syntax to solve the ODE [T,Y] = ode15s(@rhs,[0 t end],y0,options,a 1,a 2); Type the following in the command line: >> slv ode do this several times and see what happens. Before moving on to the next section, type: >> clear

3. PDEs
We will discuss how to numerically solve partial dierential equations of the form (x R1 and t [0, )): u(x, t) 2 A(x, t)u(x, t) 2 D (x)u(x, t) = 0. + t x x

(3)

These are called advection-diusion equations and arise all of the time in the physical and life sciences. This encompasses equations called Fokker-Planck, Black-Scholes, and a biological phenomena known as chemotaxis with some augmentations. For this workshop, we will consider the following advection-diusion equation: 2 u(x, t) 1 ((t) x)u(x, t) D 2 u(x, t) = 0 + t x x as follows. The advection term
1 x

(4)

With zero boundary conditions: limx u(x, t) = 0. This can be understood intuitively move (or advect) toward (t) with a velocity of ((t) x)u(x, t)
1 .

causes the mass at x = (t) to

Thus, if is smaller, the mass moves


2

faster towards (t). Of course, the value (t) is changing in time which adds to the
complications of these dynamics. The diusion term D x u(x, t) 2

spreads or smears

D that is constant here. Think of heat or gas diusing on a line. Many dierent systems have a D that depends on x: D (x) (equation 3), which also complicates things.

out the mass equally in all directions (2 directions here since x R1 ) with a coecient of

3.1. Implementation
Let us rst rewrite the PDE u(x, t) 2 u(x, t) (t) u(x, t) 1 xu(x, t) + D = t x x2 x (5)

Discretize time with a mesh size t << 1 (say t = 0.01), and label tk = 0 + t(k 1) (for k = 1, 2, .., 5001). In general, k = 1, 2, ..., Tend/t + 1. We approximate the timeu u(x, tk ) u(x, tk1 ) = t t Let us focus on the x derivatives in u and forget the time derivative
u t

On a computer, we cant go out to t = , so we pick a large enough Tend = 50 say.

derivative with the nite dierence method:

for the

moment. Again on a computer, we cant go out to x = , so we will choose some M large enough and consider x [M, M ]. Let x = 2M/(N 1) << 1, by choosing M = 25 and N = 200. Label xj = M + x(j 1) for j = 1, 2, ..., N . In both x and

t variables, we are assuming equally spaced points (i.e., uniform mesh) 1 , but t = x necessarily (in fact, this is usually the case). Write equation (4) as follows: 2 u(x, t) (t) u(x, t) u(x, tk ) u(x, tk1) xu(x, t) + D = t x x2 x discretize the right-hand-side of equation (5). Advection to 0 operator (M0) We want a matrix M0 that represents the operator: xu(x, t) = M0u x We will use a simple forward nite dierence method: xj +1 uj +1 xj uj xj u(xj , t) = x x
Many complicated numerical solvers do not assume a uniform mesh, although you can get quite far with this assumption. Some people have debated this issue and the answer is that it all depends on your problem (i.e., domain, stiness, etc.).
1

(6)

Let u = [u1 ; u2 ; ..; uN ], where uj = u(xj , t). We will now create the matrices to

So the j th row of M0 looks like [0...; jx ;

xj +1 ; 0; ..0] x

Diusion operator (Md) Similarly, we want to form the matrix Md so that 2 u(xj , t) = Mdu x2

We will use a standard 2nd derivative nite dierence approximation: 2 uj +1 2uj + uj 1 u ( x , t ) = j x2 x2 ; 2 ; 1 ; ..0] So the j th row of Md looks like [0...; 1 x2 x2 x2 Advection to (t) operator (Am) Similarly, we want to form the matrix Am u(x, t) = Amu x We will use a simple forward nite dierence method: uj +1 + uj u(xj , t) = x x so that

1 1 So the j th row of Am looks like [0...; ; ; 0; ..0] x x

I have avoided talking about boundary conditions so far because they can be quite

complicated. Neglecting boundary conditions, you can now think about how you would create the matrices. Some people would use a for-loop but they are NOT a good idea in Matlab because they take enormous computation times (for example if you make N large). You should look at the diagonal structure of these operators (recall the function diag). Now, open the le make matrix.m. Look at the syntax and the commands. If you have any questions, now is the time to discuss this. In this le (make matrix.m), insert the following commands in M0=diag([0;-x(2:end-1);0]./dx)+diag([x(2:end-1);0]./dx,1); and Md=diag([-1;-2*ones(N-2,1);-1]./(dx^ 2))+... diag(ones(N-1,1)./(dx^ 2),1)+diag(ones(N-1,1)./(dx^ 2),-1); and Am=diag([0;ones(N-2,1);0]./dx)-diag([ones(N-2,1);0]./dx,1); After this, type in M0=sparse(M0); Md=sparse(Md); 6

Am=sparse(Am); to make the matrices sparse. This will speed up the matrix-vector solver that we will write below. 3.1.1. Solve the PDE with Trapezoidal Rule Now that we have all our operators created in the le make matrix.m, we are ready to (crudely) solve this system. The trapezoidal rule is a decent implicit solver (recall equation (5)): u(x, tk ) = u(x, tk1) + t 0.5 RHS (u(x, tk1)) + 0.5 RHS (u(x, tk )) u(x, tk ) 0.5 t RHS (u(x, tk )) = u(x, tk1 ) + 0.5 t RHS (u(x, tk1)) Here, RHS is the right-hand-side operators RHS (u(x, tk )) = 1/ M 0 + D Md (tk )/ Am u(:, k ) Scroll down to the for loop that begins with for j=2:length(t). Think about how you would solve the matrix-vector equation given the inputs in this le.... HINT: It amounts to solving A*u(:,j)=b for each j=2,3,..,length(t). And we can write A=eye(N)-.5*dt*(1/tau*M0+D*Md+mu t(j)/tau*Am); and similarly for b. ..... ..... Now that we have all our les written, we can solve the PDE. In the command line, type >> help make matrix You should be able to see the form of the function and description. Now type >> [M0,Md,Am,x]=make matrix(25,200); Now we have our matrices in the workspace. Specify tau, D, and the end of time T end with the commands >> tau=2; >> D=5; >> T end=30; SOLVE THE PDE with the command: 7

[t,x,u,mu t]=solve advDif(tau,D,T end,M0,Md,Am,x); We will look at movies of our solutions in a bit.....

3.2. Monte Carlo Simulations


Dont be alarmed if you do not know any stochastic dierential equations or statistical physics. But.. The advection-diusion equation (4) describes how the probability density u(x, t) of a random variable X (t) evolves. The equation is: = (t) X + X 2D (t)

where (t) is a white noise process (i.e. a Gaussian random variables for all t with 0 mean and variance of 1; where each Gaussian random variable is uncorrelated in time). Since we took u(x, 0) = [0,1] , we can think of the initial condition X (0) as a random variable itself with uniform distribution. In the command line, type: >> avgX = monteCarlo sims(t,x,tau,D,mu t); After it is nished running, type: >> movie scpt to see a movie of how well the solution u(x, t) approximates the noisy system. The match is pretty good despite the crudeness of the methods (rst order). Other issues to be aware of: i) mesh size isnt large enough (in x or t, ii) conserving probability with weird boundary conditions, etc. You can go back and play with the function mu t in the le solve advDif if you are comfortable with the syntax, but you MUST enlarge your domain and mesh size x appropriately!

4. Nonlinear PDEs
Nonlinear PDEs are a mess to solve. Here is all I have to say about it. The instabilities that arise generally call for an implicit method (at the very least) to have any hope of a reasonable numerical solution. The following sections assume you want an implicit time-stepping solver.

4.1. Method of Lines


If your PDE is nonlinear in u(x, t), then you have to use a fancier method than what has been outlined. One method is to write the PDE as a large system of ODEs and use the Method of Lines (Google it). This basically involves discretizing your domain x [M, M ] into N pieces of width x = 2M/N , and letting u(xj , t) = u(M + for j = 1, 2, .., N . We still x(j 1), t) represent
1 x M +xj u(x, t) dx. Note M +x(j 1) u ( x,tk )u(x,tk1 ) = . have u t t

here that xj = M + x(j 1)

The system of ODEs can be written down if you discretize the advection and diusion parts with a nite dierence method. For example, you could use a centered-dierence method: A(xj +1 , t)u(xj +1, t) A(xj 1 , t)u(xj 1, t) A(xj , t)u(xj , t) = x 2x 2 D (xj +1 )u(xj +1, t) 2D (xj , t)u(xj ) + D (xj 1)u(xj 1 , t) D ( x ) u ( x , t ) = j j x2 x2 Care must be taken with the boundary conditions. Again, you might want to use XPP if your boundary conditions are not nasty. There are many good nonlinear ODE solvers once you have properly discretized the system (e.g., CVODE, ode15s, etc.).

4.2. Newtons Method


Alternatively, you could use Newtons Method to resolve the nonlinearity at each time step. This involves nding u(x, tk ) given that you know u(x, tk ). There are many deep and troubling issues such as uniqueness of roots, stability, etc. This is not unexpected with nonlinear PDEs.

S-ar putea să vă placă și