Lab 7

Repetition: while Loops

CS211 Lab Policy:

Instructions:

You will create three (3) MATLAB program file named Lab7a.m, Lab7b.m, and Lab7c.m for this lab.  For each step below, add appropriate MATLAB code and label each step with appropriate comments.  Make sure you test your code for each step before proceeding to the next step.

For Lab7a.m: Write code that implements several simple loops.

  1. Begin your program by clearing the command window and displaying your name and "Lab 7a".
     
  2. Add code that repeatedly prompts the user to enter a prime number until the user enters a prime number.  Use MATLAB's logical function isprime() to determine if a number entered by the user is prime.  If the user enters a non-prime (composite) number, your code must tell the user that the number entered is not prime before asking for input again.  Once the user enters a prime number, your code should thank the user.
     
  3. Add code that repeatedly prompts the user for a cadet squadron, an integer from 1 to 40, until the user enters a valid squadron number.  Hint: use the floor() function to test for an integer value.
     
  4. Add code that repeatedly displays a random integer from 1 to 6 (the roll of a die) until the first number displayed is redisplayed.  Thus, numbers after the first number may be repeated any number of times, but the first number will be repeated exactly once.  Hint: use the following expression to get a random integer from 1 to 6:

        floor(rand()*6) + 1

For Lab7b.m:   Write code that plays the game of "hi low" with the user.  Your code should do the following:

  1. Display a brief introduction to the game.
     
  2. Select as a secret number a random integer from 1 to 100 (use the same equation as step 4 above, but with a scaling factor of 100).
     
  3. Set the user's guess to 0 (which you know cannot be a correct guess).
     
  4. Repeat the following two steps until the user's guess equals the secret number:
  5. Thank the user for playing the game

For Lab7c.m:   Add code to a function that finds roots of an equation using the Newton-Raphson method (see description in the lesson notes).

Start with the file Lab7c.m (right-click and select "Save Target As...")

  1. Modify the code to display the new "guess" value on each iteration of the loop (i.e., display the value of X). Display the value with at least 15 digits of accuracy after the decimal point. (This will require the use of the fprintf() function.)
     

  2. Add a counting variable that counts the number of times the loop executes and display this value after the loop terminates.
     

  3. The equation in the function f(), Y = 3X2 - 12.1X + 5, has two roots, 0.467383678385362 and 3.565949654947949. Make sure you can execute the program and enter appropriate initial guess values to find each one. (No code modification required.)
     

  4. Modify the equations in the functions f() and f_derivative() to Y = 3X2 - 4X + 12 and Y = 6X - 4 respectively and then try to find the roots of this new equation. What happens? Can you find the roots? (Remember, type CTRL-C to terminate an infinite loop.)

Turn-in:

Submit your lab7a.m , lab7b.m and lab7c.m files.