Difference between revisions of "EGR 103/Spring 2022/Lab 4"

From PrattWiki
Jump to navigation Jump to search
(Created page with "<div class="noautonum">__TOC__</div> == Errors / Additions == None yet! == 4.1 Introduction == The main purpose of this lab will be to look at selective and iterative struct...")
 
Line 102: Line 102:
 
See the [[Python:Plotting]] page as well as the [https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html matplotlib.pyplot.plot] page for information about the additional pieces of information you can give the <code>plt.plot()</code> (or, in our case, <code>ax.plot()</code>) command to change sizes and colors.
 
See the [[Python:Plotting]] page as well as the [https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html matplotlib.pyplot.plot] page for information about the additional pieces of information you can give the <code>plt.plot()</code> (or, in our case, <code>ax.plot()</code>) command to change sizes and colors.
  
==4= 4.4.4.2 Chapra 3.10 / Beam Deflection ====
+
==== 4.4.4.2 Chapra 3.10 / Beam Deflection ====
 
The main concepts here are using [[Python:Logical Masks|logical masks]] to create piecewise functions, using different sets of points for mathematical analysis versus graphing, and determining and locating extrema as discussed in [[Python:Plotting|Plotting]].  Here is the test code and graph from the lab document:
 
The main concepts here are using [[Python:Logical Masks|logical masks]] to create piecewise functions, using different sets of points for mathematical analysis versus graphing, and determining and locating extrema as discussed in [[Python:Plotting|Plotting]].  Here is the test code and graph from the lab document:
 
<HTML>
 
<HTML>

Revision as of 03:03, 9 February 2022

Errors / Additions

None yet!

4.1 Introduction

The main purpose of this lab will be to look at selective and iterative structures and to learn about logical masks. The following is a companion piece to the lab handout itself. The sections in this guide will match those in the handout.

4.2 Resources

4.3 Getting Started

4.4 Assignments

4.4.1 APT

Once again, you are allowed to work on these in small groups.

4.4.2 Group Gradescope Problems

You should complete these during the lab, or at least by Sunday.

4.4.2.1 Inspired by P&E 2.1

As noted, there are several ways to think about this one. Come up with a couple of different ways that you the human would solve the problem and then figure out how to get the computer to take the same steps. You should write several test cases for yourself before submitting your code to the autograder. Hint the smallest $$n$$ digit number is $$10^{(n-1)}$$. What is the largest $$n$$ digit number? Think about using a loop and remember what mathematical operators like % and // do in Python.

4.4.2.2 Multiplication Table

  • Pre-script: The autograder expects the newline "\n" to be at the end of your string so if you *start* each row with "\n" you will need to add a "\n" to the very end before returning the string. If you want to see the control characters, you can use:
    print(repr(stuff))
    
    For example, look at the difference between the following calls:
    In [1]: thing ="Hi\nthere\nfriends!"
    
    In [2]: print(thing)
    Hi
    there
    friends!
    
    In [3]: print(repr(thing))
    'Hi\nthere\nfriends!'
    
  • The heart of this code will be nested for loops and proper format specifiers to make sure things fit. The spacing needs to be exact to pass the tests. You will basically be creating and returning, but not printing, one giant string - the good news is you can keep appending to a variable that has a string in it with code like
big_thing = big_thing + "new ending"

or

big_thing += "new ending"
  • '{:5}'.format(x) will create a string with the number x in it, reserving 5 spaces for it
  • Try to get the core of the table before figuring out how to add row or column indices
  • A double for loop is very useful here; the general structure is:
# Stuff to do before any rows
for row in ROWS:
    # Stuff to do at the start of each row
    for col in COLS:
        #Stuff to do in each column of the row
    # Stuff to do at the end of each row
# Stuff to do after all rows

Here is a trinket that might help illuminate how all this works; you may want to move the divider between the code and the output all the way to the left to see things print properly.

4.4.3 Individual Gradescope Assignments

4.4.3.1 Inspired by P&E 2.3.8

The solution to this will require understanding recursion, which requires understanding recursion. To do that, read Chapter 16 (through 16.4) in the Runestone Academy Book. Your program must use recursion to get full credit. Be sure to look at the test case (using 827) and start by getting your program to figure out that the sum of those digits is 17. Then figure out how to get the code to run itself on the number 17 to calculate (and return) 8.

This example page on Fibonacci Numbers also looks at recursion (as well as other methods). This other example page on Python Using Recursion looks at recursion as well as building a list during recursion to reduce the amount of recursion needed. The latter will not be useful for the digit sum problem but is cool nonetheless.


4.4.3.2 Based on Chapra 3.14

The main concept here is to use logical masks to create piecewise functions. The function itself actually only needs to be one line of code! One really, really long "line" of code, but still... Also - all the parentheses are important - each relational operator needs a set, the logical operators need a set, and the products need a set. And if you go over one line, you will need another set on the outside to get Python to code wrap. Be sure to look at the code we generated in class on Monday as well.

4.4.4 Individual Lab Report

4.4.4.1 Sinusoidal Functions

See the Python:Plotting page as well as the matplotlib.pyplot.plot page for information about the additional pieces of information you can give the plt.plot() (or, in our case, ax.plot()) command to change sizes and colors.

4.4.4.2 Chapra 3.10 / Beam Deflection

The main concepts here are using logical masks to create piecewise functions, using different sets of points for mathematical analysis versus graphing, and determining and locating extrema as discussed in Plotting. Here is the test code and graph from the lab document:

General Concepts

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

PythonTutor is your friend

If you are having a hard time tracing how your program is executing, you may want to put your function in pythontutor.com to get a sense of what the various variables are doing.

Determining and Locating Extrema

See Python:Plotting#Using_Different_Scales for some examples.

Logical Masks

See Python:Logical Masks.


Class Document Protection