Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
207 views

MATLAB Examples - Optimization

This document discusses optimization techniques in MATLAB. It provides examples of using functions like fminbnd, fminsearch, and fminsearch to find the minimum of single-variable, multi-variable, and Rosenbrock's banana functions. The key aspects of optimization covered are that it involves finding the minimum of a criteria function by setting its derivative equal to zero, and MATLAB provides tools for numerical optimization of both single and multi-variable functions.

Uploaded by

ClayssaNel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views

MATLAB Examples - Optimization

This document discusses optimization techniques in MATLAB. It provides examples of using functions like fminbnd, fminsearch, and fminsearch to find the minimum of single-variable, multi-variable, and Rosenbrock's banana functions. The key aspects of optimization covered are that it involves finding the minimum of a criteria function by setting its derivative equal to zero, and MATLAB provides tools for numerical optimization of both single and multi-variable functions.

Uploaded by

ClayssaNel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MATLAB

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

function f = mysimplefunc(x) clear


clc
close all
f = 2*x.^2 + 20.*x -22;
x = -20:1:20;
f = mysimplefunc(x);
x_min = plot(x, f)
grid
-5
x_min = fminbnd(@mysimplefunc, -20, 20)

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)

[xmin,fmin] = fminbnd(@myfunction, -3, 3)


This gives:
xmin =
/0 2 7 1.1547
= 3𝑥 − 4 = 0 → 𝑥456 = ≈ 1.1547 fmin =
/1 8
-3.0792
Optimization - Rosenbrock's Banana Function
Rosenbrock’s banana function is a famous
Given the following function: test case for optimization software

2
𝑓 𝑥, 𝑦 = 1 − 𝑥 + 100(𝑦 − 𝑥 2 )2

This function is known as


Rosenbrock's banana function.

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

[x,y] = meshgrid(-2:0.1:2, -1:0.1:3);

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,fval] = fminsearch(@bananafunc, [-1.2;1])

From MATLAB we get:

x = 1.0000 1.0000

fval = 8.1777e-10

Which is correct
Hans-Petter Halvorsen, M.Sc.

University College of Southeast Norway


www.usn.no

E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/

You might also like