## Functions are arguably the most important topic in the secondary math curriculum¶

### A function accepts one or more inputs and returns an output.¶

In [1]:
# Here is a function that takes in one input x, and outputs its square, x^2.

def f(x):
return x*x

print(f(-5),f(-1),f(3),f(7))

25 1 9 49

In [2]:
# Here is a function that takes in one input x, and outputs x^2-1 if x<0 and 4x-1 otherwise

def g(x):
if x<0: return x*x-1
else: return 4*x-1

print(g(-5),g(-1),g(3),g(7))

24 0 11 27

In [3]:
# Here is a function that takes in one input x, and outputs x^2+1 if x<-2, 5 if -2<x<2, and 2x+1 otherwise

def h(x):
if x<-2: return x*x+1
elif -2<=x<2: return 5
else: return 2*x+1

print(h(-5),h(-1),h(3),h(7))

26 5 7 15


## Let's plot these three functions f(x), g(x), h(x)¶

### To do this, we import three Python libraries called "numpy", "matplotlib", and "math"¶

In [4]:
%matplotlib inline
from numpy import *
from matplotlib.pyplot import *
from math import *

x = linspace(-5,5, 100)
y1 = vectorize(f)(x)
y2 = vectorize(g)(x)
y3 = vectorize(h)(x)

plot(x, y1, 'red')
plot(x, y2, 'blue')
plot(x, y3, 'green')
title("Comparing our three functions")
xlabel("x values")
ylabel("y values");
show()


## Challenge Activity: Make Your Own Shape!¶

### Define two functions f(x) and g(x) so that the plots of f(x) and g(x) form the object of your choice.¶

#### Feel free to use whatever functions you wish, including sin, cos, abs, exp, and log.¶

In [5]:
def f(x):
if x<0: return x*x
else: return 3*x

def g(x):
if x<0: return -x*x+30
else: return -3*x+30

x = linspace(-5,5, 100)
y1 = vectorize(f)(x)
y2 = vectorize(g)(x)

plot(x, y1, 'red')
plot(x, y2, 'red')
title("Our fish")
show()


## Challenge Activity: Turn the Sad Face into a Happy Face.¶

### Play with this picture below to make the eyes, nose, and mouth look different. Be creative!¶

In [6]:
def f1(x): return sqrt(36-x*x)

def f2(x): return -sqrt(36-x*x)

def f3(x):
if -4<x<-2: return sqrt(1-(x+3)*(x+3))+3
elif -1<x<1: return sqrt(1-x*x)
elif 2<x<4: return sqrt(1-(x-3)*(x-3))+3
else: return

def f4(x):
if -4<x<-2: return -sqrt(1-(x+3)*(x+3))+3
elif -1<x<1: return -sqrt(1-x*x)
elif 2<x<4: return -sqrt(1-(x-3)*(x-3))+3
else: return

def f5(x):
if -3<x<3: return -(x*x)/6-3
else: return

x = linspace(-6,6, 10000)
y1 = vectorize(f1)(x)
y2 = vectorize(f2)(x)
y3 = vectorize(f3)(x)
y4 = vectorize(f4)(x)
y5 = vectorize(f5)(x)

plot(x, y1, 'red')
plot(x, y2, 'red')
plot(x, y3, 'red')
plot(x, y4, 'red')
plot(x, y5, 'red')

show()


## Functions are used to solve hard problems, taking in inputs to return an output.¶

### This is one of the BIG ideas of computational thinking!¶

In [7]:
# Here is a function that takes in two inputs (city and sport) and outputs the name of that city's sport team

def teamname(city, sport):
if city=="Edmonton" and sport=="Football": return "Go Eskimos"
elif city=="Edmonton" and sport=="Hockey": return "Go Oilers"
elif city=="Calgary" and sport=="Football": return "Go Stamps"
elif city=="Calgary" and sport=="Hockey": return "Go Flames"
elif city=="Vancouver" and sport=="Football": return "Go Lions"
elif city=="Vancouver" and sport=="Hockey": return "Go Canucks"
else: return "Toronto Sucks"

print(teamname("Edmonton", "Hockey"))
print(teamname("Edmonton", "Football"))
print(teamname("Edmonton", "Soccer"))

Go Oilers
Go Eskimos
Toronto Sucks


### Can you spot the different FUNCTIONS that appear in the breakthrough technology shown below?¶

In [8]:
from IPython.display import YouTubeVideo