Problem 11.9
Nasser Abbasi
I ran the program 3 times, each time for different ranges of random variables. Each ranges matches the range specificed in problem 11.8 parts a,b and c.
So, the program asks the user for which part to run. If part a is selected, then the range used for the random numbers is 0..1, if part b is selected then the range is 0..infinity, and if part c is selected, then the range used is –infinity..infinity.
I use +- 100 to represent infinity.
So, this below shows the 3 runs. Each time it is run, it shows the 6 moments for N=10,100 and 10,000.
For comparison to part a, I see that the numerical approximation gets closer to analytical solution as N is increased. For N=10,000 for example, the first moment is 0.505851 compared to 0.5 analytical, and second moment is 0.333982 compared to 1/3 analytical.
For comparison to part b, the analytical solution gave LAMBDA for first moment and 2*LAMBDA^2 for second moment. If I assume LAMBDA=50, then at N=10,000 the program gave LAMBDA=50.585066, but for the second moment, I expected to get something close to 5,000 since this is what 2*50^2 is, but the program gave 3,339. Maybe the convergance is not fast in this case?
For comparison with part c, the analytical solution when mu=0 gave the first moment as 0 and the second moment as sigma^2. Running the program, I see for N=10,000 that the first moment is going to zero indeed.
» problem_11_9
solves problem 11.9 in book
Nasser Abbasi
Enter a or b or c parts from 11.8 to solve for numerically: 'a'
moment 1 for part a when N=10 =0.566857
moment 2 for part a when N=10 =0.527903
moment 3 for part a when N=10 =0.084884
moment 4 for part a when N=10 =0.197667
moment 5 for part a when N=10 =0.119564
moment 6 for part a when N=10 =0.108071
moment 1 for part a when N=100 =0.510070
moment 2 for part a when N=100 =0.313264
moment 3 for part a when N=100 =0.268097
moment 4 for part a when N=100 =0.201896
moment 5 for part a when N=100 =0.163267
moment 6 for part a when N=100 =0.141122
moment 1 for part a when N=10000 =0.505851
moment 2 for part a when N=10000 =0.333982
moment 3 for part a when N=10000 =0.251965
moment 4 for part a when N=10000 =0.203513
moment 5 for part a when N=10000 =0.169042
moment 6 for part a when N=10000 =0.138806
»
» problem_11_9
solves problem 11.9 in book
Nasser Abbasi
Enter a or b or c parts from 11.8 to solve for numerically: 'b'
moment 1 for part b when N=10 =56.685705
moment 2 for part b when N=10 =5279.026059
moment 3 for part b when N=10 =84883.977329
moment 4 for part b when N=10 =19766700.521324
moment 5 for part b when N=10 =1195638748.756388
moment 6 for part b when N=10 =108071460392.877720
moment 1 for part b when N=100 =51.006979
moment 2 for part b when N=100 =3132.637115
moment 3 for part b when N=100 =268096.906575
moment 4 for part b when N=100 =20189632.329101
moment 5 for part b when N=100 =1632665688.281873
moment 6 for part b when N=100 =141122467921.472350
moment 1 for part b when N=10000 =50.585066
moment 2 for part b when N=10000 =3339.821404
moment 3 for part b when N=10000 =251964.890258
moment 4 for part b when N=10000 =20351348.243054
moment 5 for part b when N=10000 =1690416560.356563
moment 6 for part b when N=10000 =138806093701.635190
»
» problem_11_9
solves problem 11.9 in book
Nasser Abbasi
Enter a or b or c parts from 11.8 to solve for numerically: 'c'
moment 1 for part c when N=10 =13.371410
moment 2 for part c when N=10 =3893.480889
moment 3 for part c when N=10 =-252285.690056
moment 4 for part c when N=10 =18688477.533827
moment 5 for part c when N=10 =-631285505.582327
moment 6 for part c when N=10 =44379320039.771805
moment 1 for part c when N=100 =2.013959
moment 2 for part c when N=100 =3310.739300
moment 3 for part c when N=100 =38405.540366
moment 4 for part c when N=100 =16473346.298195
moment 5 for part c when N=100 =-267896939.293912
moment 6 for part c when N=100 =150281297760.044340
moment 1 for part c when N=10000 =1.170132
moment 2 for part c when N=10000 =3347.817805
moment 3 for part c when N=10000 =736.245324
moment 4 for part c when N=10000 =20322193.440434
moment 5 for part c when N=10000 =40758028.654677
moment 6 for part c when N=10000 =140197535497.539060
»
function problem_11_9()
% solves
problem 11.9 in book
% Nasser
Abbasi
clear all; help problem_11_9;
%
design:
% for
each range from problem 11.8, we find
% a set
of random numbers in the range.
% for
part(a) in 11.8 the range is 0..1
% for
part(b) in 11.8 range is 0..infinity
% for
part(c) in 11.8 tange is -infinity..infinity
% Since
I do not know how to find random number
% all
the way to infinity, I picked +-1000 to
%
represent infintify.
rand('state',0);
part = input('Enter a or b or c parts from 11.8 to solve for
numerically: ');
switch part
case 'a'
upperRange = 1;
lowerRange = 0;
process(part,upperRange,lowerRange);
case 'b'
upperRange = 100;
lowerRange = 0;
process(part,upperRange,lowerRange);
case 'c'
upperRange = 100;
lowerRange = -100;
process(part,upperRange,lowerRange);
end
%%%%%%%%%%%%%%%%%%%%%%%
%
%
%%%%%%%%%%%%%%%%%%%%%%%
function process(part,upperRange,lowerRange)
numbers=[10 1e2 1e4];
for(k=1:length(numbers))
N=numbers(k);
for(i=1:6)
moment(i)=0;
for(j=1:N)
randomNumber = rand(1);
% map the random
number to fall into the required actuall range.
%
% 1<--X------>0
%
% upper range<----X'-------------->lower
range
%
newRange
= upperRange-lowerRange;
randomNumber = randomNumber * newRange;
randomNumber = randomNumber + lowerRange;
moment(i)
= moment(i)+ randomNumber^i;
end
moment(i) = moment(i)/N;
fprintf('moment %d for
part %c when N=%d =%f\n',i,part,N,moment(i));
end
fprintf('\n');
end