0% found this document useful (0 votes)
125 views17 pages

Operations Research I: Matlab Lab 03

This document provides examples of MATLAB functions. It discusses how to write functions that take inputs and return outputs. It provides three examples: a function that adds two numbers, a function that calculates the factorial of a number, and a function that finds the smallest value and position in a vector. It then gives two exercises - to write a function to sort a vector from smallest to largest without using the sort function, and to write a function to calculate the Fibonacci number given an index. It also mentions writing a function to implement the simplex tableau method for optimization problems.

Uploaded by

Ahmed Ismail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
125 views17 pages

Operations Research I: Matlab Lab 03

This document provides examples of MATLAB functions. It discusses how to write functions that take inputs and return outputs. It provides three examples: a function that adds two numbers, a function that calculates the factorial of a number, and a function that finds the smallest value and position in a vector. It then gives two exercises - to write a function to sort a vector from smallest to largest without using the sort function, and to write a function to calculate the Fibonacci number given an index. It also mentions writing a function to implement the simplex tableau method for optimization problems.

Uploaded by

Ahmed Ismail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

Operations

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

Name of the function the


same of the name of the file

function [ result ] = function1( number1,number2 )


%function that add number 1 and number 2
result =number1+number2;
end
How to run a function
A function cannot be executes as an script
You need to execute the function in the command windows or in
an script
EXAMPLE # 01
Write a MATLAB function that add 2 values

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

function [svalue, position]=get_small(vector)


% find how many columns are in the vector
n=size(vector,2);
svalue=inf;
for i=1:n
if(vector(1,i)<svalue)
svalue=vector(1,i);
position=i;
end
end
Exercise 01
Write a MATLAB script that sort elements in a vector from smallest to
largest value. Using for loops and if
Do not use the sort function in MATLAB
You can use the smallestvalue function from example 3
Example:
5 2 7 3 1 6
result

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

You might also like