Questions On Matlab
Questions On Matlab
(a) Write a FOR loop based program called Finite geometric series that, for
given n and x, as inputs, determines Yn, calculate its % deviation from Y and d
isplays these two results.
(b) Modify the solution of Part a in order to determine n, taking % deviation
limit as input so that % deviation (as in part a) is always lesser than the specifi
ed % deviation limit. Given, Yn(x) = 1 + x + x2 + x3 + x4 + .. + xn1.
NOTE: mod(x)<1. If this condition is not met then make sure that sum not be ca
lculated and an error message should be displayed.
Solution:
a)
Code:
%find sum
n= input ('enter the no of term');
x= input ('enter the term');
if (abs(x)>1)
disp ('error');
else
yn=(1-x^n)/(1-x);
yinf=1/(1-x);
per_dev=(yinf-yn)/yinf * 100;
disp('% deviation');
disp(' ');
disp(per_dev);
disp('yn');
disp(' ');
disp(yn);
end;
Output:
enter the no of term 5
enter the term 0.5
% deviation
3.1250
yn
1.9375
b)
Code:
%find n
per_dev=input('enter %deviation limit');
x=input('enter the term');
if(abs(x)>1)
disp('error');
else
n=log(per_dev/100)/log(x);
% per_dev = (x^n)*100
disp('n');
disp(' ');
disp(n);
end;
Output:
fgs
enter %deviation limit 3.125
enter the term 0.5
n
5
Conclusion:
Sum of the geometric series has been obtained. Percentage deviation from the sum for
infinite no of terms has been obtained.
%deviation = 3.125 for n= 5 and x=0.5
2.A unity amplitude square wave y(t) can be approximated by the sum of a sin w
ave at some fundamental frequency and its harmonics (Fourier series of a square
wave).
Write an m file square wave that generate a unity amplitude square wave by ab
ove method for n cycles of square wave at a user defined frequency f. Display
this square wave in a graph. Use adequate no. of samples in a cycle so as not to di
stort the waveform.
Solution:
Code:
% square wave generation
f=input('frequency');
t=0:0.1:(f);
y=(sin(t)+(sin(3*t)/3)+(sin(5*t)/5)+(sin(7*t)/7)+(sin(9*t)/9));
plot(t,y);
Output:
frequency 50
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
10
15
20
25
30
35
40
45
50
Conclusion:
Square wave, having frequency 50Hz, has been obtained using sine waves till ninth
harmonic. Gibbs effect has also been observed.
3.Say you are dragging a heavy dresser at constant velocity across the horizontal
floor of your new hostel room as shown in figure given below. The force you are a
pplying to the dresser has a magnitude F and it is directed at an angle above th
e horizontal. The weight of the dresser is W and the coefficient of friction betwee
n it and the floor is .Newtons law can be used to show that
0F= / (sin + cos)
Or, expressing the force as a fraction of the dressers weight , i.e., F = F/W,
where,
(a) Write a FOR loop based program called Dragging dresser that, for
a given
calculates F from = 0 to 90 in 1 increments, and then displays the resultin
g array of F values on a graph as well as in numeric form.
(b)
Run above program with = 0.1, 0.4, and = 1.0and determine the angle min
at which F is smallest in each case.
Solution
a)
Code:
%dragging dresser
u=input('enter the coefficient friction');
for x=0:1:90
f=u/(u*sin(x*pi/180)+cos(x*pi/180));
c(x+1)=f;
end;
disp(' values of F` ');
disp(' ');
disp(c);
x=0:1:90;
plot(x,c);
title('force vs angle');
xlabel('angle');
ylabel('force');
Output:
DraggingDresserA
enter the coefficient friction 0.4
values of F`
Columns 1 through 9
0.4000 0.3973 0.3947
0.3923
0.3901
0.3880
0.3860
0.3841
0.3824
Columns 10 through 18
0.3809 0.3794 0.3781
0.3769
0.3758
0.3749
0.3740
0.3733
0.3727
Columns 19 through 27
0.3722 0.3718 0.3716
0.3714
0.3714
0.3715
0.3717
0.3720
0.3724
Columns 28 through 36
0.3729 0.3736 0.3743
0.3752
0.3762
0.3774
0.3786
0.3800
0.3815
Columns 37 through 45
0.3831 0.3849 0.3867
0.3888
0.3909
0.3933
0.3957
0.3983
0.4011
Columns 46 through 54
0.4041 0.4072 0.4105
0.4139
0.4176
0.4214
0.4255
0.4297
0.4342
Columns 55 through 63
0.4389 0.4438 0.4490
0.4545
0.4602
0.4663
0.4726
0.4792
0.4862
Columns 64 through 72
0.4936 0.5013 0.5095
0.5180
0.5271
0.5366
0.5466
0.5572
0.5684
Columns 73 through 81
0.5802 0.5927 0.6059
0.6200
0.6349
0.6507
0.6676
0.6856
0.7048
Columns 82 through 90
0.7253 0.7473 0.7709
0.7963
0.8237
0.8533
0.8854
0.9203
0.9583
Column 91
1.0000
force vs angle
1
0.9
0.8
force
0.7
0.6
0.5
0.4
10
20
30
40
angle
50
60
70
80
90
b)
At =0.1
Output:
enter the coefficient friction 0.1
minimum force
0.0995
force vs angle
1
0.9
0.8
0.7
forc e
0.6
0.5
0.4
0.3
0.2
X: 6
Y: 0.0995
0.1
0
10
20
30
40
angle
50
60
70
80
90
60
70
80
90
at =0.4
Output:
enter the coefficient friction 0.1
minimum force
0.3714
force vs angle
1
0.9
forc e
0.8
0.7
0.6
0.5
X: 22
Y: 0.3714
0.4
10
20
30
40
angle
50
at =1
Output:
enter the coefficient friction 0.1
minimum force
0.7071
force vs angle
0.95
force
0.9
0.85
0.8
0.75
0.7
X: 45
Y: 0.7071
10
20
30
40
angle
50
60
70
80
90
Conclusion:
Values of F were obtained for different values of coefficient of friction and angle of
inclination. Minimum value of F= 0.0995 was obtained for = 0.1 at 6.
4.(a) Write a program called Mean of random array, which generates an array
of
100 random numbers in the range of 0 to 1, and then determines the mean of thi
s array of numbers. Display the mean.
(b) It is expected that the nominal value of Mean should be 0.500. Run Mean of
random array N times in a row, where N = 10. Due to the random manner in wh
ich the array is generated each time, you will find most of the Mean fall within a
range given by 0.500 x what value do you observe for x? What then is obser
ved percent fluctuation of the mean in this data set, where percent fluctuation is
defined as (x/0.500) 100?
(c) Automate this process taking N as input. When run, the program will deter
mine the mean of a 100 element array of random numbers N times, and save thes
e mean values in an N element array. The fluctuations of the mean can then be de
fined as the standard deviation of this array of mean values. Determine an
d name it as fluctuation and show it on command window.
Solution:
a)
a=rand (1,100)
a=
Columns 1 through 16
0.0015 0.2195 0.3881
0.1604 0.3628 0.2776
0.7244
0.5807
0.7671
0.6409
0.2407 0.0355
0.1189 0.0313
0.9523
0.5305
Columns 17 through 32
0.4808 0.1541 0.9266
0.3326 0.6935 0.1709
0.4122
0.3459
0.4038
0.3651
0.1532
0.3417
0.8751
0.9154
0.8773
0.0928
Columns 33 through 48
0.1712 0.1934 0.9662
0.9573 0.5904 0.1201
0.4610
0.2767
0.7349
0.1457
0.0868
0.7191
0.2153
0.7551
0.5706
0.3236
Columns 49 through 64
0.8479 0.1609 0.4269
0.9427 0.2389 0.6988
0.8017
0.1399
0.2064
0.4452
0.6569
0.6460
0.2902
0.0851
0.6858
0.3088
Columns 65 through 80
0.5120 0.2492 0.3753
0.6337 0.7156 0.5355
0.5897
0.0746
0.9952
0.1534
0.0384
0.6837
0.2657
0.3338
0.3022
0.2061
Columns 81 through 96
0.8420 0.9480 0.7517
0.1432 0.8201 0.1785
0.0272
0.1794
0.7441
0.1517
0.3368
0.4346
0.8014
0.8800
0.1669
0.6367
0.4440
b=mean(a)
b =0.4475
b)
Code:
%random no
n=10;
for x=1:10
a = rand(1,100);
b= mean(a);
del_x = (b-0.500);
per_fluctuation=(del_x*100/0.5);
disp('% fluctuation');
disp(' ');
disp(per_fluctuation);
end;
Output:
% fluctuation
4.6630
% fluctuation
0.2796
% fluctuation
0.8928
% fluctuation
-0.5915
% fluctuation
1.4153
% fluctuation
-0.8244
% fluctuation
1.8440
% fluctuation
-5.1838
% fluctuation
-0.8298
% fluctuation
-4.3358
c)
Code:
%random no
n=input('no of times');
for x=1:n
a = rand(1,100);
b= mean(a);
c(x)=b;
del_x = (b-0.500);
per_fluctuation=(del_x*100/0.5);
fluctuation(x)=per_fluctuation;
end;
disp('mean');
disp(' ');
disp(c);
disp('% fluctuation');
disp(' ');
disp(fluctuation);
Output:
no of times 20
mean
Columns 1 through 9
0.5280 0.4675 0.5044
0.4783
0.4929
0.5010
0.5183
0.4549
0.4310
Columns 10 through 18
0.5121 0.5147 0.5219
0.5290
0.5409
0.5053
0.4343
0.5017
0.5270
Columns 19 through 27
0.4968 0.5285 0.1027
0.1032
0.1037
0.1042
0.1048
0.1054
0.1061
Columns 28 through 36
0.1068 0.1075 0.1083
0.1092
0.1101
0.1110
0.1120
0.1130
0.1141
Columns 37 through 45
0.1152 0.1164 0.1177
0.1190
0.1204
0.1219
0.1234
0.1251
0.1268
Columns 46 through 54
0.1286 0.1304 0.1324
0.1345
0.1367
0.1390
0.1414
0.1440
0.1467
Columns 55 through 63
0.1495 0.1526 0.1557
0.1591
0.1627
0.1665
0.1705
0.1747
0.1793
Columns 64 through 72
0.1841 0.1893 0.1948
0.2008
0.2071
0.2140
0.2214
0.2294
0.2380
0.2814
0.2950
0.3102
0.3271
0.3461
0.3675
0.4902
0.5354
0.5899
0.6570
0.7416
0.8515
Columns 73 through 81
0.2474 0.2577 0.2690
Columns 82 through 90
0.3918 0.4198 0.4522
Column 91
1.0000
% fluctuation
[5.5989 -6.5093 0.8889 -4.3385 -1.4232 0.2071 3.6540 -9.0230 -13.8096
2.4199 2.9443 4.3741 5.8013 8.1812 1.0600 -13.1343 0.3489 5.4012
-0.6430 5.7051]
Conclusion:
Mean of 100 random values between 0 and 1 was obtained. Deviation of mean from
0.5 was obtained.
5. Compute the Fourier spectrum of the following sum of the two sinusoid signals
Y (t) = A1sin(2f1t) + A2sin(2f2t)
Where f1= 10kHz and f2= 12kHz where sampling frequency f s= 40kHz.
Solution:
Code:
fs=40000;
ts=1/fs;
n=200;
t=[0:n-1]*ts;
x=sin(2*pi*10000*t)+sin(2*pi*12000*t);
nfft=2^nextpow2(n);
Xf=fft(x,nfft)/n;
f=fs/2*linspace(0,1,nfft/2+1);
plot(f,2*abs(Xf(1:nfft/2+1)))
ylabel('amplitude')
xlabel('frequency')
title('fourier spectrum')
Output:
fourier spectrum
1
0.9
0.8
amplitude
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0.2
0.4
0.6
0.8
1
1.2
frequency
1.4
1.6
1.8
2
4
x 10
Conclusion:
The Fourier spectrum of y (t) has been obtained having peaks at 10 and 12 KHz.
Add
11.5s+1
Scope
Transfer Fcn
Step
simout
To Workspace
Output:
liquid level
1.4
1.2
m a g n it u d e
1
0.8
0.6
0.4
0.2
0
10
20
30
time
40
50
60
Conclusion:
Liquid level was checked with time. Level increases till 20min then tries to remain
constant.
Product
Add
Integrator 1
Scope
Product 1
Integrator
simout
1
s
To Workspace
Constant 1
0.018
4
distance
velocity
3
2
magnitude
1
0
-1
-2
-3
-4
10
20
30
Conclusion:
Velocity follows a square wave.
Distance follows a triangular wave.
40
time
50
60
70
80
-100
Magnitude (dB)
-120
-140
-160
-180
-200
Phase (deg)
-220
180
135
90
-6
10
-5
10
-4
10
Frequency (rad/sec)
-3
10
-2
10
9. Calculate the range that a ball would travel when it is thrown with an initial
velocity of v0of 20m/s at angle . Calculate this range for all angles between 0
and 90 in steps. Determine the angle that will result in the maximum range for
the ball. Plot the trajectory of the ball for angles between 5 and 85 increments.
Plot the maximum-range trajectory in a different colour and with a thicker line.
Assume that there is no air friction.
Solution:
a)
Code:
v=20;
g=9.8;
max=0;
for i=0:90
range=v*v*sin(2*i*pi/180)/g;
if(max<range)
max=range;
maxi=i;
end;
fprintf('Angle %d',i);
display(range);
end;
fprintf('Maximum Range is %d at the angle of %d degrees',max,maxi);
Solution:
Angle 0
Range = 0
Angle 1
Range = 1.4245
Angle 2
Range = 2.8472
Angle 3
Range = 4.2665
Angle 4
Range = 5.6805
Angle 5
Range = 7.0877
Angle 6
Range = 8.4862
Angle 7
Range = 9.8744
Angle 8
Range = 11.2505
Angle 9
Range = 12.6129
Angle 10
Range = 13.9600
Angle 11
Range = 15.2901
Angle 12
Range = 16.6015
Angle 13
Range = 17.8927
Angle 14
Range = 19.1621
Angle 15
Range = 20.4082
Angle 16
Range = 21.6294
Angle 17
Range = 22.8242
Angle 18
Range = 23.9912
Angle 19
Range = 25.1290
Angle 20
Range = 26.2362
Angle 21
Range = 27.3115
Angle 22
Range = 28.3534
Angle 23
Range = 29.3608
Angle 24
Range = 30.3324
Angle 25
Range = 31.2671
Angle 26
Range = 32.1637
Angle 27
Range = 33.0211
Angle 28
Range = 33.8383
Angle 29
Range = 34.6142
Angle 30
Range = 35.3480
Angle 31
Range = 36.0387
Angle 32
Range = 36.6855
Angle 33
Range = 37.2876
Angle 34
Range = 37.8442
Angle 35
Range = 38.3548
Angle 36
Range = 38.8186
Angle 37
Range = 39.2352
Angle 38
Range = 39.6039
Angle 39
Range = 39.9244
Angle 40
Range = 40.1962
Angle 41
Range = 40.4191
Angle 42
Range = 40.5927
Angle 43
Range = 40.7169
Angle 44
Range = 40.7915
Angle 45
Range = 40.8163
Angle 46
Range = 40.7915
Angle 47
Range = 40.7169
Angle 48
Range = 40.5927
Angle 49
Range = 40.4191
Angle 50
Range = 40.1962
Angle 51
Range = 39.9244
Angle 52
Range = 39.6039
Angle 53
Range = 39.2352
Angle 54
Range = 38.8186
Angle 55
Range = 38.3548
Angle 56
Range = 37.8442
Angle 57
Range = 37.2876
Angle 58
Range = 36.6855
Angle 59
Range = 36.0387
Angle 60
Range = 35.3480
Angle 61
Range = 34.6142
Angle 62
Range = 33.8383
Angle 63
Range = 33.0211
Angle 64
Range = 32.1637
Angle 65
Range = 31.2671
Angle 66
Range = 30.3324
Angle 67
Range = 29.3608
Angle 68
Range = 28.3534
Angle 69
Range = 27.3115
Angle 70
Range = 26.2362
Angle 71
Range = 25.1290
Angle 72
Range = 23.9912
Angle 73
Range = 22.8242
Angle 74
Range = 21.6294
Angle 75
Range = 20.4082
Angle 76
Range = 19.1621
Angle 77
Range = 17.8927
Angle 78
Range = 16.6015
Angle 79
Range = 15.2901
Angle 80
Range = 13.9600
Angle 81
Range = 12.6129
Angle 82
Range = 11.2505
Angle 83
Range = 9.8744
Angle 84
Range = 8.4862
Angle 85
Range = 7.0877
Angle 86
Range = 5.6805
Angle 87
Range = 4.2665
Angle 88
Range = 2.8472
Angle 89
Range = 1.4245
Angle 90
Range = 4.9986e-015
Maximum Range is 4.081633e+001 at the angle of 45 degrees
b)
Code:
v=20;
g=9.8;
max=0;
a=1;
for i=1:90
subplot(6,3,a);
range=v*v*sin(2*i*pi/180)/g;
if(max<range)
max=range;
maxi=i;
end;
if(mod(i,5)==0)
fprintf('Theta %d',i);
display(range);
for x=0:.01:range
y=x*tan(i*pi/180)-g*x*x/(2*v*v*cos(i*pi/180)*cos(i*pi/180));
plot(x,y);
xlabel('range');
ylabel('velocity');
hold on;
end;
a=a+1;
end;
end;
fprintf('Maximum Range is %d at the angle of %d degrees',max,maxi);
Output:
range6
Theta 5
Range = 7.0877
Theta 10
Range = 13.9600
Theta 15
Range = 20.4082
Theta 20
Range = 26.2362
Theta 25
Range = 31.2671
Theta 30
Range = 35.3480
Theta 35
Range = 38.3548
Theta 40
Range = 40.1962
Theta 45
Range = 40.8163
Theta 50
Range = 40.1962
Theta 55
Range = 38.3548
Theta 60
Range = 35.3480
Theta 65
Range = 31.2671
Theta 70
Range = 26.2362
Theta 75
Range = 20.4082
Theta 80
Range = 13.9600
Theta 85
Range = 7.0877
Theta 90
Range = 4.9986e-15
Maximum Range is 4.081633e+01 at the angle of 45 degrees
10
range
20
40
20
0
velocity
40
40
velocity
20
range
20
10
0
range
20
range
10
5
0
50
velocity
velocity
50
20
10
0
velocity
40
20
20
10
0
20
range
40
velocity
20
range
10
5
0
10
range
20
10
0
20
range
40
velocity
40
4
2
0
2
1
0
20
10
0
5
range
10
velocity
20
10
0
20
range
velocity
20
10
0
10
velocity
velocity
velocity
20
10
0
5
range
velocity
10
5
0
1
0.5
0
velocity
4
2
0
velocity
velocity
velocity
velocity
0.2
0.1
0
1
0
-1
-1
range
20
range
40
20
range
40
range
50
20
range
40
20
range
40
0
range
c)
Code:
v=20;
g=9.8;
imax=45;
i=input(' enter angle at which range and maximum range is to be drawn ');
range=v*v*sin(2*i*pi/180)/g;
rangemax=v*v*sin(2*imax*pi/180)/g;
for x=0:.01:range
y=x*tan(i*pi/180)-g*x*x/(2*v*v*cos(i*pi/180)*cos(i*pi/180));
plot(x,y,':m');
xlabel(' range');
ylabel(' velocity ');
title(' projectile motion ');
hold on;
end;
for x=0:.01:rangemax
y=x*tan(imax*pi/180)-g*x*x/(2*v*v*cos(imax*pi/180)*cos(imax*pi/180));
plot(x,y,'--r');
hold on;
end;
Output:
enter angle at which range and maximum range is to be drawn 30
projectile motion
12
10
velocity
10
15
20
25
range
30
35
40
45
Conclusion:
Parabolic projectile motion has been obtained for different angles of projection.
Step
Scope
Transfer Fcn
simout
To Workspace
transient response
7
6
m a g n itu d e
5
4
3
2
1
0
0
10
20
30
40
time
50
60
70
0.045
Step
s2 +0.025 s+0.045
Transfer Fcn
Scope
simout
To Workspace
80
90
transient response
2
1.8
1.6
m agnitude
1.4
1.2
1
0.8
0.6
0.4
0.2
0
10
20
30
40
time
50
60
70
80
Step
Scope
Transfer Fcn
simout
To Workspace
step response
30
25
20
m gnitude
15
10
5
0
-5
-10
-15
3
time
6
5
x 10