Generally I’m not in the habit of answering questions that clearly prove you haven’t tried anything yourself. Today is no different, but I’ll do the following:
I’m gonna provide you with a little code, that contains a few intentional errors. It’s up to you to figure out what the code does, and where the problems are.
Type help <command>
or doc <command>
in the Matlab command prompt to get more information on a specific command, for example:
>> help rand
will give you a wealth of information on the rand
function. Now, without further ado:
%%# normal distribution
nvars = 1e6;
N = randn(nvars,1);
f = @(x) 1/sqrt(2*pi) * exp( -x^2 );
figure(1), clf, hold on
[n, x] = hist(N, 50);
bar(x, n)
x = -10:10;
plot(x, f(x), 'r')
%%# uniform distribution
nvars = 1e6;
U = rand(nvars,1);
g = @(x) x>=0&x<=1;
figure(2), clf, hold on
[n, x] = hist(U, 2);
bar(x, n)
x = -1.5:1.5;
plot(x, g(x), 'r')
NOTE: After fixing the errors, it’s up to you if you consider this “proof” or not. If I were a high school teacher, I might, but if I were a professor, I certainly wouldn’t 🙂
4
solved Generate Gaussian and Uniform Random Variable [closed]