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

EXPERIMENT - 5

AIM:
Creating a vector X with elements, Xn = (-1)n+1/(2n-1) and Adding up 100 elements of the
vector, X; And, plotting the functions, x, x3, ex, exp(x2) over the interval 0 < x < 4 (by choosing
appropriate mesh values for x to obtain smooth curves), on A Rectangular Plot
SOFTWARE USED: MATLAB 7.12.0(R2011a)

PROCEDURE:

>> % Creating Vector X

>> for n=1:100


X(n)=((-1)^(n+1))/(2*n-1)
end

X=
Columns 1 through 9
1.0000 -0.3333 0.2000 -0.1429 0.1111 -0.0909 0.0769 -0.0667 0.0588
Columns 10 through 18
-0.0526 0.0476 -0.0435 0.0400 -0.0370 0.0345 -0.0323 0.0303 -0.0286
Columns 19 through 27
0.0270 -0.0256 0.0244 -0.0233 0.0222 -0.0213 0.0204 -0.0196 0.0189
Columns 28 through 36
-0.0182 0.0175 -0.0169 0.0164 -0.0159 0.0154 -0.0149 0.0145 -0.0141
Columns 37 through 45
0.0137 -0.0133 0.0130 -0.0127 0.0123 -0.0120 0.0118 -0.0115 0.0112
>> % Adding elements of vector X
>> Y=sum(X(n))
Y=
-0.0050

>> % Creating interval vector Z


>> Z=0.04:0.04:4
Z=

Columns 1 through 9
0.0400 0.0800 0.1200 0.1600 0.2000 0.2400 0.2800 0.3200 0.3600
Columns 10 through 18
0.4000 0.4400 0.4800 0.5200 0.5600 0.6000 0.6400 0.6800 0.7200
Columns 19 through 27
0.7600 0.8000 0.8400 0.8800 0.9200 0.9600 1.0000 1.0400 1.0800
Columns 28 through 36
1.1200 1.1600 1.2000 1.2400 1.2800 1.3200 1.3600 1.4000 1.4400
>> % Plotting Functions
>> % Plotting X
>> plot(Z,X)
>> xlabel('Z')
>> ylabel('X')
>> title('Plotting X')
Plotting X
1

0.8

0.6

0.4
X

0.2

-0.2

-0.4
0 0.5 1 1.5 2 2.5 3 3.5 4
Z

>> % Plotting X^3


>> A=X.^3
A=

Columns 1 through 9
1.0000 -0.0370 0.0080 -0.0029 0.0014 -0.0008 0.0005 -0.0003 0.0002
Columns 10 through 18
-0.0001 0.0001 -0.0001 0.0001 -0.0001 0.0000 -0.0000 0.0000 -0.0000
Columns 19 through 27
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
Columns 28 through 36
-0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000
Columns 37 through 45
0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000

>> plot(Z,A)
>> xlabel('Z')
>> ylabel('X^3')
>> title('Plotting X^3')
Plotting X3
1.2

0.8

0.6
X3

0.4

0.2

-0.2
0 0.5 1 1.5 2 2.5 3 3.5 4
Z

>> % Plotting e^X


>> A=exp(X)
A=
Columns 1 through 9
2.7183 0.7165 1.2214 0.8669 1.1175 0.9131 1.0800 0.9355 1.0606
Columns 10 through 18
0.9487 1.0488 0.9575 1.0408 0.9636 1.0351 0.9683 1.0308 0.9718
Columns 19 through 27
1.0274 0.9747 1.0247 0.9770 1.0225 0.9789 1.0206 0.9806 1.0190
Columns 28 through 36
0.9820 1.0177 0.9832 1.0165 0.9843 1.0155 0.9852 1.0146 0.9860

>> plot(Z,A)
>> xlabel('Z')
>> ylabel('e^X')
>> title('Plotting e^X')
Plotting eX
3

2.5

2
eX

1.5

0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
Z

>> % Plotting e^(X^2)

>> A=exp(X.^2)
A=

Columns 1 through 9
2.7183 1.1175 1.0408 1.0206 1.0124 1.0083 1.0059 1.0045 1.0035
Columns 10 through 18
1.0028 1.0023 1.0019 1.0016 1.0014 1.0012 1.0010 1.0009 1.0008
Columns 19 through 27
1.0007 1.0007 1.0006 1.0005 1.0005 1.0005 1.0004 1.0004 1.0004
Columns 28 through 36
1.0003 1.0003 1.0003 1.0003 1.0003 1.0002 1.0002 1.0002 1.0002

>> plot(Z,A)
>> xlabel('Z')
>> ylabel('e^(X^2)')
>> title('Plotting e^(X^2)')
Plotting e(X2)
2.8

2.6

2.4

2.2

e(X2)
1.8

1.6

1.4

1.2

1
0 0.5 1 1.5 2 2.5 3 3.5 4
Z

DISCUSSION:
The vector was constructed using for loop and the given formula. And sum of all elements was
calculated using sum ().Then functions x, x3, ex, exp(x2) were plotted over the interval 0 < x < 4
using plot().

RESULT:
Vector x was created and subsequently x, x3, ex, exp(x2) were plotted over the interval 0 < x < 4.

CONCLUSION:
Several functions (x, x3, ex, exp(x2)) were successfully plotted for a vector X.

You might also like