Professional Documents
Culture Documents
Tutorial 001
Tutorial 001
To display a msg
display('Hello. !! This is an introductory session for MATLAB code')
Variable declaration
a=10
b=20
c=[4 2 3]
d=[5;3;6]
e=[4 3 6;5 4 8; 9 8 7]
f=[5 4 7 6 8
5 8 7 4 6
6 4 2 5 9
8 7 6 9 7]
1
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
a =
10
b =
20
c =
4 2 3
d =
5
3
6
e =
4 3 6
5 4 8
9 8 7
f =
5 4 7 6 8
5 8 7 4 6
6 4 2 5 9
8 7 6 9 7
Mathematical operation
aintob=a*b
abyb=a/b
cintod=c*d
dintoc=d*c
aintob =
200
abyb =
0.5000
2
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
cintod =
44
dintoc =
20 10 15
12 6 9
24 12 18
Matrix operation
einv=inv(e)
expo=exp(e)
etran=e'
esin=sin(e)
eadd=e+10
emul=10*e
elemult=e.*e
matmul=e*e
sqrmat=e^2
sreleem=e.^2
einv =
expo =
1.0e+03 *
etran =
4 5 9
3 4 8
6 8 7
esin =
3
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
eadd =
14 13 16
15 14 18
19 18 17
emul =
40 30 60
50 40 80
90 80 70
elemult =
16 9 36
25 16 64
81 64 49
matmul =
85 72 90
112 95 118
139 115 167
sqrmat =
85 72 90
112 95 118
139 115 167
sreleem =
16 9 36
25 16 64
81 64 49
4
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
aug1 =
4 3 6 4 3 6
5 4 8 5 4 8
9 8 7 9 8 7
aug2 =
4 3 6
5 4 8
9 8 7
4 3 6
5 4 8
9 8 7
frow1 =
5 4 7 6 8
frow3 =
6 4 2 5 9
fcol2 =
4
8
4
7
fcol5 =
8
6
9
7
5
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
frow123 =
5 4 7 6 8
5 8 7 4 6
6 4 2 5 9
fcol34 =
7 6
7 4
2 5
6 9
Define a zero,ones,random,magic,identity,size
of matrix
zmat=zeros(3,4)
onesmat=ones(4,3)
mg=magic(4)
random1=rand(2)
random2=rand(2,3)
e(4,4)=10
size(e)
zmat =
0 0 0 0
0 0 0 0
0 0 0 0
onesmat =
1 1 1
1 1 1
1 1 1
1 1 1
mg =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
random1 =
6
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
0.1576 0.9572
0.9706 0.4854
random2 =
e =
4 3 6 0
5 4 8 0
9 8 7 0
0 0 0 10
ans =
4 4
format short
pshort=e*inv(e)
plong =
Columns 1 through 3
Column 4
0
0
0
1.000000000000000
pshort =
7
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
number =
1 2 3 4 5 6 7 8 9 10
nm =
0 10 20 30 40 50 60 70 80 90
even =
0 2 4 6 8 10
odd =
1 3 5 7 9
hold on
y2=cos(x1);
plot(x1,y2,':')
8
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
Subplot
figure(2)
subplot(2,2,1);plot(x1,y1)
xlabel('angle from 0 to 2pi')
ylabel('sin of angle form 0 to 2pi')
title('sin vs angle')
subplot(2,2,2);plot(x1,y2)
xlabel('angle from 0 to 2pi')
ylabel('sin of angle form 0 to 2pi')
title('cos vs angle')
9
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA
10