Sie sind auf Seite 1von 14

Student No: Name:

Class: Sem:

Subject: Experiment No:

Maximum Marks: Marks Obtained:


Aim: - Introduction to Matlab & Matrix Manipulation.

Clear command window and workspace


clc clear all

To display a message
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]
a = 10

b = 20

c = 4 2 3

d =

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

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 =
-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 9
112 95 118
139 115 167
sreleem =

16 9 36
25 16 64
81 64 49
Augmentation of matrix
aug1=[e,e]

aug2=[e;e]

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
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 =
0.1576 0.9572
0.9706 0.4854

random2 =

0.8003 0.4218 0.7922


0.1419 0.9157 0.9595

e =

4 3 4 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
-0.0000 -0.0000 1.0000 0
0 0 0 1.0000

Display 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('enter temperature in Fahrenheit:');
centi= (f-32)/1.8;
display('temperature is in centigrade')
display(centi);
enter temperature in Fahrenheit:76
temperature is in centigrade

centi =

24.4444

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,':')
xlabel('angle from 0 to 2pi')
ylabel('sin and cos of angle from
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')
Matrix Manipulation

Collect student data for 5 students and 5 subjects, feed into


Matlab in matrix form.
Append marks for sixth subject in matrix.
Scale marks for sixth subject out of 50.
Extract 2nd and 3rd student marks for all subjects and
calculate its total and average marks.
Calculate average marks for all the subjects
Scale all marks out of 10

NAME/SUBJECTS APS CM LCD HV PSPR

Vinod 86 75 72 94 82

Ajay 68 87 78 78 88

Sanjay 76 91 68 82 84

Rakesh 79 74 65 88 76

Karan 94 82 79 89 72
Give input to MATLAB in the form of matrix
AllMarks = [86 75 72 94 82
68 87 78 78 88
76 91 68 82 84
79 74 65 88 76
94 82 79 89 72]

AllMarks =

86 75 72 94 82

68 87 78 78 88

76 91 68 82 84

79 74 65 88 76

94 82 79 89 72

Appending marks for PEF after receiving


PEFMarks=[94 78 65 74 68]
AllMarks=[AllMarks PEFMarks']

PEFMarks = 94 78 65 74 68

AllMarks =

86 75 72 94 82 94

68 87 78 78 88 78

76 91 68 82 84 65

79 74 65 88 76 74

94 82 79 89 72 68

Scaling Marks for PEF out of 50 and adding it to the


existing matrix

PEFMarks= AllMarks(:,6)*0.5
AllMarks(:,6)=PEFMarks
PEMarks =

47.0000

39.0000

32.5000

37.0000

34.0000

AllMarks =

86.0000 75.0000 72.0000 94.0000 82.0000 47.0000

68.0000 87.0000 78.0000 78.0000 88.0000 39.0000

76.0000 91.0000 68.0000 82.0000 84.0000 32.5000

79.0000 74.0000 65.0000 88.0000 76.0000 37.0000

94.0000 82.0000 79.0000 89.0000 72.0000 34.0000

Extract 2nd and 3rd student marks for all subjects and
calculate its total and average marks.
ArunMarks=AllMarks(2,:)
ArunTotal=sum(ArunMarks)
ArunAverage=mean(ArunMarks)

ArunMarks = 68.0000 87.0000 78.0000 78.0000 88.0000 39.0000

ArunTotal =

438.0000

ArunAverage =

73.0000

VinodMarks=AllMarks(3,:)
VinodTotal=sum(VinodMarks)
VinodAverage=mean(VinodMarks)

VinodMarks = 76.0000 91.0000 68.0000 82.0000 84.0000 32.5000

VinodTotal =

433.5000

VinodAverage =

72.2500
Calculate average marks for all the subjects
AllMean=mean(AllMarks,1)

AllMean =

80.6000 81.8000 72.4000 86.2000 80.4000 37.9000

Scale all marks out of 10


AllMarks=AllMarks*diag([0.1 0.1 0.1 0.1 0.1 0.2])

AllMarks =

8.6000 7.5000 7.2000 9.4000 8.2000 9.4000

6.8000 8.7000 7.8000 7.8000 8.8000 7.8000

7.6000 9.1000 6.8000 8.2000 8.4000 6.5000

7.9000 7.4000 6.5000 8.8000 7.6000 7.4000

9.4000 8.2000 7.9000 8.9000 7.2000 6.8000

Das könnte Ihnen auch gefallen