If you run the script above a number of times, you will find you get different answers because the numbers chosen are random. Modify your script so that we will repeat the experiment in 1a 1000 times and make a histogram of the results (Hint: use a

3 visualizaciones (últimos 30 días)
To solve this question I need to provide information from Question 1.
Question 1 states "Write a script that determines how many random numbers have to be drawn from a uniform distribution between 0 and 1 (hint: use rand to draw the numbers) to get a sum greater than 10 (Hint: use a while loop). Please comment your program to clearly indicate which variable contains the answer.
I solved question 1 by using this code:
sum_rn = 0;
itr = 0;
while sum_rn < 10
sum_rn = rand + sum_rn;
itr = itr + 1;
end
But I do not know how to solve the second question.

Respuesta aceptada

Image Analyst
Image Analyst el 3 de Feb. de 2018
Question 1 is still not solved because you did not do this part: "Please comment your program".
For question 2 you take question 1 and modify it by putting it into a loop. You have another loop for 1000 times where you call that question 1 code and store the value
for k = 1 : 1000
% Existing itr computation.
allValues(k) = itr; % Save this value of itr
end
Then, like it directed you to do, simply call histogram() passing it your allValues array. Sorry, since it's so trivial I don't know how to give you a hint without practically doing it. You only need to add 4 lines of code. If you want you can add more code to fancy up the histogram like by putting on labels, titles, grid, etc.
  2 comentarios
Kelark Azer Habashi
Kelark Azer Habashi el 3 de Feb. de 2018
Editada: Image Analyst el 3 de Feb. de 2018
by existing itr comutation do you mean that i should include:
sum_rn = rand + sum_rn;
itr = itr + 1;
or just
itr = itr + 1;
by itself?
Image Analyst
Image Analyst el 3 de Feb. de 2018
I mean you copy and paste ALL of your code from your question1 into the inside of the loop:
for k = 1 : 1000
% Your existing itr computation from Question 1.
sum_rn = 0;
itr = 0;
while sum_rn < 10
sum_rn = rand + sum_rn;
itr = itr + 1;
end
% New code to add for Question 2:
allValues(k) = itr; % Save this value of itr
end
histogram(allValues);
grid on;

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by