matlab, numerical integration, and simulationmeam535/fall03/slides/numerics... · matlab, numerical...

17
1 1 University of Pennsylvania GRASP MEAM 535 Peng Song Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills n Visualization n Ways to look for help n Numerical integration n Integration methods: explicit, implicit; one-step, multi-step n Accuracy and numerical stability n Stiff systems n Programming examples n Solutions to HW0 using Matlab n Mass-spring-damper system 2 University of Pennsylvania GRASP MEAM 535 Peng Song MATLAB n MATLAB (MATrix LABoratory) is an interpretative (interactive) programming language n control flow statements, functions, data structures, input/output, and object-oriented programming features n MATLAB working environment. n tools for developing, managing, debugging, and profiling M-files n MATLAB graphics system n 2D and 3D data visualization, image processing, animation, and presentation graphics n MATLAB mathematical function library n MATLAB API n interact with C and Fortran programs

Upload: dothuan

Post on 08-Mar-2018

260 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

1

1University of Pennsylvania GRASP

MEAM 535

Peng Song

Matlab, Numerical Integration, and Simulation

n Matlab tutorialn Basic programming skillsn Visualizationn Ways to look for help

n Numerical integrationn Integration methods: explicit, implicit; one-step, multi-stepn Accuracy and numerical stabilityn Stiff systems

n Programming examplesn Solutions to HW0 using Matlab n Mass-spring-damper system

2University of Pennsylvania GRASP

MEAM 535

Peng Song

MATLAB

n MATLAB (MATrix LABoratory) is an interpretative (interactive) programming language n control flow statements, functions, data structures, input/output,

and object-oriented programming features

n MATLAB working environment. n tools for developing, managing, debugging, and profiling M-files

n MATLAB graphics system n 2D and 3D data visualization, image processing, animation, and

presentation graphics

n MATLAB mathematical function library n MATLAB API

n interact with C and Fortran programs

Page 2: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

2

3University of Pennsylvania GRASP

MEAM 535

Peng Song

Basic Matrix-Vector Operations

n Enter a matrix:>>A=[ 3 2; 2 4]n All matrices are enclosed in a pair of bracket [ ].n Case-sensitive: matrix A and matrix a are different.

n Enter a vector >>b = [4;3];n b is a 2 x 1 column vector.n The semicolon sign at the end of the line indicates that the

interpreter will not echo that line on the screen.

n Matrix operations>>c = A*b>>AA = A^2>>bb = b.^2>>x = A\b

Basic Operators:+ - * / \ ^ ’.+ .- .* ./ .\ .^ .’

4University of Pennsylvania GRASP

MEAM 535

Peng Song

M-file Programming

n Use Matlab Editor create a program and save it as a m-file with a file-extension [.m]

n Simply enter the file name of the m-file (without the file extension) in the command window, the m-file will get executed.

n Example: use Matlab Editor to create a file called test.m. In test.m, enter the following linesA = [3 2; 2 4]; b=[3; 2];c = A*b; A = A^2; b = b.^2;x = A\b

Then in the command window, enter >>test;

Page 3: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

3

5University of Pennsylvania GRASP

MEAM 535

Peng Song

Function

n A function is a special m-file. n A general syntax of a function:function [outData1, outData2] = myfunction(inData1, inData2, inData3)Example: function [c,x] = test_function(A,b)

n Workspace variables are not directly accessible to functions. They are normally passed to functions as arguments.

n All variable defined within a function are local variables. These variables will be erased after the execution of the function.

6University of Pennsylvania GRASP

MEAM 535

Peng Song

Visualization

2-Dplot Plot 2-D data with linear scales for both axesloglog Plot with logarithmic scales for both axes

3-D n Lines

Plot3 Plot 3-D data with linear scales for all axescontour Plot contour linesquiver Plot vector fields

n Surfacesmesh, surf: surface plotmeshc, surfc: surface plot with contour plot beneath itsurfl: surface plot illuminated from specified directionsurface: low-level function for creating surface graphics objects

Page 4: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

4

7University of Pennsylvania GRASP

MEAM 535

Peng Song

Examples

Line plots t = 0:pi/50:10*pi;plot3(sin(t),cos(t),t)axis square; grid on

Surface plots[X,Y] = meshgrid(-8:.5:8);R = sqrt(X.^2 + Y.^2) + eps;Z = sin(R)./R;mesh(Z)

8University of Pennsylvania GRASP

MEAM 535

Peng Song

More Examplescontour plots [X,Y,Z] = peaks;contour(X,Y,Z,20);

Vector fieldsn = –2.0:.2:2.0;[X,Y,Z] = peaks(n);contour(X,Y,Z,10);[U,V] = gradient(Z,.2);hold onquiver(X,Y,U,V)

Page 5: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

5

9University of Pennsylvania GRASP

MEAM 535

Peng Song

n Help on the toolbarn Command line>>help plot>>helpwin plot>>doc plot

n Demos>>demo

n Tutorial from mathworks: http://www.mathworks.com/products/education/student_version/tutorials/launchpad.shtml

Help

10University of Pennsylvania GRASP

MEAM 535

Peng Song

Numerical Integration of ODEs

n Initial value problem: Given the initial state at y0=y(x0), to compute the whole trajectory y(x)

y) f(xdxdy , =

y

xExplicit Euler Solution

y ' = 1 - 2y , y (0) = 0

Page 6: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

6

11University of Pennsylvania GRASP

MEAM 535

Peng Song

n Explicit: evaluate derivative using values at the beginning of the time step

n Not very accurate (global accuracy O(h)) & requires small time steps for stability

n Implicit: Evaluate derivative using values at the end of the time step

n May require iteration since the answer depends upon what is calculated at the end.

n Still not very accurate (global accuracy O(h)). n Unconditionally stable for all time step sizes (why?).

Euler’s method

)(),( 21 hOyxfhyy iiii +⋅+=+

)(),( 2111 hOyxfhyy iiii +⋅+= +++

12University of Pennsylvania GRASP

MEAM 535

Peng Song

Truncation errors

x

y

o xi xi+1

yi

yi+1

Local truncation error

xixi+1 xi+2

yi

yi+1

Global truncation error

x

y

o

Page 7: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

7

13University of Pennsylvania GRASP

MEAM 535

Peng Song

Stability

n A numerical method is stable if errors occurring at one stage of the process do not tend to be magnified at later stages.

n A numerical method is unstable if errors occurring at one stage of the process tend to be magnified at later stages.

n In general, the stability of a numerical scheme depends on the step size. Usually, large step sizes lead to unstable solutions.

n Implicit methods are in general more stable than explicit methods.

14University of Pennsylvania GRASP

MEAM 535

Peng Song

Stability Characteristics of Euler Methods

n Model:

n Error:

n Explicit Euler Method is conditionally stable:

n Implicit Euler Method is unconditionally stable:

xexyyy λλ −−=⇒=−= 1)(1)0(;dxdy

λ εε

−=dxd

)1()(1 hh iiii λελ εεε −=−+=+ λλ

εε 2

0111 ≤≤⇒≤−=+ hhi

i

hh i

iiii λε

ελ εεε+

=⇒−+= +++ 1)( 111

011

11 ≥≤+

=+ hallforsatisfiedhi

i

λεε

0>λ

Page 8: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

8

15University of Pennsylvania GRASP

MEAM 535

Peng Song

Second-order Runge-Kutta (midpoint method)

n Second-order accuracy is obtained by using the initial derivative at each step to find a midpoint halfway across the interval, then using the midpoint derivative across the full width of the interval.

n In the above figure, filled dots represent final function values, open dots represent function values that are discarded once their derivatives have been calculated and used.

n A method is called nth order if its error term is O(hn+1)

16University of Pennsylvania GRASP

MEAM 535

Peng Song

Classic 4th-order R-K method

).( of iserror Global ).( of iserror Local

size. step the is where

) ,(

)2

,2

(

)2

,2

(

),(

)22(6

34

1

34

23

12

1

43211

hOhO

xxh

hkyhxfk

kh

yh

xfk

kh

yh

xfk

yxfk

kkkkh

yy

ii

ii

ii

ii

ii

ii

−=

++=

++=

++=

=

++++=

+

+

Page 9: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

9

17University of Pennsylvania GRASP

MEAM 535

Peng Song

Runge-Kutta Methods

n c, a, and w are numerical coefficients chosen to satisfy certain conditions.

n is the number of terms. For 4th order R-K method, ? 4=?

++=

+=+

∑−

=

=

1

1,

1

)(,

)()(

i

jjjiii

iii

kaxyhcxhfk

kwxyhxyν

18University of Pennsylvania GRASP

MEAM 535

Peng Song

Runge-Kutta Methods

1

2

3 4,5

6

Page 10: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

10

19University of Pennsylvania GRASP

MEAM 535

Peng Song

Runge-Kutta Methods

1. Make a first (tentative) step with the Euler method

2. Evaluate slope at intermediate point.

3. Use the adjusted slope and make a second (also tentative) step from the initial point.

4. Evaluate function at additional points and use this information to further adjust the slope to be used at the start

5. Evaluate function at as may other points as required and make further adjustments to the slope to be used at the start.

6. Combine all the estimates to make the actual step.

20University of Pennsylvania GRASP

MEAM 535

Peng Song

Multi-step Methods

n Runge-Kutta-methods are one step methods, only the current state is used to calculate the next state.

n Multi-step methods calculate the next value based on a time series of values, that might be from the past, or from the future.n Explicit (b0 = 0) & implicit methods.n # of the previous steps.

n Adams-Bashforth Method (explicit b0 = 0)n Admas-Moulton Method (implicit)n Predictor-Corrector Methods

] ...),( ),( ),([..

1121

1101211

+++⋅+++=

−−

++−+

iiii

iiiii

yxfbyxfbyxfbhyayay

Page 11: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

11

21University of Pennsylvania GRASP

MEAM 535

Peng Song

Adams-Bashforth (A-B) Methods (Explicit)

).( of iserror local The

method. step-one using calculted are &

known, is while beginning, theAt

.12/5 & 12/16 ,12/23 ,0 ,0 ,1

.1 ........, 3, 2,for

] ),(5),(16),(23[12

4

21

0

321

021

22111

hO

yy

y

bbbbaa

-ni

yxfyxfyxfh

yy iiiiiiii

=−=====

=

+−+= −−−−+

22University of Pennsylvania GRASP

MEAM 535

Peng Song

Adams-Moulton (A-M) Methods (Implicit)

).( iserror local The method.

step - one using calculted is known, is

.12/1 and ,12/8 ,12/5 ,0 ,1.1 ........, 3, 2, ,1for

] ),(),(8 ),(5[12

4

10

21

021

11111

hO

yy

bbbaa

n-i

yxfyxfyxfhyy iiiiiiii

−=====

=

−++= −−+++

Page 12: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

12

23University of Pennsylvania GRASP

MEAM 535

Peng Song

Predictor-Corrector Methods

Adams 3rd-order Predictor-Corrector Methods

n Predictor: using 3rd-order A-B three-step method to predict

n Corrector: using 3rd-order A-M two-step method to compute

] ),(),(8 ),(5[12 11111 −−+++ −++= iiii

Piii

Ci yxfyxfyxf

hyy

] ),(5),(16),(23[12

.1 ........, 3, 2,for

22111 −−−−+ +−+=

=

iiiiiiiPi yxfyxfyxf

hyy

-ni

Piy 1+

Ciy 1+

24University of Pennsylvania GRASP

MEAM 535

Peng Song

Stiff ODEs

n Example

Page 13: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

13

25University of Pennsylvania GRASP

MEAM 535

Peng Song

Stiff ODEs

n A practical example

26University of Pennsylvania GRASP

MEAM 535

Peng Song

Stiff ODEs

n Stiff systems are characterized by some system components which combine very fast and very slow behavior.

n Requires efficient step size control that adapt the step size dynamically, as only in certain phases they require very small step sizes.

n Implicit method is the cure!n Nonlinear systems: solving implicit models by linearization (semi-

implicit methods)n Rosenbrock – generalizations of RK methodn Bader-Deuflhard – semi-implicit methodn Predictor-corrector methods – descendants of Gear’s backwoard

differentiation method

Page 14: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

14

27University of Pennsylvania GRASP

MEAM 535

Peng Song

Higher Order ODEs

n Higher order ODEs can be turned into systems of 1st

order ODEs:n For example:

)sin(22

2

3

3

xcydxdy

bdx

yda

dxyd

=+++

2

2

321

123

3

2

3

2

1

)sin(2

dxyd

ydxdy

yyywhere

cybyayxy

y

yy

y

dxd

===

−−−=

28University of Pennsylvania GRASP

MEAM 535

Peng Song

Mass-Spring-Damper System

)( tfkxxcxm =++ &&&

f(t)kx

xc &

Free-body diagram

mf(t)

mtfy

mCy

mKyxy

yyxy

)(2122

211

+−−==

==

&&

&

+

−−

=m

tfm

Cm

Kyy )(

010&

Page 15: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

15

29University of Pennsylvania GRASP

MEAM 535

Peng Song

Dynamic Simulation Example

Given y0=(x0, 0), solve for y(t) for t=[0, T].

ODE solver

(ode45)

Initial conditions

differential equations

(diff_msd.m)

output (demo_msd.m)

30University of Pennsylvania GRASP

MEAM 535

Peng Song

Simulation Results

0 1 2 3 4 5 6 7 8 9 10-0.5

0

0.5m= 1.0 kg C = 1.0 N*sec/m K = 100.0 N/m

Dis

plac

emen

t

0 1 2 3 4 5 6 7 8 9 10-6

-4

-2

0

2

4

Time (s)

Vel

ocity

Page 16: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

16

31University of Pennsylvania GRASP

MEAM 535

Peng Song

Matlab Solvers for Nonstiff Problemsn ode45

n Explicit Runge-Kutta (4,5) formulan One-step solvern Best function to apply as a "first try" for most problems

n ode23n Explicit Runge-Kutta (2,3)n One-step solvern May be more efficient than ode45 at crude tolerances and in

the presence of mild stiffness.

n ode113n Variable order Adams-Bashforth-Moulton PECE solvern Multistep solver n It may be more efficient than ode45 at stringent tolerances

and when the ODE function is particularly expensive to evaluate.

32University of Pennsylvania GRASP

MEAM 535

Peng Song

Matlab Solvers for Stiff Problemsn ode15s

n Variable-order solver based on the numerical differentiation formulas (NDFs). Optionally it uses the backward differentiation formulas (BDFs).

n Multistep solver. n If you suspect that a problem is stiff or if ode45 failed or was

very inefficient, try ode15s first.

n ode23sn Based on a modified Rosenbrock formula of order 2 n One-step solvern May be more efficient than ode15s at crude tolerances

n ode23tn An implementation of the trapezoidal rule using a "free"

interpolantn Use this solver if the problem is only moderately stiff and you

need a solution without numerical damping

Page 17: Matlab, Numerical Integration, and Simulationmeam535/fall03/slides/numerics... · Matlab, Numerical Integration, and Simulation n Matlab tutorial n Basic programming skills ... n

17

33University of Pennsylvania GRASP

MEAM 535

Peng Song

References

n Numerical Initial Value Problems in Ordinary Differential Equations, Gear, C.W., Englewood Cliffs, NJ: Prentice-Hall,1971.

n Numerical Recipes in C : The Art of Scientific ComputingWilliam H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling, Cambridge University Press, 1992. (online at http://www.library.cornell.edu/nr/bookcpdf.html)