PURPOSE: To practice plot, FOR and WHILE loop in conjunction with the IF and BREAK statements.
For each problem, create a MATLAB script file and name it FIRSTNAME_LASTNAME_LAB8_ problemX.m. Put ALL the commands for the required steps in your script file:
- Be sure to clear the display and the memory.
- Display your name.
- Separate and label different steps using comments.
- For each plot question, you can use pause and close functions to stop and check the plots, and then close the figure window.
Class: ENGR15100: Software Tools for Engineers
Instructor: Xiaoli Yang
Author: [Student’s Name]
Assignment: Lab [No.]
File Name: LASTNAME_LAB[No.]_problem[No.].m
disp(‘Your Name Here’);
disp(‘starting code: ‘);
%Completing lab x
%your source code here%
Consider the equation in meters, where ‘’ is the time in seconds. Write a script to do the following:
- Clear the screen and the variables.
- Determine the number of loops required to generate values of t from 0 to 100
seconds with a step size of 0.25 seconds.
- Reserve memory for the variables and
- Use a FOR loop to set up the index variable i.
- Use i to calculate and store which should go from 0 to 100 by 0.25’s.
- Use to calculate the values in the loop.
- Create a plot in a Figure 1 with time t as the variable on the x axis and the y variable
on the y axis.
- Add the title “Y versus Time”, the x axis label “Time (sec)” and y axis label “Y
(meters)” to the plot.
- Include a pause statement followed by a line of code to close the figure.
- Using a WHILE loop structure in the variable with a time step size of 0.01 seconds,
find the time t at which y passes through 1600 meters on the way up.
- Using a WHILE loop structure in the variable t with a time step size of 0.01 seconds,
find the time at which passes through 1600 meters on the way down.
- Using a WHILE loop structure in and again having a time step size of 0.01 seconds, find the time t at which y is at a maximum.
- Using a WHILE loop structure in t with a time step size of 0.01 seconds,
determine the elapsed time required for y to go from 1000 up to 2000 meters.
HINT: Solve this problem using the following logic.
- Add a variable etime and set it equal to zero before the WHILE loop.
- Allow the WHILE loop to run until say just past 2300 meters (which is >2000m).
- Inside the WHILE loop use an IF statement to determine if the value of y falls between the values of 1000 and 2000 meters. If it does, add 0.01 seconds to the running total variable for the elapsed time etime to count how much time is spent between 1000 and 2000 meters.
- Use the FPRINTF command to show the result of etime.
- Using if-end and while-end statements, perform the following:
- Prompt the user to enter numerical grades, one grade at a time (i.e. do not use vectors). Any number of grades can be entered, however, each grade must be non-negative and no larger than 100. Otherwise, the grade is considered invalid and the user should be prompted to re-enter the grade. A grade of 999 indicates the user has finished entering numerical grades.
- Compute the number of grades in each grade range using the standard grading policy: A [90 – 100], B [80 – 90), C [70 – 80), D [60 – 70), F [0 – 60).
- Compute the minimum grade, maximum grade, and average grade. You are NOT allowed to use built-in functions min(), max(), sum(), or mean().
- Using multiple instances of the built-in fprintf() function, display the following items in the Command Window:
- the number of valid grades entered by the user formatted as an integer (always display)
- the number of valid grades belonging to each grade range, each formatted as an integer (always display)
- the minimum grade, maximum grade, and average grade, each as a fixed-point real number showing a maximum of 3 digits beyond the decimal point (display when at least one valid grade is entered)
- the overall letter grade according on the average formatted as a character. (display when at least one valid grade is entered).
SUBMITTING YOUR LAB:
Submit your lab by uploading .m file using the Blackboard Assignment feature no later than the date specified.