# 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
# 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
# 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
%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()
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()
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()
# 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
from IPython.display import YouTubeVideo
YouTubeVideo('Nu-nlQqFCKg',start=387)