MATLAB1
1This script is based on a MATLAB course of Alexey Cherepnev and Tobias
Nagel at University of Mainz.
Outline
I Root �nding and Optimization
I Integration and Interpolation
I Random numbers
I Ordinary di�erential equations
Root �nding - Introduction
I Determination of roots of a function common problem, e.g.�nding local extrema
I Some problems can be referred as root �nding, e.g.f (x) = a ⇒ g(x) = 0, g(x) = f (x)− a
I In most cases, numerical methods will need an initial guessand will �nd the nearest root
I How to �nd an initial guess?I Graphical guess, using a plot of the functionI Plot function and select a guess using the mouse (�ginput�)
We need at least a continous function.
roots
Finding roots of polynomials: �roots(c)�c is a vector containing the coe�cients of the polynomial
Example 1
f (x) = x2 − x − 6» c =[1 -1 -6];» roots(c)ans =3-2
Example 2
f (x) = 10x3 − 2x2 + 3» c = [10 -2 0 3];» roots(c)ans =0.4045 + 0.5736i0.4045 - 0.5736i-0.6090
fzero
Let f be a continuous function of one variable.
I fzero(<fun>, x0) determines a root of the function<fun> near x0 (which can be either a scalar or a vector oflength two).
I According to x0, fzero tries to �nd a starting interval wherethe given function has a change in sign. Then this functionuses a combination of bisection and other algorithms
I Many options (input as well as output) ⇒ �help fzero�
Example:
We want to use our function eval_poly3 together with fzero
» a = [1 2 3 0];» fzero(@(x)eval_poly3(a,x),...0.5)ans =-6.9198e-020
Here we need an anonymousfunction handle @(x)since wehave an additional inputargument (vector a).
fsolve
I We already discussed fsolve
I Solves F (x) = 0 (also multivariate functions possible)
Example
F (X ) =
(x2 + 1
25y2 − 1
4x3 + 5x2 + 6x − y − 1
)= 0, X =
(xy
)
Solving systems of nonlinear equations
FunNonLin.m
function F = FunNonLin(X)%function-file%Input: X - 2-dim%col-vector%Output: F - 2-dim%vector, function valuesx = X(1); y = X(2);F = [xˆ2+(yˆ2)/25-1;4*xˆ3+5*xˆ2+6*x-y-1];
Command window» [x, fval] = fsolve(’FunNonLin’, [0.6 4])Optimization terminated: first-orderoptimality is less than options.TolFun.x =
0.5334 4.2294fval =1.0e-008 *
0.0125 0.1381
» fsolve(’FunNonLin’, [-0.6 -4])Optimization terminated: first-orderoptimality is less than options.TolFun.ans =
-0.6401 -3.8413
Optimization with constraints
fmincon can handle
min f (x), s.t.
A · x ≤ bAeq · x = beqc(x) ≤ 0ceq(x) = 0lb ≤ x ≤ ub
x , b, beq, lb and ub are vectors, A,Aeq are matrices, c(x), ceq(x) arefunctions returning vectors and f (x) is a function that returns ascalar.
I x = fmincon(’fun’, x0, A, b)
I x = fmincon(’fun’, x0, A, b, Aeq, beq)
I x = fmincon(’fun’, x0, A, b, Aeq, beq, lb, ub)
I x = fmincon(’fun’, x0, A, b, Aeq, beq, lb, ub, ’nonlcon’)
⇒ If you have, e.g. only Aeq, beq you have to use the second optionand set A = [], b = [].
fmincon cont.
fun and nonlcon are functions, so we need a tool to pass afunction to another function:⇒ �function handle�:
I Using the �@�: �fh=@functionName�
I Within a function-call: � ' �
Option 1: x = fmincon(@fun, x0, A, b)Option 2: x = fmincon(’fun’, x0, A, b)
In both cases fmincon will use the function �le fun.m.
Example: Utility maximization
We would like to solve
max u(x , y , z)s.t.0 ≤ x + 2y + 2z ≤ 72.
In order to use fmincon, we look at
min−u(x , y , z)s.t.{−x − 2y − 2z ≤ 0x + 2y + 2z ≤ 72
}⇔ Ax ≤ b
Command window» A = [-1 -2 -2;1 2 2 ];» b = [0;72];» x0 = [10; 10; 10];» [x, fval]=fmincon(’fun’, x0,A, b)x =24.0000 12.0000 12.0000
fval =-3.4560e+003
fun.mfunction out = fun(x)out = -(x(1)*x(2)*x(3));
Integration
I We want to compute the integral of a function f , i.e.I =
∫ ba f (x)dx .
I We cannot form the primitive of a function (this would beanalytical, not numerical).
I Focus on �classical approaches� based on quadrature formulas:
Quadrature formula
A quadrature formula is de�ned as
I [f ] =
∫ b
af (x)dx =
m∑i=1
wi f (xi ) =: Q[f ]
with weights wi and knots xi .
Integration cont.
In MATLAB there are e.g.
I �quad(<fun>, a, b)�
I �quadgk(<fun>, a, b)�
Disadvantage:
I Functional form is needed, but often only data points areknown.⇒ Not a problem if implementation of Trapezium rule orSimpson's rule
Curves of best �t
We want to �nd the best �tting curve for given data points.⇒ Need measure of �best�
Least Squares method: The least squares method takes itsoptimum, when the sum of squared residuals is a at minimum. Aresidual is de�ned as the di�erence of the predicted curve and thevalue of the data at this point.Let (xi , yi ) be data points and f (x ;β) the model function, then f isthe curve of best �t if
f (x ;β) = minf
∑i
r2i := mini
∑i
(f (xi ;β)− yi )2
Line of best �t
As an example, we will determine a straight line, which �ts best agiven data set.Let f (x ;β) := f (x ;m, t) = mx + t be our model function. We haveto choose m, t in order to minimize r :=
∑ir2i . Therefore
di�erentiate:
∂r
∂m=
N∑i=1
2xi (mxi + t − yi ) = 0
∂r
∂t=
N∑i=1
2(mxi + t − yi ) = 0
⇔
mN∑i=1
x2i + tN∑i=1
xi =N∑i=1
xiyi
mN∑i=1
xi + tN =N∑i=1
yi
Line of best �t cont.
I This system of equations can be transferred into matrix formAx = b and solved in MATLAB to get the unique line of best�t.
I Built-in function �polyfit� also does the job:
Command window» x = [0 2 4 8 10];» y = [1 1.5 0 -5 -4.8];» p = polyfit(x,y,1);» p_val = polyval(p,0:10);
Curves of best �t cont.
polyfit can be used for higher order polynomials, too
You can implement a linear �twith 2 points, a quadratic �t with3 points, a cubic �t with 4points...If you choose a polynomial oforder higher than (# datapoints-1), this won't be unique.
Statistical quantities
mean(x) Arithmetic meanmedian(x) Median valuevar(x) Variancestd(x) Standard deviationcov(x,y) Covariance between x and ycorrcoef(x,y) Correlation coe�cient between x and y...
Random numbers
rand(m,n) returns a m× n matrix of random number between 0and 1 (uniformly distributed, i.e. ∼ U[0,1])
Example: A dice
roll.mfunction x = roll()x=ceil(rand*6);
Command window» x =[]; » for i=1:10000x = [roll() x];end» mean(x)ans =3.5331
» hist(x, 1:6);
Random numbers cont.
randn(m,n) generates a m × n matrix of standard normallydistributed (pseudo) random numbers
More general command: random
random(’norm’,a,b,m,n) Normal distributionrandom(’unif’,a,b,m,n) Uniform distributionrandom(’unid’,a,m,n) Discrete uniform distributionrandom(’bino’,a,b,m,n) Binomial distributionrandom(’chi2’,a,m,n) Chisquare distributionrandom(’logn’,a,b,m,n) Lognormal distributionrandom(’exp’,a,m,n) Exponential distributionrandom(’poiss’,a,m,n) Poisson distribution...
Random numbers cont.
I pdf(’name’,X,a,b)I Returns the probability for given matrix X according to
speci�ed distribution 'name' with paramter values a and b
I cdf(’name’,X,a,b)I Returns the density for given matrix X according to speci�ed
distribution 'name' with parameter values a and b
I Distributions (’name’)
norm Normal distributionunif Uniform distributionunid Discrete uniform distributionpoiss Poisson distributionbino Binomial distributionchi2 Chisquare distributionlogn Lognormal distribution...
Random numbers cont.
Why �pseudo� random?
I The generation of random numbers is a di�cult task
I In MATLAB the sequence of numbers generated is determinedby the state of the generator. Unless this �state� is notchanged the sequence will be the same.
⇒ Pseudo-random numbers exhibits statistical randomness but aregenerated due to an deterministic process
Random numbers cont.
In order to make your result replicable for others, you have to makeyour random numbers replicable:
Command window» s=randn(’state’);» r1=randn(2,2);» randn(’state’,s);» r2=randn(2,2);
These commands save the current state of the random numbergenerator, generate a 2× 2 matrix of random numbers, reset thestate and produces exactly the same matrix of random numbers.
Ordinary di�erential equations
Just an overview of the MATLAB built-in functions:There are several functions which solve the initial value problem(using di�erent methods):
I ode23, ode45, ode113
as well as solver for �sti�� equations (ODE for which the Eulermethod is unstable)
I ode15s, ode23s
All functions use the following syntax:[t,y] = odeXXX(’name’, [t0 T], y0)
name name of function-m-�le, de�ning the function (�rst argumenthas to be t, second argument has to be y)
t0,T start and end pointy0 initial value