1) This code's task is to create graphs of various algebraic, logarithmic and trigonometric functions and relations using Python's matplotlib.plyplot
module. Turning code into a graph is a process. First, I secure a list of xs
using set_width(width)
. Then I iterate through the list by substituting each x
into the function's equation. The result is a same-length list of the ys of the xs. Now that I have the xs
and the functions of the xs
, I can plug the two list into ply.plot()
and display the result. The exceptions to this process are the logarithmic and square root functions due to math domain errors.
2) How would I be able to graph a circle algebraically without creating two separate parts?
import matplotlib.pyplot as plt
import numpy as np
import math
def set_width(width):
"""Sets how many xs will be included in the graphs (\"width\" of the graph)"""
return list(range(-width, width + 1))
def linear(width):
"""Graphs a linear function via slope intercept form"""
xs = set_width(width)
def ys(m=1.0, b=0):
return [m * x + b for x in xs]
'''
"xs" and "ys" are not labeled "domain" and "range" because "all real numbers" will be limited to just a certain
list of xs and ys
'''
plt.plot(xs, ys())
plt.plot(xs, ys(3, 2))
plt.plot(xs, ys(5, -3))
plt.grid()
plt.show()
def quadratic(width):
"""Graphs a quadratic function via vertex form"""
xs = set_width(width)
def ys(a=1.0, h=0, k=0):
return [a * (x - h) ** 2 + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(1, 10, -50))
plt.plot(xs, ys(-4))
plt.grid()
plt.show()
def exponential(width):
"""Graphs an exponential function"""
xs = set_width(width)
def ys(a=1.0, b=2.0, h=0, k=0):
return [a * b ** (x - h) + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(3, 2, 4, 20))
plt.plot(xs, ys(5, 0.75))
plt.grid()
plt.show()
def absolute(width):
"""Graphs an absolute function"""
xs = set_width(width)
def ys(a=1.0, h=0, k=0):
return [a * abs(x - h) + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(4, 7))
plt.plot(xs, ys(-0.5, -4, -15))
plt.grid()
plt.show()
def square_root(width):
"""Graphs a square root function"""
def transform(a=1.0, h=0, k=0):
xs = [x for x in set_width(width) if x - h >= 0]
ys = [a * np.sqrt(x - h) + k for x in xs]
return xs, ys
parent = transform()
plt.plot(parent[0], parent[1])
twice_r5 = transform(2, 5)
plt.plot(twice_r5[0], twice_r5[1])
half_l2_u5 = transform(.5, -2, 5)
plt.plot(half_l2_u5[0], half_l2_u5[1])
plt.grid()
plt.show()
def cube_root(width):
"""Graphs a cube root function"""
xs = set_width(width)
def ys(a=1.0, h=0, k=0):
return [a * np.cbrt(x - h) + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(-3, 0, 1))
plt.plot(xs, ys(2, 4, -3))
plt.grid()
plt.show()
def sideways_parabola(height):
"""Graphs a sideways parabola (quadratic relation)"""
ys = set_width(height)
def xs(a=1.0, h=0, k=0):
return [a * (y - k) ** 2 + h for y in ys]
plt.plot(xs(), ys)
plt.plot(xs(3, 3, 3), ys)
plt.plot(xs(-2, -7, 0), ys)
plt.grid()
plt.show()
def logarithms(width):
"""Graphs a logarithmic function"""
def ys(b=2.0, a=1.0, h=0, k=0):
xs = [x for x in set_width(width) if x - h > 0]
ys = [a * math.log(x - h, b) + k for x in xs]
return xs, ys
parent = ys()
plt.plot(parent[0], parent[1])
three_r3 = ys(3, 2, 1000)
plt.plot(three_r3[0], three_r3[1])
plt.grid()
plt.show()
def sine(width):
"""Graphs a sine function"""
xs = set_width(width)
def ys(a=1.0, h=0, k=0):
return [a * np.sin(x - h) + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(3, 5))
plt.plot(xs, ys(0.5, 0, -3))
plt.grid()
plt.show()
def cosine(width):
"""Graphs a cosine function"""
xs = set_width(width)
def ys(a=1.0, h=0, k=0):
return [a * np.cos(x - h) + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(-1))
plt.plot(xs, ys(2, 7, 9))
plt.grid()
plt.show()
def tangent(width):
"""Graphs the tangent function"""
xs = set_width(width)
def ys(a=1.0, h=0, k=0):
return [a * math.tan(x - h) + k for x in xs]
plt.plot(xs, ys())
plt.plot(xs, ys(1, -10))
plt.plot(xs, ys(6, -8, 20))
plt.grid()
plt.show()
linear(15)
quadratic(15)
exponential(7)
absolute(15)
square_root(16)
cube_root(27)
sideways_parabola(15)
logarithms(10000)
sine(15)
cosine(15)
tangent(25)