numerical methods in matlabmucha/scieng/lecture8.pdf · numerical analysis is a branch of...

33
Numerical methods in MATLAB Mariusz Janiak p. 331 C-3, 71 320 26 44 c 2015 Mariusz Janiak All Rights Reserved

Upload: others

Post on 22-Mar-2020

17 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Numerical methods in MATLAB

Mariusz Janiakp. 331 C-3, 71 320 26 44

c© 2015 Mariusz JaniakAll Rights Reserved

Page 2: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Contents

1 Introduction

2 Ordinary Differential Equations

3 Optimization

Page 3: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Introduction

Using numeric approximations to solve continuous problems

Numerical analysis is a branch of mathematics that solves continuousproblems using numeric approximation. It involves designing methodsthat give approximate but accurate numeric solutions, which is usefulin cases where the exact solution is impossible or prohibitivelyexpensive to calculate. Numerical analysis also involvescharacterizing the convergence, accuracy, stability, andcomputational complexity of these methods.a

aThe MathWorks, Inc.

Page 4: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Introduction

Numerical methods in Matlab

Interpolation, extrapolation, and regression

Differentiation and integration

Linear systems of equations

Eigenvalues and singular values

Ordinary differential equations (ODEs)

Partial differential equations (PDEs)

Optimization

. . .

Page 5: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Introduction

Numerical Integration and Differential Equations

Ordinary Differential Equations – ordinary differential equationinitial value problem solvers

Boundary Value Problems – boundary value problem solvers forordinary differential equations

Delay Differential Equations – delay differential equation initialvalue problem solvers

Partial Differential Equations – 1-D Parabolic-elliptic PDEs,initial-boundary value problem solver

Numerical Integration and Differentiation – quadratures, doubleand triple integrals, and multidimensional derivatives

Page 6: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

An ordinary differential equation (ODE) contains one or morederivatives of a dependent variable y with respect to a singleindependent variable t, usually referred to as time. The notationused here for representing derivatives of y with respect to t is y ′

for a first derivative, y ′′ for a second derivative, and so on. Theorder of the ODE is equal to the highest-order derivative of ythat appears in the equation.

Exampley ′′ = 2y

Page 7: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

In an Initial Value Problem, the ODE is solved by starting froman initial state. Using the initial condition y0 as well as a periodof time over which the answer is to be obtained (t0, tf ), thesolution is obtained iteratively. At each step the solver applies aparticular algorithm to the results of previous steps. At the firstsuch step, the initial condition provides the necessaryinformation that allows the integration to proceed. The finalresult is that the ODE solver returns a vector of time stepst=[t0,t1,t2,...,tf] as well as the corresponding solution ateach step y=[y0,y1,y2,...,yf].

Page 8: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Solvers in Matlab solve these types of first-order ODEs (1)

Explicit ODEs of the form

y ′ = f (t, y)

Linearly implicit ODEs of the form

M(t, y)y ′ = f (t, y),

where M(t, y) is a nonsingular mass matrix. The mass matrixcan be time- or state-dependent, or it can be a constant matrix.Linearly implicit ODEs can always be transformed to an explicitform

y ′ = M−1(t, y)f (t, y)

Page 9: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Solvers in Matlab solve these types of first-order ODEs (2)

If some components of y ′ are missing, then the equations arecalled Differential Algebraic Equations (DAE)

y ′ = f (t, y , z),

0 = g(t, y , z),

and the system of DAEs contains some algebraic variables.Algebraic variables are dependent variables whose derivatives donot appear in the equations. The number of derivatives neededto rewrite a DAE as an ODE is called the differential index

Page 10: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Solvers in Matlab solve these types of first-order ODEs (3)

Fully implicit ODEs of the form

f (t, y , y ′) = 0

Fully implicit ODEs cannot be rewritten in an explicit form, andmight also contain some algebraic variables

Systems of ODEs y ′1y ′2...

y ′n

=

f1(t, y1, y2, . . . , yn)f2(t, y1, y2, . . . , yn)

...fn(t, y1, y2, . . . , yn)

Page 11: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Higher-Order ODEs (1)

The Matlab ODE solvers only solve first-order equations

The higher-order ODEs have to be rewritten as an equivalentsystem of first-order equations using the generic substitutions

y1 = y

y2 = y ′

y3 = y ′′

...

yn = y (n−1)

=⇒

y ′1 = y2

y ′2 = y3

y ′3 = y4...

y ′n = f (t, y1, y2, y3, . . . , yn)

Page 12: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Higher-Order ODEs (2)

Example: y ′′′ − y ′′y + 1 = 0y1 = y

y2 = y ′

y3 = y ′′=⇒

y ′1 = y2

y ′2 = y3

y ′3 = y1y3 − 1

Page 13: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Stiffness

Lack of a precise definition

In general, stiffness occurs when there is a difference in scalingsomewhere in the problem, eg. two solution components thatvary on drastically different time scales

The step size taken by the solver is forced down to anunreasonably small level in comparison to the interval ofintegration, even in a region where the solution curve is smooth.

Nonstiff solvers are unable to solve the problem or are extremelyslow

Stiff solvers reliability and efficiency can be improved bysupplying the Jacobian matrix or its sparsity pattern

Page 14: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Basic Solver Selection

Solver Problem Type Accuracy When to useode45 Nonstiff Medium Should be the first solver you tryode23 Nonstiff Low Can be more efficient than ode45 at problems with crude

tolerances, or in the presence of moderate stiffnessode113 Nonstiff Low to High Can be more efficient than ode45 at problems with stringent

error tolerances, or when the ODE function is expensive toevaluate

ode15s Stiff Low to Medium Use when ode45 fails or is inefficient and you suspect thatthe problem is stiff. Also use ode15s when solving differentialalgebraic equations (DAEs).

ode23s Stiff Low Can be more efficient than ode15s at problems with crudeerror tolerances. It can solve some stiff problems for whichode15s is not effective.

ode23t Stiff Low Use if the problem is only moderately stiff and you needa solution without numerical damping. Can solve differentialalgebraic equations (DAEs)

ode23tb Stiff Low Solver might be more efficient than ode15s at problems withcrude error tolerances

ode15i Fully implicit Low Use for fully implicit problems f (t, y, y′) = 0 and for diffe-rential algebraic equations (DAEs) of index 1.

Page 15: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Solver ode45 (1)

[ t , y ] = ode45 ( odefun , tspan , y0 )[ t , y ] = ode45 ( odefun , tspan , y0 , o p t i o n s )[ t , y , te , ye , i e ] = ode45 ( odefun , tspan , y0 , o p t i o n s )s o l = ode45 ( )

Integrates the system of differential equations y ′ = f (t, y) ontime horizon tspan with initial conditions y0

odefun — Functions to solve, specified as a function handlewhich defines the functions to be integrated. The function dydt= odefun(t,y), for a scalar t and a column vector y, mustreturn a column vector dydt of data type single or double thatcorresponds to f (t, y). odefun must accept both inputarguments, t and y, even if one of the arguments is not used inthe function

Page 16: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Solver ode45 (2)

tspan – Interval of integration, specified as a vector. Atminimum, tspan must be a two element vector [t0 tf]specifying the initial and final times. To obtain solutions atspecific times between t0 and tf, use a longer vector of theform [t0,t1,t2,...,tf]. The elements in tspan must be allincreasing or all decreasing. The solver imposes the initialconditions, y0, at tspan(1), then integrates from tspan(1) totspan(end)

y0 – Initial conditions, specified as a vector. y0 must be thesame length as the vector output of odefun, so that y0contains an initial condition for each equation defined in odefun

Page 17: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Solver ode45 (3)

options – Option structure, specified as a structure array. Usethe odeset function to create or modify the options structure

t – Evaluation points, returned as a column vector

y – Solutions, returned as an array. Each row in y corresponds tothe solution at the value returned in the corresponding row of t.

te – Time of events, returned as a column vector

ye – Solution at time of events (te), returned as an array

ie – Index of vanishing event function, returned as a columnvector

sol – Structure for evaluation, returned as a structure array.Use this structure with the deval function to evaluate thesolution at any point in the interval [t0 tf]

Page 18: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

ODE Event Location (1)

Use event functions to detect when certain events occur duringthe solution of an ODE

Event functions take an expression that user specify, and detectan event when that expression is equal to zero

They can also signal the ODE solver to halt integration whenthey detect an event

Use the ’Events’ option of the odeset function to specify anevent function

Page 19: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

ODE Event Location (2)

The event function must have the general form

[ v a l u e , i s t e r m i n a l , d i r e c t i o n ] = myEventsFcn ( t , y )

The output arguments are vectors whose ith elementcorresponds to the ith eventvalue(i) is a mathematical expression describing the ith event.An event occurs when value(i) is equal to zeroisterminal(i) = 1 if the integration is to terminate when theith event occurs. Otherwise, it is 0.direction(i) = 0 if all zeros are to be located (the default). Avalue of +1 locates only zeros where the event function isincreasing, and -1 locates only zeros where the event function isdecreasing. Specify direction = [] to use the default value of 0for all events.

Page 20: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Unicycle example (1)

X

Y

Z

q

(q ,q )

3

1 2

q̇1 = u1 cos q3,

q̇2 = u1 sin q3,

q̇3 = u2,

t = [0, 2π], q(0) = (0, 0, 0).

Assuming control u1 = 1, u2 = 0.1 sin t, we are looking for solution

q(t) = ϕq0,t(u).

Page 21: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Unicycle example (2)

File unicycle.m

f u n c t i o n Dq = u n i c y c l e ( t , q , u )% u n i c y c l e ( t , q , u ) −− system d e f i n i t i o n%% t −− t ime% q −− s t a t e v e c t o r% u −− c o n t r o l

Dq = z e r o s ( 3 , 1) ;Dq( 1 ) = u ( 1 ) * cos ( q ( 3 ) ) ;Dq( 2 ) = u ( 1 ) * s i n ( q ( 3 ) ) ;Dq( 3 ) = u ( 2 ) ;

Page 22: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Unicycle example (3)

File sym1.m

% Set s y m u l a i t o n p a r a m e t e r sq0 = [ 0 ; 0 ; 0 ] ;t s p a n = [ 0 2* p i ] ;u = [ 1 ; 0 . 1 ] ;% D e f i n e odefun ans s o l v efun = @( t , y ) ( u n i c y c l e ( t , y , [ 1 , 0 . 1* s i n ( t ) ] ) ) ;[ t , q ] = ode45 ( fun , tspan , q0 ) ;

% P l o t r e z u l t sf i g u r e ; ax1 = s u b p l o t ( 1 , 2 , 1 ) ; ax2 = s u b p l o t ( 1 , 2 , 2 ) ;p l o t ( ax1 , q ( : , 1 ) , q ( : , 2 ) )x l a b e l ( ' q 1 ' ) ; y l a b e l ( ' q 2 ' ) ;t i t l e ( ax1 , ' Path ' )p l o t ( ax2 , t , q )x l a b e l ( ' t ' ) ; y l a b e l ( 'q ' ) ;t i t l e ( ax2 , ' T r a j e c t o r y ' )l e g e n d ( ' q 1 ' , ' q 2 ' , ' q 3 ' , 2 )

Page 23: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Ordinary Differential Equations

Unicycle example (4)

0 2 4 6 80

0.1

0.2

0.3

0.4

0.5

0.6

0.7

q1

q2

Path

0 2 4 6 80

1

2

3

4

5

6

7

t

q

Trajectory

q

1

q2

q3

Page 24: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

Optimization techniques are used to find a set of design parameters,x = x1, x2, ..., xn, that can in some way be defined as optimal. In asimple case this might be the minimization or maximization of somesystem characteristic that is dependent on x . In a more advancedformulation the objective function, f (x), to be minimized ormaximized, might be subject to constraints in the form of equalityconstraints, Gi (x) = 0, (i = 1, ...,me); inequality constraints,Gi (x) ¬ 0, (i = me + 1, ...,m); and/or parameter bounds, xl , xu.1

1MathWorks Inc.

Page 25: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

A General Problem (GP) description is stated as

minx

f (x)

subject to

Gi (x) = 0 i = 1, . . . ,meGi (x) ¬ 0 i = me + 1, . . . ,m

where x is the vector of length n design parameters, f (x) is theobjective function, which returns a scalar value, and the vectorfunction G (x) returns a vector of length m containing the values ofthe equality and inequality constraints evaluated at x .

Page 26: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

Optimization ToolboxSolvers

linear programming (LP)mixed-integer linear programming (MILP)quadratic programming (QP)nonlinear programming (NLP)constrained linear least squaresnonlinear least squaresnonlinear equations

optimtool – Select solver and optimization options, runproblems

Page 27: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

Optimization Decision Table

Constraint Type Objective TypeLinear Quadratic Last Squares Smooth Nonlinear Nonsmooth

Nonen/a (f = const,or min = −∞) quadprog

mldivide,lsqcurvefit,lsqnonlin

fminsearch,fminunc

fminsearch, *

Bound linprog quadprog

lsqcurvefit,lsqlin,lsqnonlin,lsqnonneg

fminbnd,fmincon, fseminf fminbnd, *

Linear linprog quadprog lsqlin fmincon, fseminf *General Smooth fmincon fmincon fmincon fmincon, fseminf *Discrete, withBound or Linear

intlinprog * * * *

* means relevant solvers are found in Global Optimization Toolbox

Page 28: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

fmincon – Nonlinear programming solver (1)

Find minimum of constrained nonlinear multivariable function

Finds the minimum of a problem specified by

minx

f (x)

subject to

c(x) ¬ 0

ceq(x) = 0

A · x ¬ b

Aeq · x = beqlb ¬ x ¬ ub

Page 29: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

fmincon – Nonlinear programming solver (2)

x = fmincon ( fun , x0 , A, b )x = fmincon ( fun , x0 , A, b , Aeq , beq )x = fmincon ( fun , x0 , A, b , Aeq , beq , lb , ub )x = fmincon ( fun , x0 , A, b , Aeq , beq , lb , ub , n o n l c o n )x = fmincon ( fun , x0 , A, b , Aeq , beq , lb , ub , nonlcon , o p t i o n s )x = fmincon ( problem )[ x , f v a l ] = fmincon ( )[ x , f v a l , e x i t f l a g , output ] = fmincon ( )[ x , f v a l , e x i t f l a g , output , lambda , grad , h e s s i a n ] = fmincon ( )

fun – Function to minimize

f u n c t i o n f = myfun ( x )f = ... % Compute f u n c t i o n v a l u e a t x

x0 – Initial point, specified as a real vector or real array

Page 30: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

fmincon – Nonlinear programming solver (3)

A – Linear inequality constraints, matrix

b – Linear inequality constraints, vector

Aeq – Linear quality constraints, matrix

beq – Linear equality constraints, vector

lb – Lower bounds, specified as a real vector or real array

ub – Upper bounds, specified as a real vector or real array

nonlcon – Nonlinear constraints, a function handle or name

f u n c t i o n [ c , ceq ] = mycon ( x )c = ... % Compute n o n l i n e a r i n e q u a l i t i e s a t x .ceq = ... % Compute n o n l i n e a r e q u a l i t i e s a t x .

. . .

Page 31: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

Example (1)Find the point where Rosenbrock’s function

f (x) = 100 ∗ (x2 − x1)2 + (1− x1)

2

is minimized within a circle centered at [13 ,

13 ] with radius 1

3

c(x) =(

x1 −13

)2

+

(x2 −

13

)2

−(

13

)2

subject to bound constraints 0 ¬ x1 ¬ 0.5, 0.2 ¬ x2 ¬ 0.8.

File circlecon.m

f u n c t i o n [ c , ceq ] = c i r c l e c o n ( x )c = ( x ( 1 )−1/3)ˆ2 + ( x ( 2 )−1/3)ˆ2 − ( 1 / 3 ) ˆ 2 ;ceq = [ ] ;

Page 32: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

Optimization

Example (2)File sym2.m% D e f i n e problemfun = @(x ) 100*( x ( 2 )−x ( 1 ) ˆ2) ˆ2 + (1−x ( 1 ) ) ˆ 2 ;A = [ ] ;b = [ ] ;Aeq = [ ] ;beq = [ ] ;l b = [ 0 , 0 . 2 ] ;ub = [ 0 . 5 , 0 . 8 ] ;x0 = [ 1 / 4 , 1 / 4 ] ;n o n l c o n = @ c i r c l e c o n ;% S o l v ex = fmincon ( fun , x0 , A, b , Aeq , beq , lb , ub , n o n l c o n )

Solutionx =

0.5000 0 .2500

Page 33: Numerical methods in MATLABmucha/SciEng/lecture8.pdf · Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing

The End

Thank you for your kind attention.