Difference between revisions of "EGR 103/Fall 2019/Lab 4"
(→4.5.1 Sinusoidal Functions) |
(→4.5.5 Geometric Progressions) |
||
(One intermediate revision by the same user not shown) | |||
Line 27: | Line 27: | ||
=== 4.5.1 Sinusoidal Functions === | === 4.5.1 Sinusoidal Functions === | ||
− | 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.5.2 Based on P&E 4.48 === | === 4.5.2 Based on P&E 4.48 === | ||
Line 40: | Line 40: | ||
=== 4.5.5 Geometric Progressions === | === 4.5.5 Geometric Progressions === | ||
− | Among other things, this problem looks at input validation. You will definitely want to check out the section on [[Python:Iterative_Structures#while_loops While Loops]] on the [[Python:Iterative_Structures]] for help in identifying type errors with the input. You will also want to make sure you think about how you will be checking for valid inputs and then write and test the code for each problem case. | + | Among other things, this problem looks at input validation. You will definitely want to check out the section on [[Python:Iterative_Structures#while_loops|While Loops]] on the [[Python:Iterative_Structures]] for help in identifying type errors with the input. That section is much more complicated than what you need, however, because you are not taking inputs nor are you giving a second chance! Your code will likely resemble the code in the following, which is a one argument function that first sees if the value given can be turned into a float. If it can, the function returns the number squared; if it cannot, the function returns -1. |
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | def fun(a): | ||
+ | try: | ||
+ | a = float(a) | ||
+ | except Exception as uhoh: | ||
+ | print('Not floatable') | ||
+ | return -1 | ||
+ | |||
+ | return a**2 | ||
+ | </syntaxhighlight> | ||
+ | Here are sample runs: | ||
+ | <syntaxhighlight lang=bash> | ||
+ | In [1]: fun(3) | ||
+ | Out[1]: 9.0 | ||
+ | |||
+ | In [2]: fun('hi') | ||
+ | Not floatable | ||
+ | Out[2]: -1 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | You will also want to make sure you think about how you will be checking for valid inputs and then write and test the code for each problem case. | ||
== General Concepts == | == General Concepts == |
Latest revision as of 17:22, 18 September 2019
Errors / Additions
None yet!
BASH Shortcuts
During lab, we will set up your account to have two new Unix shortcuts to make working with labs a little easier. To make this work, you will need to follow the instructions at BASH Shortcuts to:
- Create a ~/.bashrc file in your home directory with the contents described on BASH Shortcuts
- Create a ~/.bash file in your home directory with the two functions (get103 and ltx) described on BASH Shortcuts
From this point forward, every time you log in those shortcuts will be available. To make them available immediately without having to log out and log back in, you will need to type
source ~/.bashrc
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
See main EGR 103 page for links to these
4.3 Getting Started
Same as it ever was.
4.4 Ungraded
These problems are good ones to look at and to solve but are not a part of the graded work. Given that, you can absolutely consult with classmates about how to get the answers to these ungraded problems but you may not share those answers outside of the members of the class.
4.5 Assignment
4.5.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.5.2 Based on P&E 4.48
The heart of this code will be nested for loops and proper format specifiers to make sure things fit.
4.5.3 P&E 2.3.8
The solution to this will require understanding recursion, which requires understanding recursion. More on that in lab.
4.5.4 Beam Deflection
While this references the Chapra book, you will not need to bring the Chapra book to lab - I will have the figure available on screen when we discuss the problem. 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.
4.5.5 Geometric Progressions
Among other things, this problem looks at input validation. You will definitely want to check out the section on While Loops on the Python:Iterative_Structures for help in identifying type errors with the input. That section is much more complicated than what you need, however, because you are not taking inputs nor are you giving a second chance! Your code will likely resemble the code in the following, which is a one argument function that first sees if the value given can be turned into a float. If it can, the function returns the number squared; if it cannot, the function returns -1.
def fun(a):
try:
a = float(a)
except Exception as uhoh:
print('Not floatable')
return -1
return a**2
Here are sample runs:
In [1]: fun(3)
Out[1]: 9.0
In [2]: fun('hi')
Not floatable
Out[2]: -1
You will also want to make sure you think about how you will be checking for valid inputs and then write and test the code for each problem case.
General Concepts
This section is not in the lab report but rather has some items in it that span multiple problems in the lab.
Determining and Locating Extrema
See Python:Plotting#Using_Different_Scales for some examples.
Logical Masks
See Python:Logical Masks.