Difference between revisions of "EGR 103/Fall 2023/Lab 4"
(→4.4.3.3 Based on Chapra 3.22) |
(→4.4.1.1 Multiplication Table) |
||
Line 32: | Line 32: | ||
In [3]: print(repr(thing)) | In [3]: print(repr(thing)) | ||
'Hi\nthere\nfriends!'</syntaxhighlight> | 'Hi\nthere\nfriends!'</syntaxhighlight> | ||
+ | * If you also want to "see" the spaces, you can ask python to replace the spaces in a string with other characters. For instance, <syntaxhighlight lang=python> | ||
+ | In [1]: print("Hi there".replace(" ", ":::")) | ||
+ | Hi:::there</syntaxhighlight> It turns out the Unicode value for a box (☐) is 9744, so you can use something like <syntaxhighlight lang=python> | ||
+ | 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'</syntaxhighlight> | ||
* 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 | * 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" | big_thing = big_thing + "new ending" |
Revision as of 04:48, 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:For example, look at the difference between the following calls:
print(repr(stuff))
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, It turns out the Unicode value for a box (☐) is 9744, so you can use something like
In [1]: print("Hi there".replace(" ", ":::")) Hi:::there
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.
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$.
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 calledeps
-- 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 guessx[0]
. - Set your initial relative error,
eps[0]
, to 100.
- Use whatever value the function is called with as both
- Once the
while
loop is done (fortunately, this method is guaranteed to find an answer for any positive number relatively quickly), convert the lists forx
andeps
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$$.
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!