Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENGR 131: Elementary Computer Programming

Midterm Assessment

INSTRUCTIONS

Complete the exercises below and upload them to Canvas as a single MATLAB script file (.m) using the naming convention ENGR131_22F_Midterm_abc123.m”, replacing abc123 with your Case ID.

For example, if Dr. Williams were submitting this test, it would be ENGR131_22F_Midterm_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 10 points (10%) of your grade.

QUESTIONS

There are 3 questions for this exam.

1. SOLVING SYSTEMS OF EQUATIONS (20 PTS)

You are working for a company that makes fruit scales and are given the circuit diagram shown in figure 1.  R2 is a variable resistor that has a resistance based on how much weight is on it.  Your boss has told you to assess whether or not this design will work given the following information.

•   It must hold 10 apples

•   Apples weigh 1.2 N

•   V1 = 5 V

•   V2 = 9 V

 

Fig. 1. Circuit schematic for fruit scale.  R2 is a force-

sensitive resistor, whose resistance changes depending

on the amount of weight placed on it.

•   R1 = 8

•   R3 = 10

•   R2 = 11 – W (where W is the weight on the resistor in newtons)

•   The current in loop 1 cannot exceed 1.5 A

•   The current in loop 2 cannot exceed 2 A

A) Given the above information, compute and plot the current in each loop for each apple up to the amount specified.  Include proper text and labeling (10 pts).

B)  Based on the information above, determine if the system can handle the required number of apples.  Plot the maximum number of apples the system can weigh with a based on the limits of each loop with a distinct marker on the figure from Part A (ex. if the loop 1 and 2 requirements limit it to 2 and 3 apples respectively, there should be a marker at 2 on the loop 1 graph and one at 3 on the loop 2 graph). If it can handle more than 10 apples, do not have any markers (5 pts).

C)  Use a selection statement to output to the Command Window either the maximum number of apples the system can hold or if it can handle 10 or more apples based on your results from part B (5 pts).

2. COMPUTING AREAS (15 PTS)

You are working on a project to measure the impact of arm support on individuals with stroke.  The key to this work is reducing the activity (amount of effort) in the deltoid muscle as much as possible in order to avoid eliciting abnormal reflexes in the affected arm (a common issue in people who have had a stroke).  You have data in the file TrialData.mat that has electromyographic (EMG) data from an experiment measured from the deltoid. There are three conditions Supported, Unsupported, and Control (the unaffected arm) and was recorded at 2.4 kHz.

A)  Plot the EMG for each condition on it’s own plot in a single figure.  Use a different graph color for each condition and include appropriate text and labeling (6 pts).

B)  Compute the effort exerted in each case by computing the total magnitude over the time of the trial and report each effort to the workspace (6 pts).

C)  Look at the plots in A and the computed effort from B.  Make a comment in your code as to whether the supported condition has more or less effort than the other conditions (ie. Is the arm support actually reducing the effort of the deltoid?) (3 pts).

3. SURVIVE! (55 PTS)

Survive takes place on an island with a volcano.  Players (in this case the user and the computer) takes turns removing their people from the island, but beware! For every rescue attempt, there is a chance of a magma spout, eliminating the person being rescued.  The higher a person’s elevation, the more likely a magma spout will occur. After three or more magma spouts, the game is over as the island explodes.  Whoever rescued the most people after the island is lost wins the game.  An example of a game in mid-play can be seen in Figure 2.  Your task is to program this game.  To do this, consider the following:

A)   Start  by  displaying  the  basic  rules  in  the  Command  Window,  initializing  variables,  and  running  the SurviveGenerateMap.p file to create the map data (5 pts).

B)  Create a function called StartMap that performs the following (8 pts):

a.   Create an array of 10 people locations for each player. Players can be located on any elevation between

1 and 4 inclusive.  Each player cannot have two people on the same location but each player can have a person on the same location.

b.   Plot the map knowing that land elevations range from 1 to 5 and 6 is lava.  Also place markers for each person as appropriate on the map.

c.   Set the background to blue and remember to include appropriate labels.

d.   Return the people locations as an output argument.

C)  Use a loop that (10 pts):

a.   Runs until the game is over

b.   Alternates between the human and computer player by calling their respective selection functions in turn.

c.   Uses these selections in a function to determine the outcome of the rescue attempt.

d.   Determines when the game is over and how (too many magma spouts or everyone is rescued).

D)  Create a function called GetHumanSelection that (10 pts):

a.   Gets the users selection for the X and Y coordinates of the person they want to rescue

b.   Checks that user input is correct in terms of being a valid coordinate and that the human has a person at that location.

c.   Repeat this process until the user enters valid X and Y coordinates for one of their people.

d.   Update the human’s people locations and output that as well as their selected coordinates as output arguments.

E)  Create a function called GetComputerSelection that (10 pts):

a.   Indicates in the workspace that the computer is taking its turn

b.   Selects the coordinates of a random person to save from the computer’s list of people (created in part B).

c.   Updates the computer’s people location and output that as well as their selected coordinates as output arguments.

F)  Create a function called RescuePerson that (10 pts):

a.   Determines if a magma spout occurs based on the elevation of the selected location – 25% chance per level of elevation (this makes it a pretty quick game…)

i.   If a spout occurs:

1.   Report this to the Command Window

2.   The person being rescued is lost.

3.   Increment the number of magma spouts.

4.   Place a red star in the location where the spout occurred on the map

ii.   If no spout occurs

1.   Increment that players total number of people saved.

2.   Place an X the color of that player’s markers over that person’s location to indicate that they have been rescued.

b.   Return the players total rescued and the number of magma spouts as output arguments.

G) When its all over, report who won (or tied) and the number of people they rescued (2 pts).

Hints:

•   This may seem daunting but consider mapping out your thought process.  Most of the operation is little more than selection statements and a few loops.

•   Think about the past few labs  does any of this seem familiar?

•   While output arguments were specified in the instructions, input arguments were not.  Think about the information each function needs to work.

•   While playing, it helps to drag the figure from the corner to spread out the markers and better see the coordinates.

•   Keep in mind that increasing the row index is down” when viewed in the variable viewer, but up” in terms of how it is plotted.