In: Electrical Engineering
Write a MATLAB function that will generate chi-square random variables with v degrees of freedom by generating v standard normal, squaring them and then adding them up. This uses the fact that is chi-square with v degrees of freedom. Generate some random variables and plot in a histogram. The degrees of freedom should be an input argument set by the user.
Matlab Code:
clc;clear;
v = input('Enter the value of v \n')
R = randn(1,v)
for i = 1:1:length(R)
X(i) = 0;
for j = 1:1:i
X(i) = X(i) + R(i)^2;
end
end
X
figure;
hist(X)
grid on
---------------------------------
Command Window:
----------------------------------------
Enter the value of v
100
v =
100
R =
Columns 1 through 12
-0.2444 -1.4851 -0.3562 0.3545 1.8349 0.8977 0.2523 0.9840 -0.5146 -1.3107 -0.6873 -1.1855
Columns 13 through 24
0.9824 -0.1353 -1.1900 -0.2073 -0.8456 -0.3143 1.4243 1.4915 -0.7440 -0.3744 0.4241 0.1141
Columns 25 through 36
-0.5111 1.0223 -1.4192 2.5467 -0.3580 0.4013 -0.7062 -1.0207 1.1340 0.0385 0.2309 0.1257
Columns 37 through 48
0.6963 1.7395 0.6654 0.6875 1.2390 0.8987 -1.1501 -0.2642 -1.6252 0.5816 1.9025 1.5801
Columns 49 through 60
0.9210 -0.9379 -0.7372 0.1798 -0.1318 0.8679 1.3144 -0.2511 -0.0718 1.0952 0.4893 -1.6802
Columns 61 through 72
-0.8115 -0.7522 -1.3259 -0.1815 0.1314 1.3938 0.7062 1.1628 -0.7537 0.3219 1.6357 -0.6478
Columns 73 through 84
0.6733 1.2736 -1.0398 0.4880 -0.9068 -0.4747 -0.6661 -0.5020 -0.3089 -0.8369 -0.6283 0.5009
Columns 85 through 96
1.6703 -1.5417 -0.2720 0.3416 0.4844 0.9527 0.1314 -1.7419 0.6678 0.8982 -0.5493 -0.3175
Columns 97 through 100
-0.5827 -0.1642 -0.9351 1.1881
X =
Columns 1 through 12
0.0597 4.4108 0.3806 0.5027 16.8345 4.8351 0.4456 7.7465 2.3830 17.1781 5.1964 16.8639
Columns 13 through 24
12.5461 0.2565 21.2429 0.6876 12.1561 1.7781 38.5429 44.4901 11.6258 3.0835 4.1369 0.3125
Columns 25 through 36
6.5305 27.1745 54.3802 181.5961 3.7158 4.8317 15.4600 33.3361 42.4336 0.0505 1.8662 0.5691
Columns 37 through 48
17.9381 114.9792 17.2664 18.9080 62.9368 33.9190 56.8772 3.0708 118.8545 15.5622 170.1155 119.8415
Columns 49 through 60
41.5664 43.9831 27.7156 1.6807 0.9211 40.6780 95.0194 3.5314 0.2935 69.5700 14.1243 169.3750
Columns 61 through 72
40.1730 35.0822 110.7579 2.1079 1.1226 128.2223 33.4154 91.9505 39.1951 7.2545 189.9625 30.2139
Columns 73 through 84
33.0897 120.0238 81.0906 18.0952 63.3096 17.5768 35.0501 20.1619 7.7283 57.4331 32.7676 21.0760
Columns 85 through 96
237.1285 204.4161 6.4370 10.2716 20.8804 81.6846 1.5700 279.1521 41.4692 75.8283 28.6660 9.6774
Columns 97 through 100
32.9346 2.6426 86.5729 141.1599
>>
------------------------------------