Difference between revisions of "EGR 103/Fall 2018/Lab 3"

From PrattWiki
Jump to navigation Jump to search
 
 
(2 intermediate revisions by the same user not shown)
Line 26: Line 26:
 
* 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)
 
* 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.
 
* There should be a default case for whether or not to make the drawing.
 +
* Do not use the plt.show() command in your code - that basically completes the figure and then starts another one.  If you want to see the figures on the screen, you can add a plt.show() command to the test code '''after''' the figure has been saved - that is, after:
 +
<source lang=python>
 +
        plt.savefig('TriPlotTest{}.eps'.format(k + 1))
 +
        plt.savefig('TriPlotTest{}.pdf'.format(k + 1))
 +
</source>
 +
you can add
 +
<source lang=python>
 +
        plt.show()
 +
</source>
 +
* If your triangle plots have successively more triangles in them, add
 +
<source lang=python>
 +
plt.clf()
 +
</source>
 +
to your function before your
 +
<source lang=python>
 +
plt.plot(...)
 +
</source>
 +
command.
  
 
=== 3.3.3 P&E 1.39 ===
 
=== 3.3.3 P&E 1.39 ===
Line 70: Line 88:
 
** 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 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
 
** The fo.close() command closes the file for writing
 
+
* Chapter 5 in P&E (5-5.3.4 specifically) is a great place to get some help with understanding how functions are set up, as are the following links:
 +
** [http://greenteapress.com/thinkpython2/html/thinkpython2004.html Think Python Chapter 3: Functions]
 +
** [http://greenteapress.com/thinkpython2/html/thinkpython2007.html Think Python Chapter 6: Fruitful Functions]
 +
** [https://docs.python.org/3/tutorial/controlflow.html#default-argument-values Python Tutorial Section 4.7.1 Default Argument Values]
  
  

Latest revision as of 20:31, 19 September 2018

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 - note 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. For the resources - see main EGR 103 page for links to these

3.2 Getting Started

Same as it ever was.

3.3 Assignment

3.3.1 P&E 1.31

  • Integer division is given by // and it can take ints or floats or a combination; 5.0 // 2 is 2.0.
  • 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.

3.3.2 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 math as m, you can use m.sqrt()...and you probably know some things about a 1-1-1 triangle and a 1-1-m.sqrt(2) triangle!)
  • Remember that the output values 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.
  • Do not use the plt.show() command in your code - that basically completes the figure and then starts another one. If you want to see the figures on the screen, you can add a plt.show() command to the test code after the figure has been saved - that is, after:
        plt.savefig('TriPlotTest{}.eps'.format(k + 1))
        plt.savefig('TriPlotTest{}.pdf'.format(k + 1))

you can add

        plt.show()
  • If your triangle plots have successively more triangles in them, add
plt.clf()

to your function before your

plt.plot(...)

command.

3.3.3 P&E 1.39

  • Use the equations in the handout and not the book - note especially that the contributing factors are limited to numbers between 0 and 2.375.
  • The link is NFL Player Stats - QB Rating from TeamRankings.

3.3.4 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.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.

3.4.5 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 = 0 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!

On checking your random numbers

You can check your work by using mrg and 5000 as the inputs - 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.

General Concepts

This section is not in the lab report but rather has some items in it that span multiple problems in the lab.

  • Please do not make any changes at all to the test_ files! Also, do not include those in your lab report. Here's some information about how they work, 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 screen and in the file.
    • The seeding will take a string, convert it to 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
  • Chapter 5 in P&E (5-5.3.4 specifically) is a great place to get some help with understanding how functions are set up, as are the following links:



Class Document Protection