Class 28 — Wednesday, March 30

Look both ways


Agenda


To do


Slides


Examples


Why functions?


Program rearrangements.py

import matplotlib.pyplot as plt

import numpy as np

  • [0, 0.5, 1.0, 1.5, 2.0, 2.5]
  • [0, 0.25, 1.0, 2.25, 4.0, 6.25]

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'

  • lines.plot_line(m1, b1) has been written for you, and plots the line represented by y = 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 slope m, a y-intercept b, and an input value of x, and returns the corresponding y value.

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.