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 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Image Analyst
2018 年 2 月 3 日
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 件のコメント
Image Analyst
2018 年 2 月 3 日
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;
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!