ENGR 1181 Midterm 2 – MatLab Practice Problems and Solutions

advertisement
ENGR 1181 Midterm 2 – MatLab Practice Problems
Problem 1.
by
Autumn Semester 2013
The distance traveled by a water balloon with launch angle θ and initial speed 𝑣0 is given
where 𝑔 = 9.8067 π‘š/𝑠 2 .
𝐷=
𝑣0 2βˆ™ sin(2πœƒ )
𝑔
π‘šπ‘’π‘‘π‘’π‘Ÿπ‘ 
In MatLab, the sine function for x in degrees is 𝑠𝑖𝑛𝑑(π‘₯). Use ">> help sind" in the command
window if you need more information about 𝑠𝑖𝑛𝑑(π‘₯).
Write a MatLab script file (call it water_balloon.m) that does the following:
(1)
The first line in the script file (with your information filled in) should be:
% Name __________________ Seat ______
(9)
(2)
Use a basic for loop to repeat the following code twice
(3)
(4)
Ask the user to enter the launch speed 𝑣0 in m/s.
(5)
Calculate the distance traveled by the water balloon.
(6)
Use appropriate comment statements in your script file.
(7)
Print out the results using the fprintf command.
(8)
Run your program, using test data: 𝑣0 12.25 m/s and θ = 47.50o for the first pass and 𝑣0 8.52 m/s
and θ = 44.53o for the second pass.
Ask the user to enter the launch angle θ in degrees.
Open a new Word document. Copy-paste the script file and the command window output into the Word
document. Save your Word document. Print your Word document.
(10) The command window output should look like this:
For initial speed xxx.xx m/s and launch angle yyy.yy degrees, the
distance traveled is D = zzz.zz meters
Problem 1 Solution
Editor Window
%
Name - - - - - - - Seat - - -
%
water_balloon.m
%
%
This program calculates the distance of a water balloon toss, given
the initial speed v0 (in m/s) and launch angle Theta (in degrees).
clear, clc
%
Repeat code 2 times
for k=1:2
%
Ask the user to enter values for v0 and Theta
v0 = input('\nPlease enter the launch speed v0 (in m/s): ') ;
Theta = input('\nPlease enter the launch angle Theta (in degrees):
%
') ;
Calculate the Distance, D
D = v0^2*sind(2*Theta)/9.8067 ;
%
Print the results to the command window
fprintf('\nFor initial speed %6.2f m/s and launch angle %5.2f degrees,
\nthe distance travelled is %6.2f meters.\n\n', v0, Theta, D) ;
end
Command Window
Please enter the launch speed v0 (in m/s):
12.25
Please enter the launch angle Theta (in degrees):
47.50
For initial speed 12.25 m/s and launch angle 47.50 degrees,
the distance travelled is 15.24 meters.
Please enter the launch speed v0 (in m/s):
8.52
Please enter the launch angle Theta (in degrees):
44.53
For initial speed
8.52 m/s and launch angle 44.53 degrees,
the distance travelled is
7.40 meters.
Problem 2. Use your knowledge of Electric Circuits to write a MatLab script file (call it currents.m)
that calculates values for all three currents (I1, I2, and I3) in the series–parallel circuit diagram shown
below. Note that the voltage supplied by the Power Supply is +5.0 Volts.
(1)
The first line in the script file (with your information filled in) should be:
% Name __________________ Seat ______
(2)
Use additional appropriate comment statements in your script file.
(3)
Use a basic for loop to repeat the following code 3 times.
(4)
First, ask the user to enter values (in Ohms) for R1, R2, and R3.
(5)
Calculate an equivalent value for the parallel
combination of resistors R2 and R3 (call this
R23).
(6)
(7)
(8)
(9)
Current, I1
Then calculate the equivalent resistance for the
entire circuit (call this Req).
Use this information to calculate values for the
currents I1, I2, and I3.
Run and test the script with the resistor values
shown: R1 = 100, R2 = 200, and R3 = 300
Ohms for pass 1, R1 = 300, R2 = 200, and
R3 = 100 for pass 2, and R1 = 200, R2 = 300,
and R3 = 100 for pass 3.
V1
R1
100
Power
Supply
V2
+
+5 V
-
R3
300
R2
200
I2
Ground
Use an fprintf command to print out the
values of all three currents to the command window in units of milliAmperes (1 Ampere = 1000
milliAmperes).
(10) Open a new Word document. Copy-paste the script file and the command window output into the
Word document. Save your Word document. Print your Word document.
(11) The command window output should look like this:
The currents in the circuit are:
I3 = zz.zz mA.
I1 = xx.xx mA, I2 = yy.yy mA, and
Problem 2 Solution
Editor Window
%
%
%
Name - - - - - - - Seat - - The name of this program is currents.m
This program calculates the currents in a series-parallel electric circuit.
I3
clear, clc
%
Repeat the code 3 times
for k=1:3
% Ask the user to enter values for R1, R2, and R3
R1 = input('Please enter a value for R1:
R2 = input('Please enter a value for R2:
R3 = input('Please enter a value for R3:
') ;
') ;
') ;
% Calculate the equivalent resistance of R2 and R3 in parallel
R23 = R2*R3/(R2+R3) ;
% Calculate the total equivalent resistance (Req) of the entire circuit.
Req = R1 + R23 ;
% Calculate the total current I1 = +5.0 Volts / Req (by Ohms law)
I1 = 5.0/Req ;
%
Calculate V2 using V2 = I1 * R23
V2 = I1 * R23 ;
%
Calculate I2 and I3 using ohms law
I2 = V2 / R2 ;
I3 = V2 / R3 ;
%
Convert I1, I2 and I3 to milliAmps
I1 = I1*1000 ;
I2 = I2*1000 ;
I3 = I3*1000 ;
%
Print I1, I2, and I3 in milliAmps to the command window using fprintf
fprintf('\nThe currents in the circuit are I1 = %4.1f mA, I2 = %4.1f mA and I3 =
%4.1f mA.\n\n', I1, I2, I3);
end
Command Window
Please enter a value for R1:
Please enter a value for R2:
Please enter a value for R3:
100
200
300
The currents in the circuit are I1 = 22.7 mA, I2 = 13.6 mA and I3 =
Please enter a value for R1:
Please enter a value for R2:
Please enter a value for R3:
300
200
100
The currents in the circuit are I1 = 13.6 mA, I2 =
Please enter a value for R1:
Please enter a value for R2:
Please enter a value for R3:
9.1 mA.
4.5 mA and I3 =
9.1 mA.
200
300
100
The currents in the circuit are I1 = 18.2 mA, I2 =
4.5 mA and I3 = 13.6 mA.
Problem 3. WikiPedia defines "Terminal Velocity" as follows: In fluid dynamics, an object is
moving at its terminal velocity if its speed is constant due to the opposing force exerted by the fluid
through which it is moving.
A free-falling object achieves its terminal velocity when the downward force of gravity (FG) equals the
buoyancy/resistance force of drag (Fd). This causes the net force on the object to be zero, resulting in
an acceleration of zero.
Mathematically, the terminal velocity of an object falling through air is given by
where
𝑉𝑑 = οΏ½
2π‘šπ‘”
πœŒπ΄πΆπ‘‘
𝑉𝑑 = terminal velocity in m/s,
m = mass of the falling object in kg,
g = acceleration due to gravity = 9.8067 m/s,
Cd = drag coefficient, usually = ~ 0.5
ρ = density of the fluid through which the object is falling, for air this is = 1.29 kg/m3
A = frontal area of the object
Write a MatLab script file (call it terminal_velocity.m) that does the following:
(1)
The first line in the script file (with your information filled in) should be:
% Name __________________ Seat ______
(2)
Use a basic for loop to repeat the following code twice
(3)
Ask the user to enter the object's mass in kg.
(4)
Ask the user to enter the object's frontal area in m2.
(5)
Calculate the object's terminal velocity.
(6)
Use appropriate comment statements in your script file.
(7)
Print out the result using the fprintf command.
(8)
The command window output should look like this:
An object with mass xx.xx kg and frontal area yy.yy m^2, has a
terminal velocity in air of Vt = zz.zz m/s.
(9) Use the following values: once for a skydiver (m = 75 kg, A = 0.7 m2) and once for a raindrop (m
= 0.03 gm, A = 0.12 cm2). Be careful to use consistent units.
(10) Open a new Word document. Copy-paste the script file and the command window outputs into the
Word document. Save your Word document. Print your Word document.
Problem 3 Solution
Editor Window
%
Name - - - - - - - Seat - - -
%
terminal_velocity.m
%
%
%
This program calculates the terminal velocity of an object falling
through the atmosphere of earth. The object has a mass of m (kg)
and a frontal area (pointed at the earth) of A (m^2).
clear, clc
%
Repeat the code twice
for k=1:2
% Ask the user to enter values for m (in kg) and A (in m^2)
m = input('\nPlease enter the mass of the falling object (in kg): ') ;
A = input('\nPlease enter the frontal area of the object (in m^2): ') ;
%
Calculate the terminal velocity, Vt
Vt = sqrt(2*m*9.8067/(1.29*A*0.5)) ;
%
Print the result to the command window
fprintf('\nAn object falling to earth with mass %5.2f kg and frontal area %5.2f
m^2,\nhas a terminal velocity of %6.2f m/s.\n\n', m, A, Vt) ;
end
Command Window
Please enter the mass of the falling object (in kg):
Please enter the frontal area of the object (in m^2):
75
0.7
An object falling to earth with mass 75.00 kg and frontal area
has a terminal velocity of 57.08 m/s.
Please enter the mass of the falling object (in kg):
Please enter the frontal area of the object (in m^2):
0.70 m^2,
0.00003
0.12/10000
An object falling to earth with mass 0.000030 kg and frontal area 0.000012 m^2,
has a terminal velocity of 8.72 m/s.
Download