Operations Research I: Matlab Lab 03
Operations Research I: Matlab Lab 03
Research I
MATLAB Lab 03
INS TRUCTOR: DR. OSWALDO AGUIRRE
COURS E : IE 3390
S E MESTER: FALL 2019
Functions
Functions in MATLAB: sub program
Examples
Max(A)
Sum(A)
Rand()
Functions
A sub program that executes a set of commands
The functions has two main parameters inputs and outputs
You can create your own function
function C=addtwonumbers(a,b)
C=a+b
end
EXAMPLE # 02
Write a MATLAB function that add calculates the factorial of a number
function F=fcatorialofnumber(n)
F=1;
for i=1:n
F=F*i;
end
EXAMPLE # 03
Write a MATLAB function that calculate the smallest value of a vetor and its position in the
vector
1 2 3 5 6 7
Strategy
Repeat from the element 1 to the element n-1
5 2 7 3 1 6
For each cycle find the smallest value from the current position to the end
of the vector and repeat for all the cycles and locate the value in the
current position
Process
Cycle 1 (i =1)
5 2 7 3 1 6
Find the smallest value from the current position to the end
Smallest value=1
Swap values
1 2 7 3 5 6
Process
Cycle 1 (i =2)
1 2 7 3 5 6
Find the smallest value from the current position to the end
Smallest value=2
Swap values
1 2 7 3 5 6
Process
Cycle 1 (i =3)
1 2 7 3 5 6
Find the smallest value from the current position to the end
Smallest value=3
Swap values
1 2 3 7 5 6
Process
Cycle 1 (i =4)
1 2 3 7 5 6
Find the smallest value from the current position to the end
Smallest value=5
Swap values
1 2 3 5 7 6
Process
Cycle 1 (i =5)
1 2 3 5 7 6
Find the smallest value from the current position to the end
Smallest value=5
Swap values
1 2 3 5 6 7
Exercise 02
Fibonacci series
Write a MATLAB function
Given an index n calculate the Fibonacci number 𝐹𝑛
Example ∶
n=3
Exercise 03
Write a MATLAB to solve the simplex tableau method
Example:
Use this information as an
input