Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

INTRODUCTION TO MATLAB

by SARFARAZ and FARHA


Table of Contents
Clear command window and workspace .................................................................................. 1
To display a msg ................................................................................................................ 1
Variable declaration ............................................................................................................. 1
Mathematical operation ........................................................................................................ 2
Matrix operation ................................................................................................................. 3
Augmentation oof matrrix ..................................................................................................... 4
Display some elements from matrix ....................................................................................... 5
Define a zero,ones,random,magic,identity,size of matrix ............................................................ 6
For decimal places .............................................................................................................. 7
Diplay an even,odd numbers ................................................................................................. 8
Taking input from user and then convert to centigrade ............................................................... 8
Plot a graph, and label it ...................................................................................................... 8
Subplot .............................................................................................................................. 9
for more matlab codes , visit ............................................................................................... 10
https://blogbypower.wordpress.com ....................................................................................... 10
https://in.linkedin.com/in/sarfaraz-musa-96019a88 ................................................................... 10

Examples: These examples give you a brief introduction to MATLAB

Clear command window and workspace


clc
clear all

To display a msg
display('Hello. !! This is an introductory session for MATLAB code')

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 =

4.0000 -3.0000 0.0000


-4.1111 2.8889 0.2222
-0.4444 0.5556 -0.1111

expo =

1.0e+03 *

0.0546 0.0201 0.4034


0.1484 0.0546 2.9810
8.1031 2.9810 1.0966

etran =

4 5 9
3 4 8
6 8 7

esin =

3
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA

-0.7568 0.1411 -0.2794


-0.9589 -0.7568 0.9894
0.4121 0.9894 0.6570

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

Augmentation oof matrrix


aug1=[e,e]
aug2=[e;e]

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

Display some elements from matrix


frow1=f(1,:)
frow3=f(3,:)
fcol2=f(:,2)
fcol5=f(:,5)
frow123=f(1:3,:)
fcol34=f(:,3:4)

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 =

0.8003 0.4218 0.7922


0.1419 0.9157 0.9595

e =

4 3 6 0
5 4 8 0
9 8 7 0
0 0 0 10

ans =

4 4

For decimal places


format long
plong=e*inv(e)

format short
pshort=e*inv(e)

plong =

Columns 1 through 3

0.999999999999998 -0.000000000000000 0.000000000000001


-0.000000000000004 1.000000000000000 0.000000000000002
-0.000000000000005 -0.000000000000001 1.000000000000003
0 0 0

Column 4

0
0
0
1.000000000000000

pshort =

1.0000 -0.0000 0.0000 0


-0.0000 1.0000 0.0000 0

7
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA

-0.0000 -0.0000 1.0000 0


0 0 0 1.0000

Diplay an even,odd numbers


number=1:10
nm=0:10:90
even=0:2:10
odd=1:2:10

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

Taking input from user and then convert to


centigrade
% f=input('entern temperature in farenheit:');
% centi= (f-32)/1.8;
% ['temperature is ',num2str(centi),'C']

Plot a graph, and label it


figure(1)
x1=0:pi/10:2*pi;
y1=sin(x1);
plot(x1,y1)

hold on
y2=cos(x1);
plot(x1,y2,':')

8
INTRODUCTION TO MAT-
LAB by SARFARAZ and FARHA

xlabel('angle from 0 to 2pi')


ylabel('sin and cos of angle form 0 to 2pi')
title('sin/cos vs angle')
legend('sin','cos')

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

for more matlab codes , visit


https://blogbypower.wordpress.com
https://in.linkedin.com/in/sar-
faraz-musa-96019a88
Published with MATLAB R2014a

10

You might also like