# 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<-2
# 5 if -2<x<2
# and 2x+1 otherwise
def g(x):
if x<-2: return x*x+1
elif -2<=x<2: return 5
else: return 2*x+1
print(g(-5),g(-1),g(3),g(7))
26 5 7 15
%matplotlib inline
from numpy import *
from matplotlib.pyplot import *
from math import *
x = linspace(-5,5, 101)
y1 = vectorize(f)(x)
y2 = vectorize(g)(x)
plot(x, y1, 'red')
plot(x, y2, 'blue')
title("Comparing our two 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, 101)
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, 10001)
y1 = vectorize(f1)(x)
y2 = vectorize(f2)(x)
y3 = vectorize(f3)(x)
y4 = vectorize(f4)(x)
y5 = vectorize(f5)(x)
figure(figsize=(4,4))
plot(x, y1, 'red')
plot(x, y2, 'red')
plot(x, y3, 'red')
plot(x, y4, 'red')
plot(x, y5, 'red')
show()