Difference between revisions of "EGR 103/Fall 2023/Lab 4"

From PrattWiki
Jump to navigation Jump to search
(4.4.2.1 Chapra 3.6 Function)
(4.4.2.3 Chapra 3.13 Function)
Line 79: Line 79:
 
* Once the <code>while</code> loop is done (fortunately, this method is guaranteed to find an answer for any positive number relatively quickly),  convert the lists for <code>x</code> and <code>eps</code> into arrays.
 
* Once the <code>while</code> loop is done (fortunately, this method is guaranteed to find an answer for any positive number relatively quickly),  convert the lists for <code>x</code> and <code>eps</code> into arrays.
 
* Have your function return four things in the following order: the last estimate of $$x$$, the last relative change $$\epsilon$$ you calculated, the array containing the evolution of $$x$$, and the array containing the evolution of $$\epsilon$$.
 
* Have your function return four things in the following order: the last estimate of $$x$$, the last relative change $$\epsilon$$ you calculated, the array containing the evolution of $$x$$, and the array containing the evolution of $$\epsilon$$.
 +
* The following test code:<syntaxhighlight lang=python>
 +
if __name__ == "__main__":
 +
        x, eps, xev, epsev = my_sqrt(2023, 0.0001)
 +
        print(x)
 +
        print(eps)
 +
        print(xev)
 +
        print(epsev)</syntaxhighlight>should produce the following in the console:<syntaxhighlight lang=python>
 +
44.97777228809804
 +
2.5816916975177927e-08
 +
[2023.        1012.          506.99950593  255.49482394  131.70639628
 +
  73.53315924  50.52227855  45.28200985  44.97879433  44.9777723
 +
  44.97777229]
 +
[1.00000000e+02 9.99011858e+01 9.96057172e+01 9.84382690e+01
 +
9.39881670e+01 7.91115704e+01 4.55460073e+01 1.15725179e+01
 +
6.74129935e-01 2.27230748e-03 2.58169170e-08]</syntaxhighlight>
  
 
=== 4.4.3 Individual Lab Report ===
 
=== 4.4.3 Individual Lab Report ===

Revision as of 05:00, 27 September 2023

Errors / Additions

None yet!

4.1 Introduction

The main purpose of this lab will be to look at selective and iterative structures. 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 Group Gradescope Problems

You really should work to complete these during the lab.

4.4.1.1 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!'
    
  • If you also want to "see" the spaces, you can ask python to replace the spaces in a string with other characters. For instance,
    In [1]: print("Hi there".replace(" ", ":::"))
    Hi:::there
    
    It turns out the Unicode value for a box (☐) is 9744, so you can use something like
    In [1]: print(repr(mult_table(2, 3)).replace(" ", chr(9744)))
    '☐☐☐☐☐1☐☐☐2☐☐☐3\n☐1☐☐☐1☐☐☐2☐☐☐3\n☐2☐☐☐2☐☐☐4☐☐☐6\n'
    
  • 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.1.2 Chapra 2.26 Function

Note that the function itself should only calculate the values for the five required quantities and return them; it should not do any of the plotting.

4.4.1.3 Chapra 2.27 Function

Note that the function itself should only calculate the values for the two required quantities and return them; it should not do any of the plotting. Note that when you get to the moment equation and after there is a factor of EI in front of the derivative and that when you get to the loading there is a factor of -1 in front of the derivative as well.

4.4.2 Individual Gradescope

4.4.2.1 Chapra 3.6 Function

Note that you are only allowed to use the math module here, not numpy! Also, you are only allowed to use the math.atan() function, not the math.atan2() function! Given that, you need to have a program structured in such a way that, in cases where $$x=0$$, Python never even tries to calculate $$x/y$$. Try to create an efficient structure - nested if trees might be helpful with that!

4.4.2.2 Chapra 3.8 Function

Instead of displaying an error message, your program should just return the integer 0 if the user tries to run the function with a score that is less than zero or greater than 100 (remember, Gradescope has a hard time if extra stuff gets printed on the screen!).

4.4.2.3 Chapra 3.13 Function

  • Do not worry about dealing with what happens to 0 or negative numbers for this assignment.
  • You will be keeping track of the evolution of your guesses in a list called x and the evolution of your relative changes in a list called eps -- you should use lists because you do not know in advance how many of each you will have!
    • Use whatever value the function is called with as both a and your initial guess x[0].
    • Set your initial relative error, eps[0], to 100.
  • Once the while loop is done (fortunately, this method is guaranteed to find an answer for any positive number relatively quickly), convert the lists for x and eps into arrays.
  • Have your function return four things in the following order: the last estimate of $$x$$, the last relative change $$\epsilon$$ you calculated, the array containing the evolution of $$x$$, and the array containing the evolution of $$\epsilon$$.
  • The following test code:
    if __name__ == "__main__":
            x, eps, xev, epsev = my_sqrt(2023, 0.0001)
            print(x)
            print(eps)
            print(xev)
            print(epsev)
    
    should produce the following in the console:
    44.97777228809804
    2.5816916975177927e-08
    [2023.         1012.          506.99950593  255.49482394  131.70639628
       73.53315924   50.52227855   45.28200985   44.97879433   44.9777723
       44.97777229]
    [1.00000000e+02 9.99011858e+01 9.96057172e+01 9.84382690e+01
     9.39881670e+01 7.91115704e+01 4.55460073e+01 1.15725179e+01
     6.74129935e-01 2.27230748e-03 2.58169170e-08]
    

4.4.3 Individual Lab Report

4.4.3.1 Based on Chapra 2.26

See the Python:Plotting/Subplots page for how to get a single column of plots with a shared $$x$$ axis. Also, use:

fig.set_size_inches(6, 8, forward=True)

just after you create the figure to make it 6" wide and 8" tall. Be sure to include the sign change in the last equation! The values for $$L$$, $$E$$, $$I$$, and $$w_0$$ are given in the problem. There is a value for spacing between points in the problem but you will be ignoring it - instead using 101 linearly spaced points between 0 and 600 cm.


4.4.3.2 Based on Chapra 2.27

See the Python:Plotting/Subplots page for how to get two rows of two columns of plots. If you use the subplots method to make a 2x2 array, see Python:Plotting#Example for how to remove unused axes. Note that your butterfly_xy() function should take an array as a required argument and return two arrays - the function should not do the plotting! Your code should look like:

# %% Imports
# your imports here

# %% Function definition
def butterfly_xy(t_array):
# your function code here
    return xvals, yvals

# %% main script
if __name__ == "__main__":
    # code to generate t and calculate x and y using butterfly_xy()
    # code to generate and save figure

4.4.3.3 Based on Chapra 3.22

  • It would be a tremendously good idea to draw a flowchart detailing which steps lead to which other steps! That will make it much easier to figure out the structure of the if tree inside your for loop (oops - may have said too much there!)


General Concepts

This section is not in the lab report but rather has some items in it that span multiple problems in the lab. No content so far, but will be amended as needed!



Class Document Protection