Difference between revisions of "EGR 103/Spring 2021/Lab 3"
(→3.4 Connect Assignments) |
(→3.6 Individual In-Lab (If Possible) Autograded Assignments) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 24: | Line 24: | ||
* Use the <code>ARRAY.max()</code> method and print this value | * Use the <code>ARRAY.max()</code> method and print this value | ||
Notes about the differences between MATLAB and Python when you see the solution code: | Notes about the differences between MATLAB and Python when you see the solution code: | ||
− | * MATLAB uses arrays by default, so something like [1 2 3] in MATLAB would be np.array([1, 2, 3]) in Python | + | * MATLAB uses arrays by default, so something like <code>[1 2 3]</code> in MATLAB would be <code>np.array([1, 2, 3])</code> in Python |
* MATLAB uses '''matrix''' multiplication and division by default, so if you want to multiply the '''elements''' in corresponding locations together, you have to use <code>.*</code> and <code>./</code> in MATLAB (i.e. put a period in front of the operator). Python does element multiplication and division by default so <code>np.array([1, 2, 3]) * np.array([4, 5, 6])</code> gives an array with <code>[4, 10, 18]</code> in it. | * MATLAB uses '''matrix''' multiplication and division by default, so if you want to multiply the '''elements''' in corresponding locations together, you have to use <code>.*</code> and <code>./</code> in MATLAB (i.e. put a period in front of the operator). Python does element multiplication and division by default so <code>np.array([1, 2, 3]) * np.array([4, 5, 6])</code> gives an array with <code>[4, 10, 18]</code> in it. | ||
− | * MATLAB uses <code>.^</code> for raising arrays to powers, whereas Python uses <code>**</code> so <code>np.array([1, 2, 3])**2</code> is an array with <code>[1, 4, 9]< | + | * MATLAB uses <code>.^</code> for raising arrays to powers, whereas Python uses <code>**</code> so <code>np.array([1, 2, 3])**2</code> is an array with <code>[1, 4, 9]</code> in it. |
* MATLAB and Python both have a built-in <code>max()</code> function, but for Python arrays, it is better to use the <code>.max()</code> method appended to the array. | * MATLAB and Python both have a built-in <code>max()</code> function, but for Python arrays, it is better to use the <code>.max()</code> method appended to the array. | ||
Line 33: | Line 33: | ||
=== Chapra 2.21 === | === Chapra 2.21 === | ||
− | Your program should store the $$m$$, $$v_t$$, and $$A$$ values in arrays and the $$\rho$$ and $$g$$ values in floats. Calculate the seven different $$C_D$$ values using the equations in Case Study 2.7 (one equation has $$c_d$$ as a function of $$m$$, $$g$$, and $$v_t$$ and another equation can be re-written to calculate $$C_D$$ as a function of $$c_d$$, $$\rho$$, and $$A$$. The <code>.min()</code>, <code>.max()</code>, and <code>.mean()</code> methods for arrays will come in handy! | + | Your program should store the $$m$$, $$v_t$$, and $$A$$ values in arrays and the $$\rho$$ and $$g$$ values in floats. Calculate the seven different $$C_D$$ values using the equations in Case Study 2.7 (one equation has $$c_d$$ as a function of $$m$$, $$g$$, and $$v_t$$ and another equation can be re-written to calculate $$C_D$$ as a function of $$c_d$$, $$\rho$$, and $$A$$). The <code>.min()</code>, <code>.max()</code>, and <code>.mean()</code> methods for arrays will come in handy! |
== 3.5 APT Problems == | == 3.5 APT Problems == | ||
Line 59: | Line 59: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == 3.6 | + | == 3.6 In-Lab (If Possible) Autograded Assignments == |
=== 3.6.1 P&E 1.31 === | === 3.6.1 P&E 1.31 === | ||
Line 92: | Line 92: | ||
* Note the import line at the top. | * Note the import line at the top. | ||
* The <code>round(NUM, ndigits=DIG)</code> command will round a number NUM so that it has DIG significant digits after the decimal point. You ''can'' have negative DIG values! Play with the command to see what that does. Then make sure your function is returning a value that has been properly rounded. | * The <code>round(NUM, ndigits=DIG)</code> command will round a number NUM so that it has DIG significant digits after the decimal point. You ''can'' have negative DIG values! Play with the command to see what that does. Then make sure your function is returning a value that has been properly rounded. | ||
+ | * See [https://en.wikipedia.org/wiki/Passer_rating Passer rating] on Wikipedia and specifically the NFL rating in the [https://en.wikipedia.org/wiki/Passer_rating#NFL_and_CFL_formula NFL and CFL Formula] section to determine how $$a$$, $$b$$, $$c$$, and $$d$$ are computed. Note especially that those four values are bounded by [0, 2.375]. | ||
* The link is [https://www.teamrankings.com/nfl/player-stat/qb-rating-nfl NFL Player Stats - QB Rating] from TeamRankings; click on a player and you will get a table with the information you need. | * The link is [https://www.teamrankings.com/nfl/player-stat/qb-rating-nfl NFL Player Stats - QB Rating] from TeamRankings; click on a player and you will get a table with the information you need. | ||
* When you want to submit this to the autograder, you must also submit your <code>football_helper.py</code> script. | * When you want to submit this to the autograder, you must also submit your <code>football_helper.py</code> script. | ||
Line 114: | Line 115: | ||
* I also did not include the lines about printing. Make sure you use the correct formats! | * I also did not include the lines about printing. Make sure you use the correct formats! | ||
* There are some commands in the script or that need to go in the script that we haven't completely covered in class - we will go over the following in lab: | * There are some commands in the script or that need to go in the script that we haven't completely covered in class - we will go over the following in lab: | ||
− | ** map, ord, | + | ** <code>map</code>, <code>ord</code>, <code>m.ceil</code>, <code>m.log10</code> |
+ | *** We are going to use <code>m.ceil()</code> instead of <code>np.ceil()</code> because the Numpy version returns a float instead of an int. | ||
==== On checking your random numbers ==== | ==== On checking your random numbers ==== |
Latest revision as of 19:39, 10 February 2021
Typographical errors
- None yet!
3.1 Introduction
This lab involves functions and random numbers. Every script has been started, but sometimes only with the boilerplate community standard statement. Note - none of the test_
files will be included in your lab report; they are meant to test your code and sometimes produce other files. Those other files will be included in your lab report.
3.2 Resources
See main EGR 103 page for links to these
3.3 Getting Started
3.4 Connect Assignments
The Connect assignments come from a book that focuses on MATLAB, but you will be completing the problems in Python.
Chapra 2.13
Your program needs to perform the following tasks:
- Store the Force values in an array
- Store the $$x$$ values in an array
- Calculate the $$k$$ values for each pair (i.e. you will have five different $$k$$ values)
- Calculate the potential energy for each pair
- Use the
ARRAY.max()
method and print this value
Notes about the differences between MATLAB and Python when you see the solution code:
- MATLAB uses arrays by default, so something like
[1 2 3]
in MATLAB would benp.array([1, 2, 3])
in Python - MATLAB uses matrix multiplication and division by default, so if you want to multiply the elements in corresponding locations together, you have to use
.*
and./
in MATLAB (i.e. put a period in front of the operator). Python does element multiplication and division by default sonp.array([1, 2, 3]) * np.array([4, 5, 6])
gives an array with[4, 10, 18]
in it. - MATLAB uses
.^
for raising arrays to powers, whereas Python uses**
sonp.array([1, 2, 3])**2
is an array with[1, 4, 9]
in it. - MATLAB and Python both have a built-in
max()
function, but for Python arrays, it is better to use the.max()
method appended to the array.
Chapra 2.14
Your program should store the $$n$$, $$S$$, $$B$$, and $$H$$ values in an array, then use those to calculate $$U$$. Note that Connect will accept both floating-point and scientific notation for numbers (i.e. something like 0.3624 or 3.624e-01)
Chapra 2.21
Your program should store the $$m$$, $$v_t$$, and $$A$$ values in arrays and the $$\rho$$ and $$g$$ values in floats. Calculate the seven different $$C_D$$ values using the equations in Case Study 2.7 (one equation has $$c_d$$ as a function of $$m$$, $$g$$, and $$v_t$$ and another equation can be re-written to calculate $$C_D$$ as a function of $$c_d$$, $$\rho$$, and $$A$$). The .min()
, .max()
, and .mean()
methods for arrays will come in handy!
3.5 APT Problems
- We will go through these in labs - be sure to take notes! As we go through them, you will solve the first APT Problem! Here's what we have by the end of lab:
def falling(time, velo):
return velo * time + 0.5 * 9.8 * time ** 2
# Alternately
"""
def falling(time, velo):
dist = velo*time + 0.5 * 9.8 * time**2
return dist
"""
print(__name__)
if __name__ == "__main__":
print(falling(3, 5))
print(falling(0, 0))
print(falling(0, 1))
print(falling(1, 0))
3.6 In-Lab (If Possible) Autograded Assignments
3.6.1 P&E 1.31
- Integer division is given by // and the values can be ints or floats or a combination thereof; if either or both of the arguments are floats the answer is a float.
- The remainder operator is given by %.
- Think about how you need to update the number of seconds after accounting for some with hours or minutes.
- Remember to return the three items in a tuple in the correct order.
- Note the starter test code that has been provided for you in the script.
3.6.2 Based on P&E 1.31
- Main thing here is to figure out how to get defaults to work. Remove the
*blah
and replace it with three inputs. Note that two of them need default cases. - The tester does not always have three input parameters when the function gets called.
3.6.3 Saturation Function
- Be sure to include the default cases.
- This can be done all in one calculation! Note that in Lab 2 we used ARRAY.min() to get the minimum value of an array; that version does not work if you are comparing two numbers (unless you put them in an array). Use
min(Thing1, Thing2)
instead.
3.7 Individual Post-Lab Autograded Work
As mentioned, these are individual exercises. You should not look at anyone else's work for this nor should you allow anyone other than a TA or instructor to look at your work. Note that you can submit to the autograder as many times as you would like before the lab is due; your grade will be based on the score earned the last time the autograder ran.
3.7.1 Random Integers
- Be sure to include the default cases.
- Carefully consider how you the human would keep track of rolls and roll counts before trying to code it.
np.random.seed(INT)
is the command you need to initialize the random number generator to a particular statenp.random.randint
is your friend!- There are a few different ways to organize this - make sure you are clear about what you want to do before coding.
- Note that the first output will have as many entries as there are dice and the second will have as many as there are sides.
- The return should be a tuple of two arrays. If for whatever reason you ended up with a list somewhere, you can always convert a list of numerical values to an array (if the shape of the list is compatible with conversion) using
np.array(LIST)
- You will need a for loop for this to work.
3.7.2 Inspired by P&E 1.39
- Note the import line at the top.
- The
round(NUM, ndigits=DIG)
command will round a number NUM so that it has DIG significant digits after the decimal point. You can have negative DIG values! Play with the command to see what that does. Then make sure your function is returning a value that has been properly rounded. - See Passer rating on Wikipedia and specifically the NFL rating in the NFL and CFL Formula section to determine how $$a$$, $$b$$, $$c$$, and $$d$$ are computed. Note especially that those four values are bounded by [0, 2.375].
- The link is NFL Player Stats - QB Rating from TeamRankings; click on a player and you will get a table with the information you need.
- When you want to submit this to the autograder, you must also submit your
football_helper.py
script.
3.8 Individual Lab Report
Once again, these are individual exercises. You should not look at anyone else's work for this nor should you allow anyone other than a TA or instructor to look at your work. These scripts and graphs will go in a written lab report that will be uploaded to Sakai. There is no autograder for these parts.
3.8.1 Based on P&E 1.35
- Carefully consider what the equations need to look like for the angles before writing them.
- Test your code with some obvious triangles (hint: once you import numpy as np, you can use np.sqrt()...and you probably know some things about a 1-1-1 triangle and a 1-1-np.sqrt(2) triangle!)
- The input values for trig functions and the output values for inverse trig function in numpy are in radians.
- Get the math working before trying the drawing, then spend some times figuring out how to create the line (hint: it takes four points to draw a closed triangle)
- There should be a default case for whether or not to make the drawing.
- Make sure your graphics are set to "Automatic" - see Python:Plotting#Python_Settings
- Do not use the plt.show() or fig.show() command in your code!
3.8.2 Random Numbers
- Note that you have the code for getting a NetID, converting the NetID to a seed for the random number generator, and calculating the number of bins for the histogram. You will need to change the
nums =
line to get input from the user. - I also included the code for plotting histograms with a particular number of bins.
- I did not include the code to make the distributions. They are in the np module, in the random groups, and the actual commands have names that completely make sense for generating uniform or normal distributions, respectively.
- Oops, I've said too much...
- I also did not include the lines about printing. Make sure you use the correct formats!
- There are some commands in the script or that need to go in the script that we haven't completely covered in class - we will go over the following in lab:
map
,ord
,m.ceil
,m.log10
- We are going to use
m.ceil()
instead ofnp.ceil()
because the Numpy version returns a float instead of an int.
- We are going to use
On checking your random numbers
You can check your work by using mrg
and 5000 for the input responses - you should get the values in the lab handout. However - this assumes you calculated the uniformly distributed numbers first and the normally distributed numbers second!
Calculating them this way gives you:
Information for 5000 random numbers for mrg:
Uniform: min: +8.509e-05 avg: +5.020e-01 max: +9.998e-01
Normal: min: -4.292e+00 avg: -7.668e-04 max: +3.634e+00
as listed in the lab manual. If you calculated them in the other order (normal and then uniform), you would get:
Information for 5000 random numbers for mrg:
Uniform: min: +6.652e-05 avg: +5.001e-01 max: +1.000e+00
Normal: min: -3.272e+00 avg: -8.981e-03 max: +3.660e+00
and the histograms will be slightly different as well. We will accept either set of answers! Also - this has not been tested on different platforms - if the random number generators behave differently on OSX, Win, and Linux, I will note that here. So far - no evidence of differences.
General Concepts
This section is not in the lab report but rather has some items in it that span multiple problems in the lab. We will cover these during the lab.
- If your script's main job is to define one or more functions, and you want to put test code in the script, a great way to do that is to put the tester code at the bottom in a section headed by the statement:
if __name__ == "__main__":
- That way the test code will only run when you run the script and not when you import the script.
- Please do not make any changes at all to the
test_
file! Also, do not include it in your lab report. Here's some information about how it works, though:- The fo=open() command opens a file for writing - this is how you can "print" into a file
- The scrsave() function will take a string and send it two places - the screen with a print() function and a file with the fo.write() function. This was so I only had to generate the string once to have it show up on the screen and in the file.
- The seeding will take a string, convert it to the mapping of the ord() function on the letters in the string, then add them all together.
- The try...except structure will...try to do something. If it can - great! If not, it stores the problem in ex and then prints it. If your code doesn't work, my tester will still run.
- The fo.close() command closes the file for writing
- For random numbers, the key commands we need for this week are: