MATLAB Examples - Optimization
MATLAB Examples - Optimization
Examples
Optimization
Hans-Petter Halvorsen
Optimization
Optimization is based on finding the minimum of a given criteria
function.
𝑓(𝑥)
𝑑𝑓(𝑥)
=0
𝑑𝑥
Minimum
𝑥
Optimization
• Optimization is important in modelling, control and
simulation applications.
• Optimization is based on finding the minimum of a given
criteria function.
• It is typically used with Model based Control (MPC)
• MATLAB functions:
- fminbnd() - Find minimum of single-variable function
on fixed interval
- fminsearch() - this function is similar to fminbnd()
except that it handles functions of many variables
Optimization
Example:
We want to find for what value of x the function has its minimum value:
clear
clc
x = -20:0.1:20;
y = 2.*x.^2 + 20.*x - 22;
plot(x,y)
grid
i=1;
while ( y(i) > y(i+1) )
i = i + 1;
end
x(i) (-5,72)
y(i)
The minimum of the function
Example: Optimization
y = y = mysimplefunc(x_min)
-72
Note! if we have more than 1 variable, we have to
We got the same results as previous slide use e.g., the fminsearch() function
Optimization
Example: We have that:
𝑑𝑦
= 4𝑥 + 20
𝑑𝑥
Minimum when:
𝑑𝑦
=0
𝑑𝑥
This gives:
4𝑥 + 20 = 0
(-5,72)
𝑥 = −5
The minimum of the function
Optimization
Given the following function:
𝑓 𝑥 = 𝑥 3 − 4𝑥
We will:
• Plot the function
• Find the minimum for this function
function f = myefunction(x)
f = x.^3 - 4*x;
clear, clc
x = -3:0.1:3;
f = mysimplefunc(x);
plot(x, f)
2
𝑓 𝑥, 𝑦 = 1 − 𝑥 + 100(𝑦 − 𝑥 2 )2
We will:
→ Plot the function
→ Find the minimum for this function https://en.wikipedia.org/wiki/Rosenbrock_function
We plot the Banana function:
clear,clc
f = (1-x).^2 + 100.*(y-x.^2).^2;
figure(1)
surf(x,y,f)
figure(2)
mesh(x,y,f)
figure(3)
surfl(x,y,f)
shading interp;
colormap(hot);
function f = bananafunc(x)
f = (1-x(1)).^2 + 100.*(x(2)-x(1).^2).^2;
x = 1.0000 1.0000
fval = 8.1777e-10
Which is correct
Hans-Petter Halvorsen, M.Sc.
E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/