Introduction To Matlab: Appendix
Introduction To Matlab: Appendix
Introduction To Matlab: Appendix
INTRODUCTION TO
MATLAB
>> a=2
MATLAB creates variable 𝑎𝑎 in the workspace (or adds it to the existing variables)
and assigns it a value ‘2’. It displays the result in the command window as follows:
a =
2
To add variable 𝑏𝑏 with a value ‘6’, the following command is typed and result
displayed:
>>b=6
b=
6
Now, if a + b is typed on the command line, MATLAB calculates the indicated
sum and displays the following:
>>a+b
ans =
8
MATLAB stores the result or answer (ans) as a new variable for later calculations.
It can be given a different name by the user.
Variable name can be a combination of letters, digits and underscores but must
begin with a letter. It should be noted that MATLAB distinguishes between
uppercase and lowercase characters for variables and commands. MATLAB, by
default, displays output in the command window. However, display of output can
be suppressed by typing a semicolon at the end of command.
The following are few additional useful commands:
• clear or clear all removes all variables from the workspace
• who lists variables in workspace
• whos will give more details including size, space allocation, and class
of the variables
• clc clears the Command Window
• ctrl-c aborts a MATLAB computation
• To continue a line, type (….)
Operator Descriptions
+ addition
- subtraction
* multiplication
/ division
ʹ transpose
^ power
A =
1 3 10
7 5 9
6 8 3
>> B = [ 2 5 6
9 0 1
8 4 7]
B =
2 5 6
9 0 1
8 4 7
Symbol Descriptions
size dimensions of matrix
det determinant of matrix
inv inverse of matrix
diag diagonal of a matrix
trace sum of diagonal elements of matrix
eye(n) identity matrix (n x n)
zeros(i,j) zero matrix (i x j)
eig eigenvalues and eigenvectors
rand(i, j) (i x j) matrix with uniformly random numbers
Examples:
>> size(A)
ans =
3 3
>>det(A)
ans =
302
>>inv(A)
ans =
>>diag(A)
ans =
1
5
3
>>trace(A)
ans =
9
>>eye(3)
ans =
1 0 0
0 1 0
0 0 1
>>zeros(2,3)
ans =
0 0 0
0 0 0
>> [C L] = eig(A)
where the columns of C matrix give the eigenvectors, and the values of L matrix
gives eigenvalues. The output shows the following results:
C =
-0.4692 -0.8618 -0.8677
-0.6754 0.4374 0.1982
-0.5690 0.2569 0.4560
L =
17.4446 0 0
0 -3.5041 0
0 0 -4.9404
Extracting sub-matrix consisting of elements on the first two rows and the last
two columns of the matrix, then the commands lines are as follows:
>>A(1:2,2:3)
ans =
3 10
5 9
Symbol Descriptions
</> less than/greater than
<= / >= less than / greater than or equal to
== equal
~= not equal
~ logical not
& logical and
| logical or
A6.3.2 Loop and Logical Statements
Symbol Descriptions
if a conditional statement
elseif, else used along with if command
for loop command
while loop command with conditional statement
break terminate loop execution
The parameters on left hand side are output parameters and those on right hand
side are input parameters. Each function can have several input and output
parameters. Name of the above program is “function_name” and must be saved as
“function_name.m”.
>> myfunction(6)
g = 49
Symbol Descriptions
plot(x,y) 2-D plot y vs. x
title(' ') add title to current axes
xlabel/ylabel(' ') add label to x-axis/y-axis
text(' ') add text on the plot window
gtext(' ') receive a text from mouse input
switch on/off the grid on the plot
grid on/off
window
allow to add another plot to the
hold on/off
current plot
axis([xmin xmax ymin ymax]) scale x and y axes
produce a coordinate of a point on the
ginput
plot screen
plot3(x,y,z) plot in 3D coordinate system
generate n linearly spaced points
linspace(a,b,n)
between a and b
Example:
>> x = linspace(0,360,180);
>> y1=sind(x);
>> y2=cosd(x);
>> plot(x,y1, 'k- ');
>> hold on;
>> plot(x,y2, 'k-- ');
>> legend('y1=sinx ', 'y2=cosx');
>> xlabel('\theta');
>> ylabel('y1&y2');
>> grid on;
>> axis([0 360 -1.1 1.1]);
The options are very convenient to differentiate between variables in the same
figure. Color and line style is controlled by adding a third argument in the plot
command. The first and second characters of this third argument specify the color
and the line style, respectively. The following example produces a dashed green
plot.
Current
folder
Command window
Figure A6-1.
1 y1=sinx
y2=cosx
0.8
0.6
0.4
0.2
y1&y2
-0.2
-0.4
-0.6
-0.8
-1
0 50 100 150 200 250 300 360
θ
Figure A6-2.