Documente Academic
Documente Profesional
Documente Cultură
Basic Course on
Matlab
Prepared by:
Dr Abul Khair bin Anuar
Dept of Communication Engineering, FKEE
Course Outcomes
1
7/03/2016
Resources...
Interface
Current folder
To create
new script Workspace
Command window - Showing variables
- Where you entered your
command
Command History
- Past command
2
7/03/2016
Matrix
>> a = [1 2 3 4]
Will gives you 1 x 4 matrix
b = [1; 2; 3; 4]
Will gives you 4 x 1 matrix
>> c = [1 2; 3 4]
Will gives you 2 x 2 matrix
3
7/03/2016
4
7/03/2016
Matrix Operations
5
7/03/2016
6
7/03/2016
2 . a*b
This will produce a (4x4) matric since a is a (4x1) matrix, and b is
still a (1x4) matrix.
(4x1)*(1x4) = (4x4)
3. a.*b
This is element multiplication. Each element in a is multiplied with
element in b at the same position.
Output
ans =
2 4 6 8
Accessing element
An element in a matrix can be accessed individually by the following
syntax;
aMatrix(m,n)
where aMatrix is the matrix, m = is the elements row number and n is the
elements column number. In Matlab, indexing starts at 1.
Example;
>> a = [1 2 3 4]; b = [2 2 2 2];
>> aMatrix = a*b; produces (4x4) matrix
To access the element in row 1 and column 1, write the following syntax
>> aMatrix(1,1)
To access all elements in row 1
>> aMatrix(1,:)
To access all elements in column 1
>> aMatrix (:,1)
Accessing certain elements
>> aMatrix(1:2,2:3)
7
7/03/2016
Matrix manipulation
Exercise
8
7/03/2016
Creating a script...
Writing a script
9
7/03/2016
To clear Command
Window, close figures
and clear Workspace
Variables
Most computer codes begin with variable/constant
declaration. In Matlab, this is done simply by writing the name
and its value before any attempt to use the variable.
A good practice is to use name that it should be self explained.
Eg. To declare a variable for temperature, the name
temperature = 30; %initial temperature, degree Celcius
Each line is normally ended with semi-colon.
Comment is placed after the declaration
for information on the variable.
is much better than just;
t = 30; %initial temperature
Constants are usually represented using CAPITAL LETTERS, eg.
SPEED_OF_LIGHT = 3e8; How to write 3 x 108
Use small and capital letters for long variable names, eg.
initialTemperatureCelcius = 30; Since the variable is self-explained,
no comment is needed
10
7/03/2016
Plotting
Use the Editor to plot a sine function. Let the x-axis represents
the angle, and y-axis represents the values.
Write
angle = -pi:0.1:pi; to create the matrix of x-axis from
to with step size of 0.1
y = sin (angle); sine function (and all trigonometric
functions) only works with radian values
Run the script by clicking the run icon. You might be asked to
save the file first. Save it as Example1.m.
On the main window, you should observe the following.
In Workspace, youll see two variables, angle and y. You can see
the values(by clicking on it) and size of each variable.
To plot the sine function, you can use the Command Window or
add the command in your script.
plot(angle,y); x-axis followed by y-axis
In Command Window, it will be one off command.
In script, the line will be executed everytime the code is executed.
A popup figure will appear. And the plot of sin should be
plotted.
The property of the plot can be change according to your needs.
In Command Window, type
help plot
The help function should be your best tool to understand the
syntax of any Matlab function.
11
7/03/2016
Lets write
plot(angle,y,rx--);
This shows how to control the colour, marker and the linetype of
the plot.
In addition to plot function, there are other types of plotting
tool available. If you type
help plot
you will see related function to plot at the end of the
document.
Try using stem to plot the signal
stem(angle,y);
12
7/03/2016
13
7/03/2016
Multiple Plot
14
7/03/2016
15
7/03/2016
16
7/03/2016
Conditional Operation
17
7/03/2016
Example 2
Write a script that ask user to choose any number between 1 to 10. For
odd number, do the square of the number, and for even number find the
square root.
n = input('Please choose a number between 1 and 10\n');
To ask user to if mod(n,2)==0 Modulus. It gives the remainder of
pass in value for the operation n/2
a variable disp('The number you choose is even number ');
A command to convert the type of variable from
disp(num2str(n)); number to string. Check str2num using help.
Display a variable
value of type string
disp('The root of the number is');
disp(num2str(sqrt(n)));
Square root
else
disp('The number you choose is odd number ');
disp(num2str(n));
disp('The square of the number is');
disp(num2str(n^2)); The power operator
power normally use more
end memory than simply n*n !
Basic Troubleshooting
18
7/03/2016
Process error
Much harder to detect, unless you know what are you expecting
Script can be executed, but you get wrong output
Eg. Instead of adding two numbers, you do multiplication.
Use keyboard to check the output of the code at different stages of
execution.
Exercise 2
sinc
sinc
19
7/03/2016
Basic functions
20