You are on page 1of 5

MATLAB I - INTRODUCTION TO MATLAB

1. Variables, matrices and vectors

Define the followings by using MATLAB.

a=5

v = {1, 2, 3, 4}

1 2 3
e = 0 2 3
0 1 1

1
0

c = 2

3
6

2 1 2 3
1 1 3 2
1 1 4 4

0 1 1 5
1 1 2 6

>> a=5;
>> v=[1 2 3 4];
>> e=[1 2 3 ; 0 2 3 ; 0 1 1 ];
>> c=[1,2,1,2,3 ; 0,1,1,3,2 ; 2,1,1,4,4 ; 3,0,1,1,5 ; 6,1,1,2,6];

here v can also be defined as,


>> v=1:5; % v={From one to five}

2. Some Operations Using Matrices Vectors and Scalars


+
*
*
^
.^

Plus
Minus
Matrix multiplication
Array multiplication
Matrix power
Array power

./
%

Array division
Comment

'

Transpose and quote

det
inv
sin
cos
exp
log

Determinant of a Matrix
Inverse of a Matrix
Sine
Cosine
Exponential
Natural logarithm

sqrt

Square root

SPECIAL MATRICES

A number of special functions exist in MATLAB to create special or unusual


matrices.

Examples

>> zeros(4)

% creates a square matrix (4x4) whose elements are zero.

ans =
0

Page 1

MATLAB I - INTRODUCTION TO MATLAB


>> zeros(3,2)

% creates a 3 row,2 column matrix of zero.

ans =
0

>> ones(4)

% creates a square matrix (4x4) whose elements are one.

ans =
1

Examples
>> V2=v.^2

% the square of the vector v

V2 =
1

16

>> e' % the transpose of the matrix e


ans =
1
2
3
>> det(e)

0
2
3

0
1
1

% determinant of the matrix e

ans =
-1
inv(c)
ans =
-0.3000
0.7000
-0.9000
-0.2000
0.4000

Page 2

0.5000
-0.5000
3.5000
-0.0000
-1.0000

-0.4000
0.1000
-2.7000
0.4000
0.7000

-0.3000
-0.3000
0.1000
-0.2000
0.4000

0.5000
-0.0000
1.0000
-0.0000
-0.5000

MATLAB I - INTRODUCTION TO MATLAB


Example:

Define y=sin(t)*cos(t), where -2 < t < 2.


>> t=-2*pi:pi/100:2*pi;
>> y=sin(t).*cos(t);

3. Plotting Graphs
PLOT Plot vectors or matrices.
PLOT(X,Y) plots vector X versus vector Y. If X or Y is a matrix, then the vector
is plotted versus the rows or columns of the matrix, whichever line up. PLOT(Y) plots
the columns of Y versus their index. If Y is complex, PLOT(Y) is equivalent to
PLOT(real(Y),imag(Y)). In all other uses of PLOT, the imaginary part is ignored.
Examples:
Plot this function y = cos(2t ) and z = (cos(2t )) 3 , for -2<t<2
>>t=-2*pi:pi/100:2*pi;
>>y=cos(2*t);
>>plot(t,y)
>>z=y.^3;
>>plot(t,z)

Plot y and z on the same graph.


>> plot(t,y,r,t,z,g);

% to plot y red and z green on the


same garph

Label the x-axis as time and y-axis as output and title as fatos.
>> xlabel(time);
>> ylabel(Output);
>> title(fatos);

Example:
Plot the following functions onto same graph windows.
y1=sin(t), y2=cos(t), t=-2,2
>> t=-2*pi:0.01:2*pi;
>> y1=sin(t); y2=cos(t);
>> plot(t,y1,r, t,y2,b);

Page 3

MATLAB I - INTRODUCTION TO MATLAB

4. M-files
To crate m file just go to file menu and chose edit file. You must save into the folder of
work.
Exercise: Create a m-file and write these into,

addition.M
%

ADDITION

function addition(a),
sum=0;
for i=1:a,
sum=sum+i;
end
disp(['THE SUM OF THE THESE NUMBERS IS =', num2str(sum)]);
% disp means display
>> addition(4)

5. How to get help on library functions


HELP : Used to get help on the specified topic. "help topic" gives help on the specified

topic. The topic can be a command name or a directory name. If it is a command name,
help displays information on that command. If it is a directory name, help displays the
Table-Of-Contents for the specified directory. It is not necessary to give the full
pathname of the directory; the last component, or last several components, are sufficient.
Example: If you want to get help on special topic, such as xlabel you may type,
help xlabel
XLABEL X-axis labels for 2-D and 3-D plots.
XLABEL('text') adds text below the X-axis on the current axis.
See also YLABEL, ZLABEL, TITLE, TEXT.

Page 4

MATLAB I - INTRODUCTION TO MATLAB


6. Home Exercises:
(Submit the printed figures and necessary commands for the following three exercises
next week.)

1.) Plot the given function on the same graph windows, give your name as title use
time for x-axis and output for y-axis.
y1(t)=cos(t)+sin(3t), y2(t)=2.5sin(t/2), t { -2, 2 }
x1=cos(3*t), x2=0.5*sin(t/3), t { -3, 3 }

2.) Let

1
1
A=
2

0
2
1
0

9
5
1
0

2
1 2 3
1 0 -1 3
0
1
2
4

, B = 0 0 1 , C =
and D = 0 -1 2 1

1
0
5
1

0 1 1 2

1 2 1
1

Find the followings.


The determinant of matrices A and B.
T

AD
The inverse of matrix A.

3.) Write the M-file of the product of first n numbers ( n ! ) and write the name of
this file is the factorial(n)

Page 5

You might also like