EGR 102 Exam #2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

I3. Why is it good to do "clear" before starting a script?

"clear" will remove all variables from the global workspace, so you can make sure the program doesn't accidentally depend on unexpected variables, and will work correctly when used on another computer. It also disconnects old connections to servos or micro controllers as a result of removing their variables.

B3. What do the following symbols do for a logical statement: && || ~ == >= <=

&& (and) || (or) ~ (not) ==(is equal to) >= (greater than or equal to) <= (less than or equal to)

E1. How would you make MATLAB print the following: "The desired dimension is 45.2 inches however we are allowing a range of 46.0 to 44.3." Assume that you have variables midNum, highNumand lowNumfor the three numbers. Use an fprintf statement.

fprintf('The desired dimension is %.1f inches however we are allowing a range of %.1f to %.1f.\n' midNum, highNum, lowNum)

B2. How would you write MATLAB code that says: if GAMES is greater than or equal to 50, display "Too many games" otherwise it says "Buy more games"?

if GAMES >= 50 disp('Too many games'); else disp('Buy more games'); end

B1 When do you use if, if/else, if/elseif, if/ elseif/else

1.if : a single criterion (may or may not be done), 2.if/else: a single criterion, but something will happen if it is true and something else if it is false. 3.if/ elseif: more than one linked criterion-the answer might be one of them, or it might be none at all. 4.if/ elseif/else: multiple linked criteria- the answer will fall into one of several buckets.

F4. A power outlet puts out 120 ± 5V. What are 120V and 5V called? What is the range of voltages it might produce. Will it work with a device that requires 110 V or higher? One that requires 130V or higher? 117 V or higher?

120 ± 5V : 120 V is the nominal value, 5 V the tolerance. The range of possible voltages is from 115 to 125 V.The outlet would work with a device that requires >110 V, and would not work with a device that requires >130 V. It might work sometimes with a device that requires >117 V, but the voltage isn't guaranteed to be high enough, so that wouldn't be a good idea.

D4. We frequently run for loops based on sequences which can have one or two colons. What sequence of numbers will the following produce: 1:2:10 10:-2:1 2:2:20 2:3:20

1:2:10 will do 1,3,5,7,9; 10:-2:1 will do 10, 8, 6, 4, 2; 2:2:20 will do 2, 4, 6, 8, 10, 12, 14, 16, 18, 20; and 2:3:20 will do 2, 5, 8, 11, 14, 17, 20

D2 What is sentinel variable? What is an accumulator? When do you want to use each of these?

A sentinel value is used in a while loop to watch for certain values of something being monitored and exit the loop if they are seen. Inputting or measuring the value should be the last thing before the loop, and the last thing inside the loop. An accumulator is a variable that "gathers up" or accumulates values -for example, adding up the sum in Sequence-O-Meter.

H3. If the array shown below is called MyData, how do you put just column 3 into the variable "data2"?

Data2 = MyData(:,3)

E2. How would you put an integer into an fprintfstatement? How about going to the next line?

An integer can be put in with %d (or you can use %.0f). If you want to have the cursor go to the next line, use \n inside the quotes.

C2. What is the difference between an analog and a digital signal?

Analog is a signal that can vary continuously (mathematically you can always have one point between any other two points). Digital has fixed values it can assume (it moves in steps instead of a continuous output): for example, on the Arduino, either 5V or 0V

H1. What arrays do each of the following commands create? Assume the commands are run in order. 1. Array1=1:2:152. 2. Array2=Array1<53 3. Num=Array1(end) 4.NumOther=Array1(end-1) 5.Array3=Array1+3

As listed below. Array1 = [1, 3, 5, 7, 9, 11, 13, 15] 2.Array2 = [1, 1, 0, 0, 0, 0, 0, 0] 3.Num= 15 4.NumOther= 13 5.Array3 = [4, 6, 8, 10, 12, 14, 16, 18]

G5. What is "bounce" and what is a strategy to deal with it?

Bounce is the signal turning on/off rapidly as a switch or button first starts to connect. Allowing a small delay in time before looking for a signal from the switch can frequently take care of the issue.

C8. Connecting commands - how do you connect an Arduino? A servo motor?

Connecting to arduino: either a=arduino('com3', 'uno') or the Connect_Arduino() function provided in class.

C6. Write a command that would move a servo motor to a 90 degree angle if it is on digital pin D3.

First connect to the servo, then writePosition: s1 = servo(a, 'D3', 'MinPulseDuration', 700*10^-6, ... 'MaxPulseDuration', 2300*10^-6); % or the Connect_Servo() provided in class writePosition(s1, 0.5);

H4. How do you replace the 50.3 with 44?

MyData(6, 2) = 44;

D6 What is wrong with this while loop? DangerThreshold= 3; NumInput=input('enter a number'); while NumInput< 3 disp('all is well') end disp('shut down test!')

The while loop does not change the state of the critical variable inside the while loop, and will therefore go into an infinite loop and not monitor the test as designed.

G6. Given a voltage divider circuit and a resistor that varies can you describe what happens to the output voltage?

R1 increases Voutdecreases R1 decreases Voutincreases R2 increases Voutincreases R2 decreases Voutdecreases

F1. In control systems, what are redundancy and feedback and how are they useful?

Redundancy is having more than one system that can do a function, so if one fails the other can continue to keep the system functional. Feedback is a signal from a downstream system being fed back upstream to a control algorithm: using the outputs to control the inputs. It lets the control algorithm know the effect of what it is doing on outputs.

I1. If I have an spreadsheet called "Data.xlsx" in my matlabdirectory, how would I read it in and assign it to an array called Sensor? How would I take data from an array called Sensor and write it out to an excel spreadsheet called "Data.xlsx"?

Sensor=xlsread('Data.xlsx'); xlswrite('Data.xlsx', Sensor)

G1. What types of sensors can you put into a voltage divider?

Sensors that change resistance with an input are used with voltage dividers

H2. How do I get the number of elements in Array1 and store it in the variable Size1?

Size1 = length(Array1);

E4. What would be the difference in the above if I wanted the plot to be a scatter plot (dots) instead of a connected line?

The ylim() function puts limits on the y axis for how high and low the plot can go. It takes an array with the minimum and maximum Y-value.

D5. What is the word for the loop structure below, where one loop is "inside" another? How many times will a sum be calculated? What will be the second answer displayed? for x = 1:2:20 for y = 1:5 for z = 1:3:8 answer = x+y+z; disp(answer); end end end

The code is a nested loop. The first loop (1:2:20) takes the values 1,3,5,7,9,11,13,15,17,19 - or 10 numbers. The second has 5 numbers, and the third (1:3:8) has the values 1, 4, 7, so 3 numbers. There will be 10 x 5 x 3 = 150 numbers displayed. The innermost loop variable changes the most quickly, so 1 + 1 + 4 = 5 is the second answer computed.

I5. How do you get out of an infinite loop? (How do you get INTO one?)

To get out of an infinite loop, click in the command window and press Ctrl-C (even on a Mac, not Cmd-C) to end the program. A while loop is one way to get in one (planned or unplanned): if the condition of the loop never becomes false, it will be an infinite loop.

I4. How do you work the debugger?

The debugger is in the editor tab. You can insert breakpoints where you would like the program to stopby clicking on the "-" next to the line number: it changes into a red dot indicating a breakpoint. Continue allows the program to keep running from there until the next breakpoint. Step will have the program run the current line of code (the one with the green arrow).

I6. Name at least two kinds of coding mistakes that can cause the error message "Undefined function or variable"

The error means a variable was not defined (assigned to) before it is used. It could be that the definition is missing; the definition or comes after the line where it is used; or it could be that the name is misspelled or has the wrong capitalization. It could also happen when you try to call a function and the function is missing or not in the MATLAB folder./

G2. What do we measure with a voltage divider?

We measure the voltage - which tracks changes in resistance

D1. While loops and for loops. What are they, what is the difference between them and when would you want one versus the other?

While loops do something "while" or as long as something is true. For loops are counting loops; they do something a given number of times. You want a while loop for tasks that need to potentially keep on going. You need a for loop to do things a set number of times or once per item on a list for example.

F2. I have an optical sensor that varies between two general levels depending on whether the sensor is blocked or open as shown in the image on the next slide. What is a good threshold to use to determine if the sensor is blocked or not?

You want a threshold that will be between the blocked and not blocked signal levels (so that slight changes in those levels do not give false readings). For this graph a number of about 4 would be a good choice, but 3 or 5 would work too. Don't use 2 or 6, or you might miss some of the signals. Analog, since it varies continuously across a range of values.

C3. When you have a structure like a voltage divider circuit to read a sensor like your thermistor - what kind of pin do you want to read it from and why?

You want to read it from an Analog pin, because the output of the voltage divider is an analog voltage that is changing as the thermistor resistance changes.

C4. Create a line of code that would read analog pin A0 on your arduino"a" and assign it to a variable called data.

data = readVoltage(a, 'A0');

I2. If I have a function called weight_sensor that takes one input argument and reads my sensor, how do I call that from inside my program, give it as its argument the variable Mine, and assign the result to a variable called "data"?

data=weight_Sensor(Mine);

E3. If I have 2 arrays, population and time, how would I make a plot with population on the vertical axis and time on the horizontal? What are the commands for adding a title that says "Population Growth" and an x axis label that says "time in months", and a y axis label that says "population total"?

plot(time, population) 1.title('Population Growth') 2.xlabel('Time in months') 3.ylabel('population total')

C5. Create a line of code that would send a "high" or "on" voltage out to digital pin D6.

writeDigitalPin(a, 'D6' ,1);

D3. (Zybook) use the || or && with a while loop

|| is a logical OR as in (x>3 || x< 1). The single | also works in most situations. The && means AND as in( x>3 && X<5). Likewise, & usually works too.


संबंधित स्टडी सेट्स

Greatest Common Factor (GCF):, GCF and LCM of Monomials

View Set

Handling Difficult Customer Situations - Chapter 5

View Set

Chemistry 1120 Unit 1: Chapter 7: The Quantum-Mechanical Model of the Atom

View Set

4. A GAZDASÁG SZEREPLŐI A GAZDASÁGI KÖRFORGÁSBAN ÉS A GAZDASÁGI CIKLUSOK - 1. A PÉNZ TÖRTÉNETE 2. A PÉNZ SZEREPE ÉS FUNKCIÓI

View Set

The Essentials of Human Anatomy and Physiology: Chapter 1 Practice Test (The Human Body: An Orientation)

View Set