Uploaded by Eesha Mathur

ENGR 131 22S-IN-030-04 (Lab 4 Instructions)

advertisement
ENGR 131: Elementary Computer Programming
Team IN – Instructor
Lab Exercise #4
INSTRUCTIONS
Complete the exercises below and upload them to Canvas as a single MATLAB script file using the naming
convention “ENGR131_22S_Lab##_abc123.m”, replacing abc123 with your Case ID, and ## with the two-digit
lab number.
For example, if Dr. Williams were submitting Lab 4 it would be ENGR131_22S_Lab04_mrw8.m
For your script, please perform the following:
1. Separate each question into separate, runnable sections using the “%%” comment notation.
2. You may use the code and notes from class, the textbook, MATLAB’s documentation, and anything you
find using Google to solve these problems.
3. Use comments as appropriate to indicate your thoughts and how your code works (or is supposed to work).
This is 5 points (10%) of your grade.
QUESTIONS
There are 3 questions for this lab.
1. MODULAR PROGRAMMING & REPEATED PLOTS (20 PTS)
For this Lab, you will be building on the work of Lab 3. In this lab, you will use nested for loops to plot the
behavior of the system shown in Labs 2 & 3 over several damping coefficients and oscillation frequencies. You
will also be revising your Lab 3 code to make it more modular. Modifications to your Lab 3 code will consist of
the following:
• Removing the user input commands and instead loading the system parameters from a file
o This file, once loaded, will two variables, DC, and w in your workspace. These are the damping
coefficients and oscillation frequencies you will be evaluating.
• Revising the CalcPosition function to use w as an input argument
• Moving the plotting commands to a separate function that uses the loop variables as well as the time,
computed height, and w as input arguments.
The overall output of this question will produce a plot similar to that shown in Fig. 1.
ENGR 131 22S-IN-030-04 (Lab 4 Instructions)
2/1/2022
Page 1 of 5
1
0.5
0.5
0.5
0
Height (m)
1
Height (m)
Height (m)
Height as a function of Damping
w=8
Height as a function of Damping
w=4
Height as a function of Damping
w=1
1
0
-0.5
-0.5
-0.5
-1
-1
-1
0
5
10
0
15
0
1
2
3
0
0.5
1
1.5
Time (sec)
Time (sec)
Time (sec)
Height as a function of Damping
w = 16
Height as a function of Damping
w = 12
1
1
0.5
0.5
DC = 1
Height (m)
Height (m)
DC = 3
0
DC = 6
0
DC = 9
-0.5
-0.5
-1
-1
0
0.5
1
Time (sec)
0
0.2
0.4
0.6
0.8
Time (sec)
Fig. 1. Example of output for question 1 showing five plots, each with a different frequency of
oscilation (w) across four different Damping Coefficients. Both the title and the legend entries
use the str2num function to include numeric data from the variables used for the plots.
Recreate the plot in Fig. 1 by following the steps below:
A. Create a new script and copy your CalcPosition function from Lab 1 over to this new script (remember,
functions stored inside the script file are not available to other scripts). Include a third input argument, w (1
pts)
B. Load the data file ENGR131_Lab4_Data.m into the workspace (3 pts).
C. Since we’ll be looping over two variables, we’ll need two loops with one inside the other. For the first one
(outer loop), create a loop that will range from the first to the last value of the oscillation frequency (w) (4
pts).
D. Create a Time vector that ranges from 0 to 15/w for each value of w and contains 30 evenly spaced values.
E. For each value of w, calculate and plot the height of the system over time for each of the damping coefficients
that you calculated
1. Use a for-loop that runs from 1 to the length of the damping coefficient vector (this is your inner loop)
(3 pts)
2. Compute the height for each damping coefficient by calling the CalcPosition and passing in your Time
vector, the damping coefficient for that iteration, and the w for that iteration.
a. Ex: Time, DampingCoeff(1,j), w(1,i)
3. Call your plotting function (Covered below in part f) and pass both loop variables, the time and height
vectors, and the current value of w.
ENGR 131 22S-IN-030-04 (Lab 4 Instructions)
2/1/2022
Page 2 of 5
F. Create function to handle plotting your data. It should have your two loop variables, the time and computed
height vectors, and the current value of w for that outer loop iteration as input arguments. Similar to Lab 3,
use a switch statement based on the inner-loop iterator variable to assign a character array to the color and
linetype code you wish for that iteration. (2 pts)
Ex: switch i
case 1
LineColor = ‘g-‘;
….
end
G. Set figure 1 as the active figure (the first time you use this, it will create the figure, the subsequent times, it
will “grab” the figure and make it active for the following plot commands.
H. Use the subplot command to make a 2 x 3 set of subplots and select the current one using the outer-loop
iterator variable you passed in (2 pts).
I. Set the hold on toggle (so you don’t overwrite your plots with each iteration)
J. Plot the system behavior using the plot command using the character array you set in the switch statement to
specify the color and line type codes. Set the marker size to 4 (1 pt).
K. Add appropriate axis labels (1 pts)
L. Create a two-line title with the first line stating what is being shown and the second indicated the value of the
oscillation frequency (hint: [‘w = ‘,num2str(w)] ) (1 pts)
M. Check to see if the last plot has been plotted. If so, place a legend on the plot that lists value of each damping
coefficient. (hint: this too will involve the num2str command). Include this property statement when you call
the legend command: ‘Position’, [0.75 0.25 0.1 0.1] (2 pts)
N. Set the axis limits for x to be 0 to the last value in the time vector and y to -1 to 1
O. Turn off the hold toggle
Write a script that steps through the simulation with
a key press (s) and ends by pressing the q key. At
each step tell the user how much money they have
earned and at the end, report the total number of
weeks that were simulated.
ENGR 131 22S-IN-030-04 (Lab 4 Instructions)
Dock #1
20
Ship Length
15
11
10
7
5
0
0
1
2
3
0
0
4
5
Ship
Dock #2
20
15
Ship Length
2. STEPPED SIMULATIONS & ADVANCED
PLOTTING (20 PTS)
Simulations are often computed in discrete steps.
For this problem, you are asked to create a
simulation that computes the income for two docks
based on the length of the ships that arrive to
unload. The lengths range from 3 to 13 in steps of
2 units and there is a 40% chance of not having a
ship in any given slip. When the total length of
ships for a dock that week is over 20, show them in
green to emphasize how productive the dock is.
11
10
9
7
5
0
1
0
0
2
3
4
5
Ship
Fig. 2. Matlab plotted ships in port. Green ships
indicate that the total length of ships for that
dock that week are over 20.
2/1/2022
Page 3 of 5
To recreate something similar to Fig. 2, perform the following:
A. Initialize the overall simulation parameters to 0 including the state of the simulation (ie. SimOver), the total
user’s cash, and the total number of weeks (2 pts)
B. Set up the random ship schedule (2 pts)
1. Create a column vector that includes values from 3 to 13 in steps of 2. Add 4 zeros to the end of this
to account for the 40% chance of not having a ship.
2. Repeat this vector 20 times so that you end up with a 200x1 vector (hint: look up the repmat
command).
3. Randomize the schedule using randperm function.
C. Create a script that will run until the user ends the simulation by pressing the q key. The script should step
through the simulation once for each press of the s key. No other key presses are allowed. At each step, the
sim should 1) See what length of ships is in port that week and compute the week’s income between both
docks, 2) Display the ships, and 3) report the amount of money the user has. Steps 1 and 2 should be handled
by calling a function that handles each step of the simulation (see part D). (4 pts)
D. Create a function to handle the week’s work (while the code in part C is for the overall simulation). This
function should receive the user’s cash as an input argument and return the revised cash as an output argument.
(4 pts) Within this function, do the following:
1. Initialize the week’s variables (the individual ships and the total length of shipping at each dock).
2. Call the GetShips function (see part E) for each dock
3. Call the PlotShips function (see part F) for each dock
4. Compute the total user cash by adding the week’s income from each dock to the total
E. Create a function called GetShips to handle getting ships from the schedule. There are no input arguments,
but the function should output a vector with the length of each ship in the dock’s five slips and a scaler with
the total length of the ships for that week. (4 pts) Within this function, do the following:
1. Use the evalin command to get the ship schedule from the base workspace (see part B).
2. Take the first five ships from the top of the schedule as the ships in port for that week
3. Add up the total length of the ships from E.2.
4. Remove the ships that docked from the schedule and use the assign command to place the updated
schedule back in the base workspace.*
F. Create a function called PlotShips to plot the ship with a subplot for each dock as shown in Figure 2. This
function will need the dock number to be plotted, the ships lengths to be plotted, and the calculated total length
passed to it. (4 pts) Within this function, do the following:
1. Use a selection statement to change the bar graph color to green if the total for that dock is over 20
2. Use a selection statement to change the plot title so it lists the correct dock
3. Make the figure active
4. Select the correct subplot for the dock you are plotting
5. Plot the ship lengths using the bar command
6. Add a text label to the top of each ship showing its length
7. Change the background color to blue, but not the “stock” blue (set the RGB values)
8. Apply the title, axis label, and Xtick labels
*Using the evalin and assign commands like this is, in this context, a bit of a “cheat”, but is a fair approximation of the process of reading
from a file, modifying the data, and then re-saving the revised data back to the file. We’ll cover this in more detail later in the course.
ENGR 131 22S-IN-030-04 (Lab 4 Instructions)
2/1/2022
Page 4 of 5
Profile of Espresso Cup
4
3.5
3
2.5
height (cm)
3. COMPUTING VOLUMES (5 PTS)
The profile of an espresso cup is shown in Figure 3. The
profile (radius) follows the equation r = 0.5*log(h) + 0.5
where h is the height in cm. Compute the volume of the mug
using both the trapezoidal and summation integration
methods for determining the volume of a revolved solid using
a step size of 1 mm and report these values to the command
window. Plot the profile as shown.
• Hint #1: Do not let the figure fool you.
• Hint #2: To plot the figure, swap the order of the
independent and dependent axes.
2
1.5
1
0.5
0
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
radius (cm)
Fig. 3. Profile of coffee mug
Revision
A
Description
Original Document
ENGR 131 22S-IN-030-04 (Lab 4 Instructions)
Date
2/1/2022
2/1/2022
Page 5 of 5
Download