Matlab Course Anna Kristine Wåhlin Department of Geophysics, University of Oslo January 2003 Matlab Course p.1/??
Numerical estimate of the derivative An estimate of the time derivative of dataset at time interval is sampled (27) The differentiation can be done with different methods in Matlab. 1. For-loop: for n = 1:N der(n) = (A(n+1) - A(n))/dt; end remember to presize der 2. Vector: der = (A(2:N) - A(1:N-1))/dt; 3. Built-in function der = diff(a)/dt; Day 2 p.93/??
Day 2 p.94/?? The function diff diff(a,m) Performs the differencing M times recursively diff(b), where is a matrix performs the differentiation along each row diff(c), where C an M-dimensional matrix performs the differntiation along the first non-singleton dimension
More dimensions Gradient (28) The gradient of a scalar field in N dimensions is calculated with the function gradient, which is simply diff applied along each of the dimensions. Two dimensions Three dimensions N dimensions Example graddemo.m Day 2 p.95/??
Day 2 p.96/?? More dimensions LaPlace operator The curvature of a scalar field in N dimensions is calculated with the function del2, that uses a centered finite difference scheme in N dimensions. (29) L = del2(a) Calculates the LaPlacian in N dimensions L = del2(a,x,y,z,...,n) Uses the grid spacing defined by (X,Y,Z,...,N)
Day 2 p.102/?? Ordinary differential equations Analytical solutions can be attempted with the function dsolve. Usage: y = dsolve( Equation, BC1, BC2,..., variable ) S = dsolve( Equation1, Equation2, BC1,...) Example odean.m
Day 2 p.103/?? Ordinary differential equations A number of different ODE solvers are available in Matlabs library. The different solvers are designed for various types of ODEs (stiff or non-stiff etc) and different levels of accuracy. A complete list of the solvers can be found by typing help ode23. The usage is equivalent and will here be demonstrated with the solver ODE23 (a solver for non-stiff ODEs, low order method).
Day 2 p.104/?? The solution at times [t, y] = ode23(odefun,tspan,y0) Manual for use of ODE23 to the ODE is obtained by where odefun is a function file that describes the ODE. The time span is tspan = [t0 tend] and initial conditions are y0 = y(t0). The file odefun should return the time derivative given the input and, i. e. (30)
Day 2 p.105/?? Manual for use of ODE23 If a system of differential equations is going to be solved then odefun should have the form (31) To solve higher-order differential equations, convert to a system of first-order equations.
Day 2 p.106/?? Example: Bessel s equation of zeroth order Bessel s equation can be converted to the following set of first-order differential equations: (32) (33)
Day 2 p.107/?? bess.m function dydt = bess(t,y) g = y(1); f = y(2); dgdt = -g/t-f; dfdt = g; dydt = [dgdt; dfdt];
Day 2 p.108/?? odedemo.m clear clf % clears the figure window [t23,y23] = oded23( bess,[eps,10],[0;1]); % The function can not handle t = 0 bess23 = y23(:,2); [t45,y45] = ode45( bess,[eps,10],[0;1]); % use t = eps instead bess45 = y45(:,2); t = linspace(0,10,1000); plot(t,besselj(0,t), b,t23,bess23, r,t45,bess45, g ) legend( Analytical, ODE23, ODE45 )
Day 2 p.109/?? Partial differential equations In order to illustrate two techniques for PDE solving the diffusion (or heat) equation will be used: (34) For constant an initial distribution spreads in all directions according to (35) where is the distance to the origin.
Explicit numerical scheme The field T at time t + t is approximately given by T 2 ( t + t) T ( t) κ t T, or 2 T( t + t) = T ( t) + tκ T. The function del2 calculates the LaPlacian: del2(t) del2(t) del2(t) 1 2 T 4 1 or 2 dimensions 1 2 T 6 3 dimensions 1 2 T 2*dim( T) N dimensions
The Matlab code for the explicit scheme looks like this: T = T + dt*k*del2(t)*2*ndims(t); Explicit scheme ndims(t) Number of (non-singleton) dimensions del2(t,dx,dy,dz,...) Grid spacing dx, dy etc del2(t,xv,yv,zv,...) Grid defined by vectors Xp, Yp etc del2(t,xm,ym,zm,...) Grid defined by matrices Xp, Yp etc Example: explic3.m
Day 2 p.111/?? Implicit numerical scheme In an implicit scheme the space de rivative is calculated at time. The temperature at this time is then the solution to the linear equation system (37) where (38) is a tridiagonal NxN matrix and
Day 2 p.112/?? Implicit numerical scheme There are several ways to solve this in Matlab. The quickest method makes use of LU decomposition for sparse matrices, [L,U\] = lu(a); T = U\(L\T); The time taken for this operation can be compared with the slower Gaussian elimination, T = A\T; or the even slower inverse matrix method, T = inv(a)*t; Example: implicit2.m
Day 2 p.113/?? Implicit numerical scheme The extension to several dimensions can be done in many ways for the implicit scheme, for example by splitting the operator and compute each direction independently.
Day 2 p.114/?? The PDE toolbox The special toolbox for calculation of partial differential equations includes a library with numerical schemes of different orders. There is also a set of aides for finite element methods, with or without graphical user interface. The program Femlab is very user-friendly and was developed from this toolbox. For more information, go to the Partial Differential Equation Toolbox page in the Helpdesk.