In [18]:
import numpy as np
import matplotlib.pyplot as plt
$y = Mx + c$
Can be re-formated though:
$ax + by + c = 0$
Can find equation of line given known point and gradient
x = 3
y = 6
M = 4
$6 = 4\times 3 + c$
$c = -6$
$y = 4x - 6$
$M = \frac{y_2 - y_1}{x_2 - x_1}$ or $M = \frac{dy}{dx}$
Can be used to find gradient from 2 known points (and used to find equation after)
$x_1 = 2$
$y_1 = -1$
$x_2 = 7$
$y_2 = -15$
$M = \frac{-15 + 1}{7 - 2}$
$M = \frac{-14}{5}$
$M = -2\frac{4}{5}$
We can also use this re-formated equation to find the full equation of the line
$(y - y_1) = M(x - x_1)$
With $x_1$ and $y_1$ and $M$ as our known variables
$(y - 6) = 4(x - 3)$
$y = 4x - 6$
In [19]:
# x data
x = np.linspace(-10, 10, 2001).astype(np.float32)
# variables
x1 = 2
x2 = 7
y1 = -1
y2 = -15
# gradient function
def grad(x1, x2, y1, y2):
return((y2-y1)/(x2-x1))
# line function
def f(y1, x1, x, M):
c = y1 - M*x1
return(M*x + c, c)
# get the gradient
M = grad(x1, x2, y1, y2)
# get the y values
# also get the c value to use in our plot key
y, c = f(y1, x1, x, M)
# set up plot
fig, ax = plt.subplots(figsize=(8,4))
ax.plot(x,y, label='y = %sx + %s' % (M, c))
ax.legend()
ax.grid(True)
plt.show()
In [20]:
# x data
x = np.linspace(-10, 10, 2001).astype(np.float32)
# variables
M = 3.0
c = 2.0
# line function
def f(M, x, c):
return(M*x + c)
# parallel line function
def par_f(M, x, c):
new_c = c-4
return(M*x + new_c, new_c)
# perp line function
def perp_f(M, x, c):
new_M = -np.reciprocal(M)
return(new_M*x + c, new_M)
# get the y values
y = f(M, x, c)
par_y, par_c = par_f(M, x, c)
perp_y, perp_M = perp_f(M, x, c)
# set up plot
fig, ax = plt.subplots(figsize=(4,4))
ax.plot(x,y, label='y = %sx + %s' % (M, c))
ax.plot(x,par_y, label='y = %sx + %s' % (M, par_c))
ax.plot(x,perp_y, label='y = %sx + %s' % (perp_M, c))
ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
ax.grid(True)
plt.xlim(-2, 4)
plt.ylim(-2, 4)
plt.show()
This is the same as pythagoras's theorem
with (x,y) as point on radius with (a,b) as point at centre of circle
$(x-a)^2+(y-b)^2=r^2$
Circles come as this equation:
$Ax^2 + Ay^2 + Bx + Cy + D = 0$
$\left(\sqrt{A}x + \frac{B}{2\sqrt{A}}\right)^2 - \left(\frac{B}{2\sqrt{A}}\right)^2 + \left(\sqrt{A}y + \frac{C}{2\sqrt{A}}\right)^2 - \left(\frac{C}{2\sqrt{A}}\right)^2 = -D$
$\left(\sqrt{A}x + \frac{B}{2\sqrt{A}}\right)^2 + \left(\sqrt{A}y + \frac{C}{2\sqrt{A}}\right)^2 = \left(\frac{C}{2\sqrt{A}}\right)^2 + \left(\frac{B}{2\sqrt{A}}\right)^2 - D$
note: the right hand side should be $r^2$
$x^2 + y^2 - 2x - 4y - 4 = 0$
$x^2 - 2x + y^2 - 4y = 4$
$(x - 1)^2 + (y - 2)^2 = 4 + (-2)^2 + (-1)^2$
$(x - 1)^2 + (y - 2)^2 = 3^2$
Useful for:
circles expressed as:
$x^2 + y^2 = r^2$
parametric equations:
$x = r\cos(\theta))$
$y = r\sin(\theta))$
parabolas expressed as:
$y^2=4ax$
parametric equations:
$x=at^2$
$y=2at$
note: $t = \sqrt{\frac{x}{a}}$ so $x = x$ and $y = 2a\sqrt{\frac{x}{a}}$ so $y^2 = 4ax$
Ellipse expressed as:
$\displaystyle \frac{x^2}{a^2}+\displaystyle \frac{y^2}{b^2}=1$
parametric equations:
$x = a\cos(\theta)$
$y = b\sin(\theta)$
Rectangular hyperbola expressed as:
$xy=c^2$
parametric equations:
$x=ct$
$\displaystyle y=\frac{c}{t}$
Parametric equations:
$x = at^2$
$y = 2at$
Cartesian equation in terms of y:
$t = \frac{y}{2}$
$x = a\left(\frac{y}{2a}\right)^2$
$x = \frac{y^2}{4a}$
Cartesian equation in terms of x:
$y^2 = 4ax$
This is the equation of a parabola
Parametric equations:
$x = 3 \cos(\theta)$
$y = 4 \sin(\theta)$
Cartesian equation:
$\cos(\theta) = \frac{x}{3}$
$\sin(\theta) = \frac{y}{4}$
$\cos^2(\theta) = \left(\frac{x}{3}\right)^2$
$\sin^2(\theta) = \left(\frac{y}{4}\right)^2$
Using trig identity: $\sin^2(\theta) + \cos^2(\theta) = 1$
$\left(\frac{x}{3}\right)^2 + \left(\frac{y}{4}\right)^2 = 1$
This is the equation of an ellipse
$xy = c^2$
$x = \frac{c^2}{y}$
$y = \frac{c^2}{x}$
let $t = \frac{c}{y}$
$x = ct$
$y = \frac{c^2}{ct}$
$y = \frac{c}{t}$
We could have also used a different t value like $t = \frac{1}{y}$ where:
$x = c^2t$
$y = \frac{1}{t}$