09-Mar-21
MATLAB
• The name MATLAB stands for
MATLAB MATrix LABoratory.
• MATLAB is a high-performance
language for technical computing.
Introduction
Areej Abbas Abubaker 1 Areej Abbas Abubaker 2
Uses Basic Operations and Commands
• Math and computation • The four elementary arithmetic operations in
MATLAB are done by the operators +,-,* and /,
• Algorithm development and ^ stands for power operator:
• Modeling, simulation, and prototyping >> 2+3*4^(1-1/5)
• Data analysis, exploration, and visualization
ans =
• Scientific and engineering graphics
• Application development, including Graphical 11.0943
User Interface building .
>>
Areej Abbas Abubaker 3 Areej Abbas Abubaker 4
• The operator \ is for left division. For example,
try:
• MATLAB easily handles complex and infinite
>> 2\4 numbers:
ans = >> sqrt(-1)
2 ans =
>> 2/4 0.0000 + 1.0000i
ans = >>
0.5000 Areej Abbas Abubaker 5 Areej Abbas Abubaker 6
1
09-Mar-21
• If any expression cannot be evaluated,
• Both (i) and (j) stand for the complex number
MATLAB returns NaN, which stands for Not-a-
−1 unless another value is assigned to them.
Number:
• The variable pi:
>> 0*log(0)
>> pi
ans = ans =
3.1416 NaN
Areej Abbas Abubaker 7 Areej Abbas Abubaker 8
• The equality sign is used to assign values to • If no name is introduced, result of the
variables: expression is saved in a variable named ans:
>> a=2; >> a+b
>> b=3*a
ans =
b=
8
6
Areej Abbas Abubaker 9 Areej Abbas Abubaker 10
• If you do not want to see the result of the
command, put a semicolon at the end of it. >> format short,c
• You can see the value of the variable by simply c=
typing it.
• MATLAB is case sensitive. 23.1407
• >> c=exp(pi) >> format long e,c
c=
c=
2.314069263277927e+01
23.1407 >> format short e,c
>> format long,c
c=
c=
23.140692632779267 2.3141e+01
Areej Abbas Abubaker 11 Areej Abbas Abubaker 12
2
09-Mar-21
• Use the command who to see names of the • Remember that by using the up arrow key you
variables, currently available in the workspace. can see the commands you have entered so
far.
• To see a list of variables together with
information about their size, bytes…, use the
command whos.
• In order to delete a variable from the memory
use the clear a command.
• The clc command clears the command window
and homes the cursor.
Areej Abbas Abubaker 13 Areej Abbas Abubaker 14
• Elements of a matrix can be called or replaced
Vectors, Matrices and Arrays individually:
• MATLAB is designed to make operations on >> m(1,3)
matrices as easy as possible.
• A scalar number is a 1x1 matrix. ans =
• A vector is a 1xn (or nx1) matrix.
• Introducing a matrix is done by equality sign: 3
>> m=[1 2 3;4,5,6] >> m(1,3)=7
m= m=
1 2 3 1 2 7
4 5 6 4 5 6
Areej Abbas Abubaker 15 Areej Abbas Abubaker 16
• Matrices may combine together to >> o=[n,n]
form new matrices:
>> n=[m;m]
o=
n=
1 2 7 1 2 7
1 2 7
4 5 6 4 5 6
4 5 6
1 2 7 1 2 7 1 2 7
4 5 6 4 5 6 4 5 6
Areej Abbas Abubaker 17 Areej Abbas Abubaker 18
3
09-Mar-21
• The transpose of a matrix can be • A very useful syntax in
by putting a single quote after a MATLAB is the colon operator
matrix:
>> m=[m;6,8,9]'
that produces a row vector:
>> v=-1:4
m=
v=
1 4 6
2 5 8
-1 0 1 2 3 4
7 6 9
Areej Abbas Abubaker 19 Areej Abbas Abubaker 20
• The default increment is 1, but the user can
change it if required:
>> w=[-1:0.5:4;8:-1:-2;1:11]; >> w(1,:)
>> w(:,5) ans =
ans = -1.0000 -0.5000 0 0.5000 1.0000
1.5000 2.0000 2.5000 3.0000 3.5000
1 4.0000
4
5
Areej Abbas Abubaker 21 Areej Abbas Abubaker 22
>> w(2:3,4:7) • There are many built-in array construction
functions:
ans = >> ones(2) % matrix of ones
>> ones(2,3) % matrix of ones
5 4 3 2 >> zeros(2,3,2) % array of zeros
4 5 6 7 >> eye(3) % identity matrix
>> w(2,8:end) >> rand(4,2) % matrix of random entries
>> linspace(-1,5,7) % row vector of equally
ans = spaced number.
>> logspace(-1,2,8) % row vector of
1 0 -1 -2 logarithmically equally spaced points.
Areej Abbas Abubaker 23 Areej Abbas Abubaker 24
4
09-Mar-21
• Two useful array functions are size, which gives the Array Arithmetic
size of the array, and length, which gives the
maximum length of the array:
• Multiplying a scalar to an array, multiplies all the
elements by the scalar:
>> size(w) >> a=[1,2,4;2:4;4:0.5:5]
>> b=2*a
ans = • Only two arrays of the same size may be added
or subtracted:
3 11 >> c=ones(3);
>> a-c
>> length(w)
>> a+c
• Adding a scalar to an array results in adding the
ans = scalar to all the elements of the array:
>> a+2
11 Areej Abbas Abubaker 25 Areej Abbas Abubaker 26
• Vector and matrix multiplication requires that
the sizes match:
>> d=ones(3,1)
• Some useful matrix functions are:
>> a*d
>> d*a >> det(a) % determinant of a square matrix.
>> d'*a >> inv(a) % inverse of matrix.
• To perform an operation on an array element by >> rank(a) % rank of matrix.
element, use a (.)
>> a.*c
>> a.^2
>> 1./a
Areej Abbas Abubaker 27 Areej Abbas Abubaker 28
Graphics >> plot(x,y) % plot y against x
• 2D Graphs >> grid % adds grid lines to lines to the current
>> x=linspace(0,2); axes
>> y=x.*exp(x); >> grid % removes the grid lines
>> plot(y) % plot y versus their index
Areej Abbas Abubaker 29 Areej Abbas Abubaker 30
5
09-Mar-21
>> xlabel('x') % adds text below the x-axis
>> ylabel('y') % adds text besides the y-axis
>> title('y=x*exp(-x)') % adds text at the top of the >> gtext('anywhere') % places text with
graph mouse
>> text(1,0.2,'(1,0.2)') % places text at the
specific point
You can use symbols instead of lines. You can
also plot more than one function in a graph:
>> plot(x,y,'.',x,x.*sin(x))
Areej Abbas Abubaker 31 Areej Abbas Abubaker 32
• More than one graph can be shown in different
frames: • Axis limits can be seen and modified using the
>> subplot(2,1,1) axis command:
>> plot(x,x.*cos(x)) >> axis
>> subplot(2,1,2) >> axis([0,1.5,0,1.5])
>> plot(x,x.*sin(x)) • Before continuing, clear the graphic window:
>> clf
• Another easy way to plot a function is:
>> fplot('x*exp(-x)',[0,2])
• The function to be plotted may also be a user-
defined function.
Areej Abbas Abubaker 33 Areej Abbas Abubaker 34
• Other useful two-dimensional plotting Scripts & Functions
facilities are:
• The programs written in the language of
>> semilogx(x,y) % semilogarithmic plot MATLAB should be saved with the extension
of (.m).
>> semilogy(x,y) % semilogarithmic plot
• M-files can be in the form of scripts and
>> loglog(x,y) % full logarithmic plot
functions.
>> area(x,y) % filled area plot
>> polar(x,y) % polar coordinate plot
>> bar([2:5]) % bar graph
Areej Abbas Abubaker 35 Areej Abbas Abubaker 36
6
09-Mar-21
Scripts
• Return to the MATLAB command window and
• Calculate the volume of a cylindrical tank:
type, and input the required data and see the
% Calculation of volume of a cylindrical tank results:
clc
disp('Calculating the volume of a cylindrical tank ')
d=input('Vector of diameters (m)=');
>> Volume
h=input('the height (m)='); Calculating the volume of a cylindrical tank
a=pi*d^2; % the area of the base Vector of diameters (m)=[1:0.5:5]
v=a*h;
plot(d,v) the height (m)=2
xlabel('d(m)')
ylabel('V (m^3)')
title('Volume of Tank vs Diameter')
Areej Abbas Abubaker 37 Areej Abbas Abubaker 38
Functions
• You can develop your own function and
execute it just like other built-in functions in
MATLAB.
• A function takes some data as input, performs
required calculations, and returns the results
of the calculations back to you.
• As an example, let us write a function to
calculate the volume of a cylindrical tank, that
we have already done in a script.
Areej Abbas Abubaker 39 Areej Abbas Abubaker 40
>> h=2;
M.file >> d0=[1:0.5:5];
function v =tank(d,h) >> volume=tank(d,h);
% Calculation of volume of a cylindrical tank >> plot(d,volume)
for k=1:length(d) >>
v(k,:)=pi*d(k)^2*h; % Ideal gas law
end
end
• This function can be used in the workspace, in a
script, or in another function. For example:
Areej Abbas Abubaker 41 Areej Abbas Abubaker 42