Professional Documents
Culture Documents
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];
Plus
Minus
Matrix multiplication
Array multiplication
Matrix power
Array power
./
%
Array division
Comment
'
det
inv
sin
cos
exp
log
Determinant of a Matrix
Inverse of a Matrix
Sine
Cosine
Exponential
Natural logarithm
sqrt
Square root
SPECIAL MATRICES
Examples
>> zeros(4)
ans =
0
Page 1
ans =
0
>> ones(4)
ans =
1
Examples
>> V2=v.^2
V2 =
1
16
0
2
3
0
1
1
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
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)
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
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)
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
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
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