Class 28 — Wednesday, March 30
Look both ways
Agenda
- Slides discussing pass by value vs pass by reference, and how it relates to us.
- Learn how Python can graph any function
- Write several functions to determine info about linear equation.
To do
- Check out and use CS 1112 Grading Issue/Late Work
- Look at Hands on Python.
- Look at function epistle.
- Look at www.python.org resources.
- Check out the Python Tutorial section on functions.
- Look over artifacts
Slides
Examples
- Program rearrangements.py
- Module lines.py
- Program plotacular.py
Why functions?
- Question: Why bother with functions?
- Makes modern software possible
- Question: Why bother with non-standard functions?
- Tasks important to you can be solved and reused as wanted
Program rearrangements.py
- We provide this program to you to start our investigation of plotting a graph in python
- First, we will have to add a few new modules to our Pycharm installation. The following code block shows which libraries must be imported to produce the neat graph images.
- Note the new keyword--
as
: this is to allow us to not have to type super-long module and sub-module names in order to execute the useful functions that we're interested.
import matplotlib.pyplot as plt
import numpy as np
- This program takes in a lower-bound, upper-bound and step-size as its input.
- It then feeds these values into
arange
which is just like range, but can deal withfloat
(decimal) values.
- For example, if we wanted all the half-steps from 0 to 3, we could get the sequence of x-values,
[0, 0.5, 1.0, 1.5, 2.0, 2.5]
- Next, the program calculates the value of y = x*x for every x in this sequence.
- This will produce a sequence of y-values,
[0, 0.25, 1.0, 2.25, 4.0, 6.25]
- Note that theses ordered pairs of (x,y) values will graph a parabola.
x = np.arange( lb, ub, step )
print( "x-size =", len( x ) )
y = x ** 2
print( "y-size =", len( y ) )
print( len( x ) == len( y ) )
plt.plot( x, y )
Spend some time with your neighbor running
rearrangements.py
, and experimenting with different lower and upper bounds, and step sizes to see what you can graph!
Module lines.py
and Program 'plotacular.py'
- First, open up
plotacular.py
, and notice the different operations we are tasked to write as functions for modulelines.py
lines.plot_line(m1, b1)
has been written for you, and plots the line represented byy = m*x + b
lines.find_m
takes in two ordered pairs, (x1, y1) and (x2, y2), and returns slope.
lines.find_b
takes in two ordered pairs, (x1, y1) and (x2, y2), and returns the y-intercept.
lines.find_perpendicular
takes in a slope value, m, and returns the opposite reciprocal slope.
lines.fine_y
takes in a slopem
, a y-interceptb
, and an input value ofx
, and returns the correspondingy
value.
- We'll give you a chance to try each on your own, then we will review as a class.
import matplotlib.pyplot as plt
import numpy as np
def plot_line(m, b):
lb = -10
ub = 10
step = 0.01
x = np.arange(lb, ub, step)
y = m*x + b
plt.plot(x, y)
plt.show()
return
def find_m(x1, y1, x2, y2):
...
return m
def find_b(x1, y1, x2, y2):
...
return b
def find_perpendicular(m):
...
return perp
def find_y(m, b, x):
...
return y
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |