import numpy as np
import matplotlib.pyplot as plt
Parabola, defined locus of points P such that the distance from a line (called the directrix) to P is equal to the distance from to a fixed point (called the focus).
The code plots the parabola $$ y = ax^2 + bx + c $$
with $$ a = 1, b = 0, c = 0. $$ It calculates the vertex at (h, k) with h = 0, k = 0.
The foucs is at $$ (h, k + \frac{1}{4a}), \text{ and the directrix is at } y = k - \frac{1}{4a}. $$
$$ \text{A tangent line at } (x_{\text{point}}, y_{\text{point}}) \text{ is drawn, with slope } m = 2ax_{\text{point}} + b. $$
lim_max, lim_min = 5, -5
a = 1
b = 0
c = 0
h = -b / (2 * a)
k = c - (b**2) / (4 * a)
generate_title = lambda a, b, c: (
'Parabola: y = ' +
(f'{"x²" if a == 1 else f"{a}x²"}' if a != 0 else '') +
(f' + {"x" if b == 1 else f"{b}x"}' if b != 0 else '') +
(f' + {c}' if c != 0 else '')
).replace(' + -', ' - ')
x_point = 2
y_point = a * x_point**2 + b * x_point + c
focus_y = k + 1 / (4 * a)
directrix_y = k - 1 / (4 * a)
x = np.linspace(-10, 10, 200)
y = a * x**2 + b * x + c
plt.xlim(lim_min, lim_max)
plt.ylim(lim_min, lim_max)
plt.gca().set_aspect('equal', adjustable='box')
plt.grid(True, linestyle='--', alpha=0.6)
plt.plot(x, y, label=generate_title(a,b,c))
directrix = np.full(200, directrix_y)
plt.plot(x, directrix, label='Directrix')
plt.plot(h, focus_y, marker="o", markersize=3, color="black", label='Focus')
plt.plot(x_point, directrix_y, marker="o", markersize=3, color="black")
plt.plot([h, x_point], [focus_y, y_point], color="black")
plt.plot([x_point, x_point], [y_point, directrix_y], color="black")
radius = np.sqrt((x_point - h)**2 + (y_point - focus_y)**2)
theta = np.linspace(0, 2 * np.pi, 100)
circle_x = radius * np.cos(theta) + x_point
circle_y = radius * np.sin(theta) + y_point
plt.plot(circle_x, circle_y, color="blue", label='Circle')
slope = 2 * a * x_point + b
intercept = y_point - slope * x_point
diffx = np.linspace(-10, 10, 200)
diffy = slope * diffx + intercept
plt.plot(diffx, diffy, color="red", label='Tangent')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title(generate_title(a,b,c))
plt.legend()
plt.show()
The code plots a parabola defined parametrically as:
$$ (x(t), y(t)) = \left(t, \frac{1}{4} t^2\right) $$
The derivative of the curve is given by:
$$ \frac{dx}{dt} = 1, \quad \frac{dy}{dt} = \frac{1}{2} t $$
Tangents to the curve at various points ( t ) are plotted using the equation:
$$ y_t = y_0 + \frac{dy}{dx}(x - x_0) $$
For each $$(t)$$ a tangent line is drawn in red if $$ (t < 0) $$ and in purple if $$ ( t \geq 0 ).$$
# Tangents of a Parabola
# Translated to python from xahlee's wolfram scripts
# http://xahlee.info/SpecialPlaneCurves_dir/Parabola_dir/parabola.html
def curve(t):
return t, (1/4) * t**2
def derivative(t):
return 1, (1/2) * t
t_values = np.arange(-10, 10.5, 0.5)
t_smooth = np.linspace(-10, 10, 400)
x_curve, y_curve = curve(t_smooth)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(x_curve, y_curve, 'k', linewidth=2)
for t in t_values:
x_t, y_t = curve(t)
dx, dy = derivative(t)
tangent_x = np.linspace(x_t - 50, x_t + 50, 2)
tangent_y = y_t + (dy/dx) * (tangent_x - x_t)
color = 'red' if t < 0 else 'purple'
ax.plot(tangent_x, tangent_y, color=color, linewidth=1.5)
ax.plot(x_t, y_t, 'bo', markersize=5)
ax.set_xlim(-3, 3)
ax.set_ylim(-4.5, 3)
ax.axis('off')
ax.set_title("parametric curve with tangents")
plt.show()
# Optical properties of a Parabola
# Translated to python from xahlee's wolfram scripts
# http://xahlee.info/SpecialPlaneCurves_dir/Parabola_dir/parabola.html
def parabola(x):
return (1/4) * x**2
x_vals = np.linspace(-10, 10, 400)
y_vals = parabola(x_vals)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(x_vals, y_vals, 'k', linewidth=2, label="Parabola")
ax.plot(0, 1/4, 'ro', markersize=6, label="Focus")
for x_p in np.linspace(-8, 8, 8):
y_p = parabola(x_p)
ax.plot([0, x_p], [1/4, y_p], 'b--', linewidth=1)
rx = [x_p, x_p]
ry = [y_p, y_p + 10]
ax.plot(rx, ry, 'r--', linewidth=1.5)
ax.set_xlim(-10, 10)
ax.set_ylim(-2, 10)
ax.set_xlabel("x-axis")
ax.set_ylabel("y-axis")
ax.set_title("reflection of rays from parabola's focus into parallel lines")
ax.legend()
ax.grid(True)
plt.show()
def transform(t, v):
x = t + v
y = (t**2 + 2*t*v) / 4
return x, y
p, q = 0, -3
r = 1.0
theta = np.linspace(0, 2*np.pi, 1000)
t_circle = p + r * np.cos(theta)
v_circle = q + r * np.sin(theta)
x_proj, y_proj = transform(t_circle, v_circle)
t_values = np.arange(-10, 10.5, 0.5)
t_smooth = np.linspace(-10, 10, 400)
x_curve = t_smooth
y_curve = (t_smooth**2)/4
plt.figure(figsize=(10, 6))
for t in t_values:
x_tangent = np.linspace(t - 10, t + 10, 100)
y_tangent = (t/2)*x_tangent - (t**2)/4
color = 'skyblue' if t < 0 else 'lightcoral'
plt.plot(x_tangent, y_tangent, color=color, alpha=0.4)
plt.plot(t_circle, v_circle, 'purple')
plt.plot(x_proj, y_proj, 'navy')
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.gca().set_aspect('equal')
plt.axis('on')
plt.title("Circle Projected onto Parabola's Tangent Grid")
plt.show()
def parabola(x): return (1/4) * x**2
def parabola_derivative(x): return (1/2) * x
def transform(t, v): return t + v, (t**2 + 2*t*v) / 4
circle_center, radius, theta = (0, 0), 4, np.linspace(0, 2*np.pi, 1000)
t_circle, v_circle = circle_center[0] + radius * np.cos(theta), circle_center[1] + radius * np.sin(theta)
x_proj, y_proj = transform(t_circle, v_circle)
x_tangent_points, x_vals = np.linspace(-10, 10, 50), np.linspace(-10, 10, 400)
y_vals = parabola(x_vals)
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x_vals, y_vals, 'k', linewidth=2, label="parabola")
for x_0 in x_tangent_points:
y_0, slope = parabola(x_0), parabola_derivative(x_0)
x_tangent = np.linspace(x_0 - 10, x_0 + 10, 100)
ax.plot(x_tangent, y_0 + slope * (x_tangent - x_0), color="skyblue", alpha=0.4)
ax.plot(x_proj, y_proj, color="purple", linewidth=2, label="projected circle")
ax.plot(t_circle, v_circle, color="skyblue", linewidth=2, label="original circle")
ax.set_xlim(-10, 10), ax.set_ylim(-10, 10), ax.set_aspect('equal')
ax.set_xlabel("x-axis"), ax.set_ylabel("y-axis"), ax.set_title("projection of a circle onto the tangent grid of a parabola")
ax.grid(True), ax.legend()
plt.show()
circle_centers = [(0, -1/4), (0, -10), (0, 10), (-10, 0), (10, 0)]
radii = np.linspace(1, 5, 5)
theta = np.linspace(0, 2 * np.pi, 1000)
x_tangent_points = np.linspace(-10, 10, 50)
x_vals = np.linspace(-10, 10, 400)
y_vals = parabola(x_vals)
fig, ax = plt.subplots(figsize=(10, 8))
ax.plot(x_vals, y_vals, 'k', linewidth=2, label="Parabola")
for x_0 in x_tangent_points:
y_0 = parabola(x_0)
slope = parabola_derivative(x_0)
x_tangent = np.linspace(x_0 - 10, x_0 + 10, 100)
y_tangent = y_0 + slope * (x_tangent - x_0)
ax.plot(x_tangent, y_tangent, color="skyblue", alpha=0.4)
for center in circle_centers:
for r in radii:
t_circle = center[0] + r * np.cos(theta)
v_circle = center[1] + r * np.sin(theta)
x_proj, y_proj = transform(t_circle, v_circle)
ax.plot(t_circle, v_circle, color="black", linestyle="--", alpha=0.4)
ax.plot(x_proj, y_proj, color="purple", linestyle='-', alpha=0.4)
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.set_aspect('equal')
ax.set_xlabel("x-axis")
ax.set_ylabel("y-axis")
ax.set_title("Projection of Multiple Circles onto the Tangent Grid of a Parabola")
ax.grid(True)
ax.legend()
plt.show()
def mandelbrot_outline(xmin, xmax, ymin, ymax, width, height, max_iter):
x = np.linspace(xmin, xmax, width)
y = np.linspace(ymin, ymax, height)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y
Z = np.zeros_like(C, dtype=complex)
mask = np.zeros(C.shape, dtype=bool)
for _ in range(max_iter):
Z = Z**2 + C
mask |= np.abs(Z) > 2
Z[np.abs(Z) > 2] = np.nan
return X, Y, mask
xmin, xmax = -2, 1
ymin, ymax = -1.5, 1.5
width, height = 500, 500
max_iter = 30
X, Y, mask = mandelbrot_outline(xmin, xmax, ymin, ymax, width, height, max_iter)
x_proj, y_proj = transform(X, Y)
fig, ax = plt.subplots(figsize=(10, 8))
ax.contour(X, Y, mask, levels=[0.5], colors='black', linewidths=1.0, linestyles='solid')
ax.contour(x_proj, y_proj, mask, levels=[0.5], colors='red', linewidths=1.0, linestyles='solid')
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.set_title("Mandelbrot Set and Parabolic Projection")
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()