Aim: Compute The Running Sum (Check With Sum), Where Running Sum For Element J Tool Used: Matlab Version 7.0.0.19920 Procedure

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

EXP 3-B

Aim: Compute the Running Sum (Check with sum), where Running Sum for element j
= the sum of the elements from 1 to j, inclusive.
Tool Used : Matlab version 7.0.0.19920
Procedure
>> sum=0

sum =

>> for i=1 :length(X)


sum=sum+X(i)
end

sum =

sum =

sum =

12

sum =

16

sum =

23

sum =

31

sum =
40

sum =

74

sum =

139

Result: loop was created successfully.


EXP 3-C

Aim: Generating a Random Sequence using rand() / randn() functions and plot them.
Tool Used : Matlab version 7.0.0.19920
Procedure

>> rand(5)

ans =

0.9501 0.7621 0.6154 0.4057 0.0579


0.2311 0.4565 0.7919 0.9355 0.3529
0.6068 0.0185 0.9218 0.9169 0.8132
0.4860 0.8214 0.7382 0.4103 0.0099
0.8913 0.4447 0.1763 0.8936 0.1389

>> randn(5)

ans =

-0.4326 1.1909 -0.1867 0.1139 0.2944


-1.6656 1.1892 0.7258 1.0668 -1.3362
0.1253 -0.0376 -0.5883 0.0593 0.7143
0.2877 0.3273 2.1832 -0.0956 1.6236
-1.1465 0.1746 -0.1364 -0.8323 -0.6918

Result : random sequence was generated successfully.


EXP 4-A

Aim : Evaluating a given expression and rounding it to the nearest integer value using
Round, Floor, Ceil and Fix functions
Tool Used : Matlab version 7.0.0.19920
Procedure
ciel-Rounds off to next greatest integer
floor- rounds of to previous integer
Fix-truncates the decimal value of the integer

>> round(99.8)

ans =

100

>> round(99.2)

ans =

99

>> round(-99.8)

ans =

-100

>> round(-99.2)

ans =

-99
>> ceil(99.8)

ans =

100

>> ceil(99.2)
ans =

100

>> ceil(-99.2)

ans =

-99

>> ceil(-99.8)

ans =

-99
>> floor(99.8)

ans =

99

>> floor(99.2)

ans =

99

>> floor(-99.8)

ans =

-100

>> floor(-99.2)

ans =

-100

>> fix(99.8)
ans =

99

>> fix(43.2)

ans =

43

>> fix(-43.2)

ans =

-43
Result : expressions were evaluated successfully.
EXP 4-B
Aim: Also, generating and Plots of (A)
Trigonometric Functions - sin(t),cos(t), tan(t), sec(t), cosec(t) and cot(t) for a given
duration, t.
Tool Used : Matlab version 7.0.0.19920
Procedure :
>> x=-pi:0.1:pi
x=

Columns 1 through 16
-3.1416 -3.0416 -2.9416 -2.8416 -2.7416 -2.6416 -2.5416 -2.4416 -2.3416
-2.2416 -2.1416 -2.0416 -1.9416 -1.8416 -1.7416 -1.6416
Columns 17 through 32
-1.5416 -1.4416 -1.3416 -1.2416 -1.1416 -1.0416 -0.9416 -0.8416 -0.7416
-0.6416 -0.5416 -0.4416 -0.3416 -0.2416 -0.1416 -0.0416
.
.
.
>> y=sin(x)
y=
Columns 1 through 16
-0.0000 -0.0998 -0.1987 -0.2955 -0.3894 -0.4794 -0.5646 -0.6442 -0.7174
-0.7833 -0.8415 -0.8912 -0.9320 -0.9636 -0.9854 -0.9975
Columns 17 through 32
-0.9996 -0.9917 -0.9738 -0.9463 -0.9093 -0.8632 -0.8085 -0.7457 -0.6755
-0.5985 -0.5155 -0.4274 -0.3350 -0.2392 -0.1411 -0.0416
.
.
.
>> plot(x,y)
>> y=cos(x)
y=
Columns 1 through 16
-1.0000 -0.9950 -0.9801 -0.9553 -0.9211 -0.8776 -0.8253 -0.7648 -0.6967
-0.6216 -0.5403 -0.4536 -0.3624 -0.2675 -0.1700 -0.0707
Columns 17 through 32
0.0292 0.1288 0.2272 0.3233 0.4161 0.5048 0.5885 0.6663 0.7374
0.8011 0.8569 0.9041 0.9422 0.9710 0.9900 0.9991
.
.
.
>> plot(x,y)
>> y=tan(x)

y=

Columns 1 through 16

0.0000 0.1003 0.2027 0.3093 0.4228 0.5463 0.6841 0.8423 1.0296


1.2602 1.5574 1.9648 2.5722 3.6021 5.7979 14.1014

Columns 17 through 32

-34.2325 -7.6966 -4.2863 -2.9271 -2.1850 -1.7098 -1.3738 -1.1192 -0.9160


-0.7470 -0.6016 -0.4727 -0.3555 -0.2464 -0.1425 -0.0416
.
.
.

>> plot(x,y)
File :sin.m
x=-pi:0.1:pi
plot(x,sin(x))
xlabel('time axis')
ylabel('sin(X)')
title('plot for sinx')

Result : Graphs of trigonometric functions were plotted successfully.


EXP 4-C
Aim: Logarithmic and other Functions log(A), log10(A), Square root of A, Real n th root
of A
Tool Used : Matlab version 7.0.0.19920
Procedure
File : log.m
A=1:1:20
subplot(2,2,1)
plot(A,log(A))
xlabel('A')
ylabel('Log(A)')
title('Logrithm')
subplot(2,2,2)
plot(A,log10(A))
xlabel('A')
ylabel('Log10(A)')
title('Logrithm with base 10')
subplot(2,2,3)
plot(A,sqrt(A))
xlabel('A')
ylabel('sqrt(A)')
title('square root')
subplot(2,2,4)
plot(A,nthroot(A,5))
xlabel('A')
ylabel('nthroot(A)')
title('nth root ')
Result: Graphs of the given functions were plotted successfully.
Exp 5
Aim : Creating a vector X with elements, Xn = (-1)n+1/(2n-1) and Adding up 100
elements of the vector, X
x(n)=(-1)^n/(2n-1)
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
Tool Used : Matlab version 7.0.0.19920
Procedure:
>> for n=1:1:100
x(n)=((-1)^(n+1))/((2*n)-1)
end
>> z=0.4:0.04:4
>>plot(z,x)

Result : vector was created successfully and graph was plotted successfully.
Exp 6
Aim : Generate a sine wave and plot using graphical enhancements
Eg: multiple plot,legend,title,lable,adding text,special symbol.color etc
Tool Used : Matlab version 7.0.0.19920
Procedure :
t=0:0.01*pi:2*pi
y=sin(t)
plot(t,y,'-r')
xlabel('x axis')
ylabel('function')
title('multiple graph')
legend('sin t')
hold on
y1=sin(3*t)
plot(t,y1,'-b')
legend('sin t','sin 3t')

Result: sin wave was plotted successfully with graphical enhancements.


Exp 7
Aim : To solve an ordinary differential eq of 1st order.
x(dy/dx)+2y=x^3
Tool Used : Matlab version 7.0.0.19920
Procedure :
1<x<3
y=4.2
eq1=@(x,y)((x^3-2*y)/x)

eq1 =

@(x,y)((x^3-2*y)/x)

>> [x,y]=ode45(eq1,[1:0.01:3],4.2)

plot(x,y)

Result : equation was solved successfully.


Exp 9
Aim : Generate and plot a square wave from sum of sine waves using Fourier series
axpansion
s(t)=(4*A/pi)(sin t + sin3t/3+sin 5t/5+.....)
Tool Used : Matlab version 7.0.0.19920
Procedure:
t=0:0.05*pi:2*pi
y=sin(t)
x=0
for n=1:2:100
y1=sin(n*t)/n
x=x+y1
end
A=3
z=4*A*x/pi
plot(t,z,'-r')
hold on
plot(t,y,'-b')
legend('square wave','sin t')

Result : graph of sine square wave was plotted successfully.

You might also like