Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP WITH MATLAB SCRIPT Write a function called getFive that returns the number

ID: 2268565 • Letter: H

Question

HELP WITH MATLAB SCRIPT

Write a function called getFive that returns the number of iterations required for one such experiment.

Once you’ve got your function working, let’s use it to see what happens if we conduct the experiment several times, say 20 times. Let’s run it 20 times and calculate the average number of iterations needed to get a five.

We can use a script to do this. Since we know the number of times we want to run the experiment, we should use a ________ loop. (You fill in the blank.)

We can use an array to save the values that are returned by our getFive function in each iteration. We can prefill an array of 20 zeros using the command zeros(1,20)to create a row array. Let’s call our array AllIters. So to initialize the array, we will use the assignment statement

AllIters = zeros(1,20)

before we start the loop. Then during each iteration in our loop, we’ll replace one of the 0’s in the array with the number of iterations it took to get a five during that run of the experiment. We can do this for the i th iteration of our loop by using the assignment statement

AllIters(i) = getFive()

Once you’ve completed your script, run it and see how many iterations on average it returns. What would you expect the number to be? Is it close? Run it again. Does your result change? Why?

MATLAB should return an integer between 1 and 10 each time. If you compare your results with your classmates, your sequence of numbers will most likely be different because they are random (well, pseudorandom but that's close enough for us right now). Now let's expand our experiment and see how many numbers we have to choose until we get a five (5). Which kind of loop should you use? Now it's your turn. Write a function called getFive that returns the number of iterations required for one such experiment. Once you've got your function working, let's use it to see what happens if we conduct the experiment several times, say 20 times. Let's run it 20 times and calculate the average number of iterations needed to get a five. We can use a script to do this. Since we know the number of times we want to nun the experiment, we should use a loop. (You fill in the blank.) We can use an array to save the values that are returned by our getFive function in each iteration. We can prefill an array of 20 zeros using the command zeros (1,20) to create a row array. Let's call our array AllIters. So to initialize the array, we will use the assignment statement

Explanation / Answer

Hello,
          Please find the answer attached as under. Please give a thumbs up rating if you find the answer useful! Have a rocking day ahead!

**** Matlab Code ****

function [n,avg] = getFive

iter = 20;                          % no of iterations
AllIter = zeros(1,iter);            % memory allocation
imax = 10;                          % max range for the random number generator


for i=1:length(AllIter)
    X = 0;
    count = 0;
    while(X~=5)                     % run till 5 is generated
        X = randi(imax);
        count = count+1;            % keep a count of the iterations
    end
    AllIter(i) = count;
end
n = AllIter;
avg = mean(AllIter);                % return the number of iterations and the average
end

*** End of Code ***

Now run the following at the command prompt:

[y,b] = getFive;

The values in y gives you the number of iterations in each trial, and b gives the average of these trails. The values of b in successive running of the files are :

7.7, 7.4, 9.7....

Note that you may get different values, since this is a random number generator. The expected value is 10 trials, since the probablity of getting one number (5) out of 10 is 1/10.