An Introduction To MATLAB: Based On MATLAB Tutorial by Math Works
An Introduction To MATLAB: Based On MATLAB Tutorial by Math Works
RIJAS B M
About MATLAB
• MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment.
• A proprietary programming language developed by Math Works
MATLAB allows:
• Matrix manipulations
• Plotting of functions and data
• Implementation of algorithms
• Creation of user interfaces
• interfacing with programs written in other languages(C, C++, C#, Java, Fortran and
Python)
2 MATLAB Basics
• To create an array with four elements in a single row, separate the elements with
either a comma (,) or a space.
a = [1 2 3 4]
• To create a matrix that has multiple rows, separate the rows with semicolons.
a = [1 2 3; 4 5 6; 7 8 10]
• Another way to create a matrix is to use a function, such as ones, zeros, or rand. For
Example, create a 5-by-1 column vector of zeros.
z = zeros (5, 1)
• MATLAB allows you to process all of the values in a matrix using a single arithmetic
operator or function.
Eg: a + 10
sin(a)
b= [1 2 3; 4 5 6; 7 8 10]
c= [1 2 3; 4 5 6; 7 8 10]
p = b.*c
• Inverse of a matrix can be obtained using the function ‘inv’ inv(b)
• The matrix operators for multiplication, division, and power each have a
corresponding array operator that operates element-wise. For example, raise each
element of b to the third power:
• b.^3
• Concatenation: It is the process of joining arrays to make larger ones. The pair of
square brackets [ ] is the concatenation operator.
• A = [b,c]
• Concatenating arrays next to one another using commas is called horizontal
concatenation. Each array must have the same number of rows. Similarly, when the
arrays have the same number of columns, you can concatenate vertically using
semicolons.
A = [b; c]
4 Workspace variables
• The workspace contains variables that you create within or import into MATLAB
from data files or other programs
• Workspace variables do not persist after you exit MATLAB. Save your data for later
use with the save command,
Save ‘ myfile.mat’
• Saving preserves the workspace in your current working folder in a compressed file
with a.mat extension, called a MAT-file.
• To clear all the variables from the workspace, use the clear command.
• When you are working with text, enclose sequences of characters in single quotes.
You can assign text to a variable.
• If the text includes a single quote, use two single quotes within the definition.
• myText and otherText are arrays, like all MATLAB variables. Their class or data
type is char, which is short for character.
• You can concatenate character arrays with square brackets, just as you concatenate
numeric arrays.
longText = [myText,' - ',otherText]
f = 71;
c = (f-32)/1.8;
6 Functions
A = [1 3 5];
max(A)
B = [10 6 4];
max(A,B)
maxA = max(A)
• When there are multiple output arguments, enclose them in square brackets:
[maxA,location] = max(A)
disp('hello world')
• To call a function that does not require any inputs and does not return any outputs,
type only the function name: clc
• The simplest type of MATLAB program is called a script. A script is a file with a .m
extension that contains multiple sequential lines of MATLAB commands and function
calls. You can run a script by typing its name at the command line.
• This opens a blank file named name.m. Enter the following code:
• Whenever you write code, it is a good practice to add comments that describe the
code.
Comments allow others to understand your code, and can refresh your memory when you
return to it later. Add comments using the percent (%) symbol.
• Save the file in the current folder. To run the script, type its name at the command
line:
• You can also run scripts from the Editor by pressing the Run button.
• You can loop over sections of code and conditionally execute sections using the
keywords
count =0;
for i=1:20
r = randi(10);
disp(r);
if (r<=5)
count = count+1;
end
end
final_count = count;
disp(final_count);
While statement
• An expression is true when its result is nonempty and contains only nonzero
elements
(logical or real numeric). Otherwise, the expression is false.
n = 10;
f = n;
while n > 1
n = n-1;
f = f*n;
end
Switch statement
switch n
case -1
disp('negative one')
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end
Problem
The ICARE company has three plants located throughout a state with production capacity 20,25,15
gallons. Each day the firm must furnish its four retail shops R1,R2 ,R3 &R4 with at least 10,15,15 and
20 gallons respectively. The transportation costs (in RS.) are given below The economic problem is to
distribute the available product to different retail shops in such a way so that the total transportation
cost is minimum?
R1 R2 R3 R4 Supply
R1 10 0 20 11 20
R2 12 7 9 20 25
R3 0 14 16 18 15
Demand 10 15 15 20
Using
a) NWC method
b) Matrix Minimum method
c) VAM method
MATLAB CODE (NWC)