Question

Using MATLAB code only please! 4. Write a script which does the following (use a while...

Using MATLAB code only please!

4. Write a script which does the following (use a while loop).

a. Creates a random number

b. Calculates the sum of random numbers selected, across all loop iterations

c. Prints the number of random numbers (the number of loop iterations) which were required to add up to 20 (or more).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

% initilize s to zero

s = 0;

% initilize c to zero

c = 0;

% while loop till s less than 20

while(s<20)

% generate random integer from 0 to 10

r = randi([0 10]);

% add r to s

s = s+r;

% increment c

c = c+1;

end

%display count of numbers

disp(c);

%display sum

disp(s);

% sample output

% 5

% 23

Add a comment
Know the answer?
Add Answer to:
Using MATLAB code only please! 4. Write a script which does the following (use a while...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that...

    THE CODE NEEDS TO BE ON MATLAB 2. Exercise (a) Let's write a script file that calculates exp(2) by a Maclaurin series using a while loop exp x )=-+-+-+-+ The while loop should add each of the series terms. The program error as defined below is smaller than 0.01. Error is the exact value (i.e. exp(2)), minus the approximate value (i.e., the current series sum) should exit the loop when the absolute absolute error-lexact-approx Use fprintf within the loop such...

  • MATLAB CODE. answer the following question in a driver in matlab please. 3. Write a for...

    MATLAB CODE. answer the following question in a driver in matlab please. 3. Write a for loop that calculates the first eleven values of the recursive sequence ak = -1(1 - ax-1), do = 10, and stores all eleven numbers in a vector. After the loop write a command to display the vector in the command window. Note that your loop does not need to compute do since this is given, and we should be the first entry in your...

  • By using MatLab please. Best. Write a short script with a 'for-loop' which will output the...

    By using MatLab please. Best. Write a short script with a 'for-loop' which will output the first 10 numbers in the Fibonacci sequence. (0, 1, 1, 2, 3, 5, 8, ...) a_0 = 0, a_1 = 1, a_2 = 1, a_3 = 2, ..., a_n = a_n-2 + a_n-1

  • MATLAB You've recently looked at 'while' loops in Matlab. Break statements can be contained within 'while'...

    MATLAB You've recently looked at 'while' loops in Matlab. Break statements can be contained within 'while' loops to exit the loop early. Look at the following example code: x=-10; while x<0       x=x+2;      if x == -2             break;       end end Without the break statement, the while loop would usually add 2 to x=-10 until x=0, and then produce a final answer of x=0 at the end of the loop. Now, the loop ends prematurely when the value of...

  • THIS IS USING MATLAB 1. The value of a can be approximated by: a. Write a...

    THIS IS USING MATLAB 1. The value of a can be approximated by: a. Write a script which asks the user to input the number of iterations (n), then computes the expression, using a for loop to compute the summation with the specified number of iterations. The larger the number of iterations, the closer the result should be to it. Print out the value of n, and the result using fprintf with 8 digits after the decimal to see the...

  • Please write the script for following problem in MATLAB. Write a script with a file name...

    Please write the script for following problem in MATLAB. Write a script with a file name Problem3.m that, when executed, prints 1.99 dollars per unit. How many units will you purchase? [new line] Then control is given back to the user to input a scalar integer quantity. The script then prints out __ units at 1.99 dollars per unit is a total of ___ dollars. [new line] where the first blank is filled with whatever number was inputted by the...

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • MATLAB Write a function with the header function [s, count] = myMonteCarlo(f, xLeft, xRight, tol) which uses bracketing...

    MATLAB Write a function with the header function [s, count] = myMonteCarlo(f, xLeft, xRight, tol) which uses bracketing logic and random numbers to solve for the root of f. Start from your code for Problem 1, then modify the update equation to randomly choose a number between xLeft and xRight. That is your xNew. Note your code will take a different number of iterations to find the root every time you run it, even for the exact same initial bracket,...

  • Write a Matlab Function (not a script) that performs a single iteration of the Euler's method...

    Write a Matlab Function (not a script) that performs a single iteration of the Euler's method for the regular parashot problem, (i.e. just write a function that calculates v_next) Assume: cd = 0.25 [kg/m] for t < 10sec and cd = 1.5   [kg/m] for t ≥ 10sec Hint: use only if-statements, do not use any LOOPs you only need to compute only one iteration Hint2: make sure you define the inputs and outputs of the function correctly Please show how...

  • 3. Write a MATLAB script that does the following: elements equal to 3. colum ,. Creates...

    3. Write a MATLAB script that does the following: elements equal to 3. colum ,. Creates a matrix having 200 rows and 100 columns with al that are in both the last 50 rows and the Changes the elements in the matrix of part a last 50 columns from 3 to 5. . ur script should make MATLAB do the heavy lifting. This means there should be arn to tpe unfstle In order to enter your script into a text...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT