Matlab Course. Anna Kristine Wåhlin. Department of Geophysics, University of Oslo. January Matlab Course p.1/??

Similar documents
Introduction to MATLAB

Ordinary Differential Equations (ODEs) Background. Video 17

1 Finite difference example: 1D implicit heat equation

Finite Difference Methods (FDMs) 1

System Simulation using Matlab

Physics 584 Computational Methods

Symbolic Solution of higher order equations

Applied Linear Algebra in Geoscience Using MATLAB

Fall 2003 Math 308/ Numerical Methods 3.6, 3.7, 5.3 Runge-Kutta Methods Mon, 27/Oct c 2003, Art Belmonte

Solving ODEs and PDEs in MATLAB. Sören Boettcher

MATH 333: Partial Differential Equations

NUMERICAL METHODS. lor CHEMICAL ENGINEERS. Using Excel', VBA, and MATLAB* VICTOR J. LAW. CRC Press. Taylor & Francis Group

Initial value problems for ordinary differential equations

ODE solvers in Julia. Gabriel Ingesson. October 2, 2015

A First Course on Kinetics and Reaction Engineering Supplemental Unit S5. Solving Initial Value Differential Equations

Lecture 9 Approximations of Laplace s Equation, Finite Element Method. Mathématiques appliquées (MATH0504-1) B. Dewals, C.

Iterative Methods for Linear Systems

Ordinary Differential Equations (ODE)

Syntax. Arguments. Solve m oderately stifo DEsand DAEs;trapezoidalrule. 1 of :34

Chapter 9 Implicit integration, incompressible flows

Ordinary Differential Equations I

Scientific Computing with Case Studies SIAM Press, Lecture Notes for Unit V Solution of

Ordinary Differential Equations (ode)

Lecture 8: Calculus and Differential Equations

Lecture 8: Calculus and Differential Equations

Linear Systems of Equations. ChEn 2450

Section 2.1 Differential Equation and Solutions

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

Chapter 1: Introduction

Final Examination. CS 205A: Mathematical Methods for Robotics, Vision, and Graphics (Fall 2013), Stanford University

Mathematical Computing

Lecture 16: Relaxation methods

Numerical Methods for Engineers and Scientists

WILEY. Differential Equations with MATLAB (Third Edition) Brian R. Hunt Ronald L. Lipsman John E. Osborn Jonathan M. Rosenberg

Ordinary Differential Equations I

Chapter 9b: Numerical Methods for Calculus and Differential Equations. Initial-Value Problems Euler Method Time-Step Independence MATLAB ODE Solvers

Applied Numerical Analysis

Math 308 Week 8 Solutions

10.34 Numerical Methods Applied to Chemical Engineering. Quiz 2

A Glimpse at Scipy FOSSEE. June Abstract This document shows a glimpse of the features of Scipy that will be explored during this course.

Diffusion / Parabolic Equations. PHY 688: Numerical Methods for (Astro)Physics

Mechanics of Structures (CE130N) Lab 3

Computer lab for MAN460

11.3 MATLAB for Partial Differential Equations

Lecture 42 Determining Internal Node Values

Numerical Solution Techniques in Mechanical and Aerospace Engineering

Numerical Methods Process Systems Engineering ITERATIVE METHODS. Numerical methods in chemical engineering Edwin Zondervan

1 Introduction to MATLAB

. For each initial condition y(0) = y 0, there exists a. unique solution. In fact, given any point (x, y), there is a unique curve through this point,

Lecture 2. Introduction to Differential Equations. Roman Kitsela. October 1, Roman Kitsela Lecture 2 October 1, / 25

AMS 27L LAB #8 Winter 2009

Main Steps Developing Your Own Simulator

APPLICATIONS OF FD APPROXIMATIONS FOR SOLVING ORDINARY DIFFERENTIAL EQUATIONS

Proper Orthogonal Decomposition. POD for PDE Constrained Optimization. Stefan Volkwein

Solving Differential Equations Using MATLAB

Index. higher order methods, 52 nonlinear, 36 with variable coefficients, 34 Burgers equation, 234 BVP, see boundary value problems

Boundary Value Problems and Iterative Methods for Linear Systems

AMSC/CMSC 466 Problem set 3

Part 1. The diffusion equation

PDEs in Image Processing, Tutorials

Numerical Solution of Laplace Equation By Gilberto E. Urroz, October 2004

Index. C 2 ( ), 447 C k [a,b], 37 C0 ( ), 618 ( ), 447 CD 2 CN 2

AMS 27L LAB #6 Winter 2009

1 Number Systems and Errors 1

Solving PDEs with CUDA Jonathan Cohen

Numerical Methods for Engineers. and Scientists. Applications using MATLAB. An Introduction with. Vish- Subramaniam. Third Edition. Amos Gilat.

Department of Mathematics California State University, Los Angeles Master s Degree Comprehensive Examination in. NUMERICAL ANALYSIS Spring 2015

2 Solving Ordinary Differential Equations Using MATLAB

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Cache Oblivious Stencil Computations

Linear Algebra. PHY 604: Computational Methods in Physics and Astrophysics II

Review for Exam 2 Ben Wang and Mark Styczynski

Introduction. Finite and Spectral Element Methods Using MATLAB. Second Edition. C. Pozrikidis. University of Massachusetts Amherst, USA

Lecture 2. Classification of Differential Equations and Method of Integrating Factors

Linear Equations and Matrix

AMS526: Numerical Analysis I (Numerical Linear Algebra for Computational and Data Sciences)

SYMBOLIC AND NUMERICAL COMPUTING FOR CHEMICAL KINETIC REACTION SCHEMES

Chapter 5. Methods for Solving Elliptic Equations

Numerical Implementation of Transformation Optics

Chabot College Fall Course Outline for Mathematics 25 COMPUTATIONAL METHODS FOR ENGINEERS AND SCIENTISTS

Elementary maths for GMT

MA3232 Summary 5. d y1 dy1. MATLAB has a number of built-in functions for solving stiff systems of ODEs. There are ode15s, ode23s, ode23t, ode23tb.

Sign the pledge. On my honor, I have neither given nor received unauthorized aid on this Exam : 11. a b c d e. 1. a b c d e. 2.

A Hybrid Method for the Wave Equation. beilina

sing matlab Farida Mosally Mathematics Department King Abdulaziz University

Math background. Physics. Simulation. Related phenomena. Frontiers in graphics. Rigid fluids

Parallelism in Structured Newton Computations

Lecture 18 Classical Iterative Methods

CHAPTER 6 STATE SPACE: FREQUENCY RESPONSE, TIME DOMAIN

u n 2 4 u n 36 u n 1, n 1.

M.A. Botchev. September 5, 2014

Additive Manufacturing Module 8

ME Computational Fluid Mechanics Lecture 5

Applied Linear Algebra in Geoscience Using MATLAB

Vector Fields and Solutions to Ordinary Differential Equations using Octave

10 Numerical Solutions of PDEs

Stability of Krylov Subspace Spectral Methods

2 Ordinary Differential Equations: Initial Value Problems

Numerical Methods of Applied Mathematics -- II Spring 2009

Vector Fields and Solutions to Ordinary Differential Equations using MATLAB/Octave

Transcription:

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.