IB352 Warwick Wk2 - Lecture2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Xuan Vinh Doan

Spring 2023

Applied Optimisation
Methods
Lecture 2
Lecturer

• Dr Xuan Vinh Doan


• Reader, Warwick Business
School
• PhD in Operations Research
from MIT (US): data mining
and optimisation
http://ise.illinois.edu/images/researchareas/data-nalytics.jpg
[email protected],
Room: 0.210 (WBS Building)
• Telephone: 024 765 22475
Overview

• Introduction to Matlab (L2)


• Unconstrained optimisation (L3-L4)
• Optimality conditions
• Numerical algorithms
• Constrained optimisation (L5-L6)
• KKT conditions
• Feasible direction method
Agenda

• Coding in Matlab
• Reading
• Getting Started with Matlab:
https://uk.mathworks.com/help/matlab/getting-started-
with-matlab.html
Integrated Development Environment
Editor
• Script file: sequences of Matlab commands
• Function file: accepts input and returns output
• Text files, extension .m
• Directory needs to be part of Matlab path: Right-click
on directory  Add to path  Selected folders and
sub-folder
• Run a script by either calling its name in the
command window, or clicking the Run button
Functions
• Variables used in function files only exist inside the
function, and only while the function is called
• First line:
function [y1, y2, ..., yn] = functionname(x1, x2,..., xm)
• Example
function y = average(x)
y = sum(x)/numel(x);
end
• return ends the function call at any point
• Single line functions can also be defined as
average = @(x) sum(x)/numel(x);
for Loop

• Syntax
for index = values
statements
end
• Example:
x = 0;
for i = 1 : 10
x = x + 1;
end
while Loop
• Syntax
while expression
statements
end
• The statements inside the while loop will be repeated as long
as the expression is true.
• Example:
s = 0;
i = 1;
while ((s < 10) && (i <= numel(x)))
s = s + x(i);
i = i + 1;
end
if Statements
• Syntax
if expression
statements
elseif expression
statements
else
statements
end
• Example:
if ((x >= minVal) && (x <= maxVal))
disp(’Value within specified range.’);
elseif (x > maxVal)
disp(’Value exceeds maximum value.’);
else
disp(’Value is below minimum value.’);
end
Examples

1) Write a script to add up all odd numbers from 13 to


99
2) Write a function that takes a value and returns a
matrix that looks as follows
Save and Load Data
• save(filename,variablenames): use .mat as binary file
extension
• load(filename): load all saved variables from a .mat file
• Writing data to a text file
x = 0:.1:1; A = [x; exp(x)];
fid = fopen(’filename.txt’,’w’); %open file
fprintf(fid,’%6s %12s\n’,’x’,’exp(x)’); %header
fprintf(fid,’%6.2f %12.8f\n’,A); %print matrix
fclose(fid); %close file
• Formatting: %s string, %6.2f real value with 6 digits, 2
after decimal point, %d integer, \n new line
• Attention: matrix is printed column by column
How to Get Help

• help <command>: Display help text on command in


Command Window
• lookfor <XYZ>: looks for the string XYZ in the first
comment line of the HELP text in all MATLAB files
found on MATLABPATH
• Google is your best friend
Problems

1) Implement the bubble sort algorithm in Matlab given the


following pseudo-code
for i=1:n-1
for j=1:n-i
if a[j] < a[j+1]
swap(a[j],a[j+1]);
end
end
end
What is the output if ?
Problems
2) Write a Matlab function to determine how many real roots
a quadratic equation , , has and
return those real roots.
3) Write three Matlab functions to compute the value, the
gradient, and the Hessian matrix of the following function:
.
4) Use the built-in Matlab function linprog to solve the
following linear optimisation problem with different
objective coefficient vector :
Problems
5) Implement the bisection method in Matlab to find the real root of the cubic equation
in the interval :
i = 1;
a = 0;
b = 1;
while i<= N
c = (a + b)/2;
if f(c) = 0 or (b-a)/2 < eps
return c;
end
if sign(f(c)) = sign(f(a))
a = c;
else
b = c;
end
i = i + 1;
end

You might also like