0% found this document useful (0 votes)
27 views10 pages

Experiment 1

This document contains the code for 10 experiments involving basic MATLAB operations like defining vectors and matrices, performing element-wise operations, plotting functions, writing custom functions, and converting between data types. The code demonstrates how to create and manipulate different data structures in MATLAB, including vectors, matrices, cell arrays, and Java arrays. Key functions and operations used include ones, diag, reshape, mean, find, and cell.

Uploaded by

abhigyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

Experiment 1

This document contains the code for 10 experiments involving basic MATLAB operations like defining vectors and matrices, performing element-wise operations, plotting functions, writing custom functions, and converting between data types. The code demonstrates how to create and manipulate different data structures in MATLAB, including vectors, matrices, cell arrays, and Java arrays. Key functions and operations used include ones, diag, reshape, mean, find, and cell.

Uploaded by

abhigyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment 1

%
%simple problems
%q1
a) a=10;
a =10
b) b=2.5*(10^23);
b=2.5000e+023
c) c=2+3i;
c = 2.0000 + 3.0000i
d) d=exp((j*2*pi)/3);
d = -0.5000 + 0.8660i
%q2
a) avec=[3.14 15 9 26];
avec = 3.1400 15.0000

9.0000 26.0000

b) bvec=[2.71;8;28;182];
bvec = 2.7100
8.0000
28.0000
182.0000
c) cvec=[5:-0.2:-5];
cvec = Columns 1 through 10
5.0000
3.2000

4.8000

4.6000

4.4000

4.2000

4.0000

3.8000

3.6000

3.4000

2.4000

2.2000

2.0000

1.8000

1.6000

1.4000

0.4000

0.2000

Columns 11 through 20
3.0000
1.2000

2.8000

2.6000

Columns 21 through 30
1.0000
-0.8000

0.8000

0.6000

0 -0.2000 -0.4000 -0.6000

Columns 31 through 40
-1.0000 -1.2000 -1.4000 -1.6000 -1.8000 -2.0000 -2.2000 -2.4000 -2.6000
-2.8000
Columns 41 through 50

-3.0000 -3.2000 -3.4000 -3.6000 -3.8000 -4.0000 -4.2000 -4.4000 -4.6000


-4.8000
Column 51
-5.0000
d) dvec=logspace(0,1,101);
dvec = Columns 1 through 10
1.0000
1.2303

1.0233

1.0471

1.0715

1.0965

1.1220

1.1482

1.1749

1.2023

1.3490

1.3804

1.4125

1.4454

1.4791

1.5136

1.6982

1.7378

1.7783

1.8197

1.8621

1.9055

2.1380

2.1878

2.2387

2.2909

2.3442

2.3988

2.6915

2.7542

2.8184

2.8840

2.9512

3.0200

3.3884

3.4674

3.5481

3.6308

3.7154

3.8019

4.2658

4.3652

4.4668

4.5709

4.6774

4.7863

5.3703

5.4954

5.6234

5.7544

5.8884

6.0256

6.7608

6.9183

7.0795

7.2444

7.4131

7.5858

8.5114

8.7096

8.9125

9.1201

9.3325

9.5499

Columns 11 through 20
1.2589
1.5488

1.2882

1.3183

Columns 21 through 30
1.5849
1.9498

1.6218

1.6596

Columns 31 through 40
1.9953
2.4547

2.0417

2.0893

Columns 41 through 50
2.5119
3.0903

2.5704

2.6303

Columns 51 through 60
3.1623
3.8905

3.2359

3.3113

Columns 61 through 70
3.9811
4.8978

4.0738

4.1687

Columns 71 through 80
5.0119
6.1660

5.1286

5.2481

Columns 81 through 90
6.3096
7.7625

6.4565

6.6069

Columns 91 through 100


7.9433
9.7724

8.1283

Column 101

8.3176

10.0000
e) evec=['hello'];
evec = hello

%q3
a) aMat=2*ones(9);
aMat =
2

b) v=[1 2 3 4 5 4 3 2 1];
v=1

bMat=diag(v);
bMat =
1

c) a=1:100
a = Columns 1 through 17
1

10

11

12

13

14

15

16

17

Columns 18 through 34
18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

40

41

42

43

44

45

46

47

48

49

50

51

Columns 35 through 51
35

36

37

38

39

Columns 52 through 68
52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

Columns 69 through 85
69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

91

92

93

94

95

96

97

98

99 100

Columns 86 through 100


86

87

88

89

90

cMat=reshape(a,10,10);
cMat =

11

21

31

41

51

61

71

81

91

12

22

32

42

52

62

72

82

92

13

23

33

43

53

63

73

83

93

14

24

34

44

54

64

74

84

94

15

25

35

45

55

65

75

85

95

16

26

36

46

56

66

76

86

96

17

27

37

47

57

67

77

87

97

18

28

38

48

58

68

78

88

98

19

29

39

49

59

69

79

89

99

10

20

30

40

50

d) dMat=nan(3,4);
dMat = NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
e) eMat=[13 -1 5 ; -22 10 -87];

60

70

80

90 100

84

85

eMat =

13

-1

-22

10 -87

f) a=-3+6*rand(3,5)
a=

-0.3675

1.7712 -0.3265

-0.7106 -1.8788

1.5281

0.9306

0.8779 -1.3438 -2.0243

1.5931 -0.0614

1.2562

1.0782 -2.2860

fvec=floor(a);
fvec =

-1

-1

-1

-2

-2

-3

-1

-3

%q4
a) csum=sum(cMat);
csum = Columns 1 through 8
55 155 255 355 455 555 655 755
Columns 9 through 10
855 955
b) emean=mean(eMat,2);
emean =

1
-33

c) eMat(1,:)=[1 1 1];
eMat =

1
-22

10 -87

d) csub=cMat(2:9,2:9);
csub = 12

22

32

42

52

62

72

82

13

23

33

43

53

63

73

83

14

24

34

44

54

64

74

84

15

25

35

45

55

65

75

85

16

26

36

46

56

66

76

86

17

27

37

47

57

67

77

87

18

28

38

48

58

68

78

88

19

29

39

49

59

69

79

89

e) lin=[1:20];
lin = Columns 1 through 17
1

10

11

-8

9 -10

0.5853

0.2238

12

13

14

15

16

17

Columns 18 through 20
18

19

20

lin(1,2:2:20)=-1.*lin(1,2:2:20);
lin = Columns 1 through 17
1

-2

-4

-6

Columns 18 through 20
-18

19 -20

f) r=rand(1,5);
r = 0.4984

0.9597

0.3404

ind=find(r<0.5);
ind = 1

r(ind)=0;
r= 0

0.9597

%q5
t=0:0.2:20
y=sin(t)
plot(t,y);
hold on;
plot(t,cos(t),'--r');
xlabel('time');
ylabel('sin(t) & cos(t)');
legend('sin(t)','cos(t)',6);
xlim([0 20]);
ylim([-1.4 1.4]);

0.5853

11 -12

13 -14

15 -16

17

%q6
function [x,y] = getcircle(center,r)
center=[4,8];
r=4;
x=-r+center(1,1):0.05:r+center(1,1);
y=sqrt(r.^2- ((xcenter(1,1)).^2))+center(1,2);
plot(x,y);
xlim([x(1,1)-1 x(end)+1]);
ylim([-r-1+center(1,2)
r+1+center(1,2)]);
hold on;
y=-sqrt(r.^2- ((xcenter(1,1)).^2))+center(1,2);
plot(x,y);
%q7
function [] = looptest(N)
for n=1:N
a=num2str(n);
if (mod(n,2)==0)
if (mod(n,3)==0)
disp(sprintf('%s is divisible by
else
disp(sprintf('%s is divisible by
end
elseif (mod(n,3)==0)
disp(sprintf('%s is divisible by 3',
else
disp(sprintf('%s is not divisible by
end
end

>> looptest(7)
1 is not divisible by 2
2 is divisible by 2
3 is divisible by 3
4 is divisible by 2
5 is not divisible by 2
6 is divisible by 2 AND
7 is not divisible by 2

AND 3

AND 3
3
AND 3

2 AND 3', a))


2', a))
a))
2 AND 3', a))

%q8
% 3a+6b+4c=1 a+5b=2 7b+7c=3
a=[3 6 4;1 5 0;0 7 7];
a= 3

b=[1;2;3];
b =

1
2
3

x=a\b
x=

-0.5824
0.5165
-0.0879

%q9
x=0:0.01:5;
y=x.*exp(-x./3);
z=trapz(x,y)

% quad(fun,a,b) y=@(x)x.*exp(-

x./3);
z = 4.4670
% analytic ans -24*exp(-5/4)+9 =4.4670

%q10
strArray = java_array('java.lang.String',3,3);
java.lang.String[][]:
[]

[]

[]

[]

[]

[]

[]

[]

[]

% quad(y,0,5)
%ans =4.4670

strArray(1,1) = java.lang.String('joe');
strArray =
java.lang.String[][]:
'joe'

[]

[]

[]

[]

[]

[]

[]

[]

strArray(2,1) = java.lang.String('sarah');
strArray =
java.lang.String[][]:
'joe'

[]

'sarah'
[]

[]

[]
[]

[]
[]

strArray(3,1) = java.lang.String('pat');
strArray(1,3) = java.lang.String('$30,000');
strArray(2,3) = java.lang.String('$150,000');
strArray(3,3) = java.lang.String('$120,000');
strArray(1,2) = java.lang.String('smith');
strArray(2,2) = java.lang.String('brown');
strArray(3,2) = java.lang.String('jackson');
cellArray=cell(strArray)
cellArray =
'joe'
'sarah'
'pat'

'smith'

'$30,000'

'brown'
'jackson'

'$150,000'
'$120,000'

%or

cellArray={ 'joe' , 'smith' , '$30,000' ;


'sarah' , 'brown' ,
'pat'

'jackson'

'$150,000';

'$120,000'};

disp(cellArray); cell Array =

'joe'

'smith'

'$30,000'

'sarah'
'pat'

'brown'
'jackson'

'$150,000'
'$120,000'

You might also like