INTRODUCTION
SPEAKER
LEARNING OUTCOMES
At the end of the workshop:
1. Participants should be able to use MATLAB in research, data
analysis, creating functions, and mathematical presentations.
2. Participants should be able to solve scientific/research
problems using MATLAB
3. Participants should be able to model the data using MATLAB.
4. Participants should be able to create professional
graphs/plots for peer reviewed journals and international
conferences.
EXCEL SPREADSHEETS.
Importing excel spread sheet.
Plotting data.
GRAPHICS.
Two dimensional plotting functions.
Sub-plots.
Bar and 3-D bar graphs.
Polygons graphs.
Pie graphs.
Three dimensional plotting functions.
MATLAB PROGRAMMING.
Loops.
Branching with if.
Switch statement.
Branching with while.
MATLAB Workshop
Command Window - to
main window, enter
variables, run programs
Current Folder - purpose
as a shows the files in the
current directory
Command History window & logs commands
entered in the command
window
Workspace Window - to
provides information
about the variables that
are used
Editor Window - creates
and debugs script and
function files
Introduction
Type a valid expression, for example:
>> 5 + 5
When you click the Execute button, the result returned is:
ans = 10
Let us take up few more examples:
>> 3^2
ans = 9
>> 7/0
ans = Inf
>> 732 * 20.3
ans = 1.4860e+04
MATLAB Workshop
Introduction
Use of Semicolon (;) in MATLAB: indicates end of
statement. However, if you want to suppress and hide the
MATLAB output for an expression, add a semicolon after the
expression. For example,
>> x = 3;
>> y = x + 5
ans = 8
Adding comments: The percent symbol (%) is used for
indicating a comment line. For example,
>> x = 9
MATLAB Workshop
Introduction
MATLAB supports the following commonly used operators
and special characters:
+
*
.*
.
\
/
.\
./
:
()
[]
MATLAB Workshop
Introduction
MATLAB Workshop
Introduction
MATLAB Workshop
Introduction
For example, you can compute the sum of the square and the
square root of the previous answer as follows:
>> ans^2 + sqrt(ans)
ans =
510.9934
Observe that MATLAB assigns a new value to ans with each
calculations.
MATLAB Workshop
Introduction
Multiple Assignments: You can have multiple assignments
on the same line. For example,
>> a = 2; b = 7; c = a * b
c=
14
I have forgotten the variables!: The who command
displays all the variable names you have used.
>> who
MATLAB will execute the above statement and return the
following result:
Your variables are:
a
ans b
MATLAB Workshop
Introduction
The format command: By default, MATLAB displays
numbers with four decimal place values. This is known as
short format. However, if you want more precision, you need
to use the format command.
The format long command displays 16 digits after decimal.
For example,
>> format long
>> x = 7 + 10/3+5^1.2
x=
17.231981640639408
Another example,
>> format short
>> x = 7 + 10/3+5^1.2
x=
17.2320
MATLAB Workshop
>> X = 1:9
The increment can be specified as the second of three
arguments:
>> X = 0:2:10
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
Creating arrays:
1
Row vector:
array = [1 5 7 9];
Column vector:
array = [1; 5; 7; 9];
array = [1 5 7 9];
After, MATLAB creates an array automatically, with the value
you specified, stored in MATLAB as array.
MATLAB Workshop
Examples:
1. array = [1 5 8 7];
2. num = array(2);
3. num = array(4);
to copy an entire array over, simply set another variable equal to the
1
array you
want to copy.
2
array2 = array
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
Example:
A = [1 2 3 4;
5 6 7 8;
9 6 4 3;
2 3 5 9]
A(3,4) = 27;
A(1:2,3:4) = [5 6; 99 22];
A(2,:) = [8 19 2 20];
A(:,3) = [5;4;3;2];
A(1:2,:) = 5;
MATLAB Workshop
MATLAB Workshop
Example:
>> r = sort(A, 2, descend)
>> d = sort (A, ascend) % ascend is the default
MATLAB Workshop
MATLAB Workshop
Example:
>> zeros(3)
>> zeros(2,3)
>> ones(3)
>> ones(2,3)
>> rand(3)
>> rand(2,3)
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
M-Files
MATLAB Workshop
Script M-Files
MATLAB Workshop
Function M-Files
MATLAB Workshop
Graphics
MATLAB Workshop
MATLAB Workshop
Assuming that we have two variables, x and y and are both the
same size
To produce the most basic graph in MATLAB that plots y vs.
x, you just do:
plot(x,y)
MATLAB Workshop
Graphics - plot
step size of 1
MATLAB Workshop
Graphics - linspace
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
Example:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
hold on
y2 = cos(x);
plot(x,y2,r:)
legend(sin(x),cos(x))
grid
MATLAB Workshop
MATLAB Workshop
Example:
To make a window that has 4 plots: 2 rows and columns
1 Do subplot(2,2,1): Specify the top left corner
2 Do subplot(2,2,2): Specify the top right corner
3 Do subplot(2,2,3): Specify the bottom left corner
4 Do subplot(2,2,4): Specify the bottom right corner
MATLAB Workshop
MATLAB Workshop
Bar Graphs
MATLAB Workshop
Bar Graphs
The bar function distributes bars along the x-axis. Elements
in the same row of a matrix are grouped together.
For example, if a matrix has five row and three columns, the
bar displays five groups of three bars along the x-axis. The
first cluster of bars represents the elements in the first row of
Y.
>> Y = [5,2,1
8,7,3
8,8,6
5,5,5
4,3,2];
>> figure
>> bar(Y)
MATLAB Workshop
MATLAB Workshop
Polygons
patch: create one or more filled polygons. For example,
>> xdata = [1;5;3];
>> ydata = [3;2;5];
>> patch(xdata,ydata,w)
MATLAB Workshop
Pie Graph
>> x = [1,2,3];
>> figure
>> pie(x)
>> legend(Product A,Product B,Product C)
% OR
>> labels = {Product A,Product B,Product C}
>> legend(labels,Location,Southoutside,...
Orientation,horizontal)
MATLAB Workshop
Graphs
MATLAB Workshop
Graphs
How do we generate a grid of x and y values?
1
MATLAB Workshop
Graphs
MATLAB Workshop
Graphs
MATLAB Workshop
MATLAB Programming
MATLAB Workshop
MATLAB Workshop
Another example:
>> x = 1:10;
>> for i = 1 : 10
x2(i) = x(i)^2;
end
>> x2
x2 =
1 4 9 16
25
36
49
64
MATLAB Workshop
81
100
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
Relational Operators
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
Relational Operators
MATLAB Workshop
An if can have zero or one elses and it must come after any
elseifs.
An if can have zero to many elseifs and they must come
before the else.
Once an elseif succeeds, non of the remaining elseifs or elses
will be tested.
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
Switch statement
A switch block conditionally executes one set of statements
from several choice. Each choice is covered by case
statement. An evaluated switch expression is a scalar or
string. The switch block tests each case until one of the cases
is true. A case is true when:
1
2
3
MATLAB Workshop
Switch statement
otherwise
<statements>
end
MATLAB Workshop
Switch statement
Example: create a script file and type the following code in it,
grade = B;
switch(grade)
case A
fprintf(Excellent!\n);
case B
fprintf(Well done\n);
case C
fprintf(Well done\n);
case D
fprintf(You passed\n);
case F
fprintf(Better try again\n);
otherwise
fprintf(invalid grade\n);
end
Dr. Mahmoud A. M. Albreem
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop
MATLAB Workshop