InĀ [25]:
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
Exercise 1¶
InĀ [26]:
t = sp.symbols("t")
x = sp.exp(0.1*t)*sp.cos(t)
y = sp.exp(0.1*t)*sp.sin(t)
sp.plot_parametric((x,y), (t, 0, 4*sp.pi))
Out[26]:
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7760e9daa1b0>
Exercise 2¶
InĀ [27]:
t = sp.symbols("t")
x = t * sp.sin(t)
y = sp.cos(2 * t)
t0 = 7 * sp.pi / 4
p1 = sp.plot_parametric((x, y), (t, 0, 2 * sp.pi), show = False)
x_dot = sp.diff(x, t)
y_dot = sp.diff(y, t)
x_tangent = x.subs(t, t0) + t * x_dot.subs(t, t0)
y_tangent = y.subs(t, t0) + t * y_dot.subs(t, t0)
p2 = sp.plot_parametric((x_tangent, y_tangent), (t, -2, 2), show = False)
p1.extend(p2)
p1.show()
Exercise 3¶
InĀ [28]:
x, y = sp.symbols("x, y", real = True)
F = x**3 + 2*x**2*y**2 - 3*y
p1 = sp.plot_implicit(sp.Eq(F, 0), (x, -3, 3), (y, -3, 2), show=False)
p2 = sp.plot_implicit(sp.Eq(y, 0), (x, -3, 3), (y, -3, 2), show=False)
p3 = sp.plot_implicit(sp.Eq(y, 81**0.2 / 2), (x, -3, 3), (y, -3, 2), show=False)
p1.extend(p2)
p1.extend(p3)
p1.show()
InĀ [29]:
sol = sp.solve([sp.diff(F, x), F], [x, y])
sol
Out[29]:
[(0, 0), (-3**(3/5), 3**(4/5)/2)]
Exercise 4¶
InĀ [30]:
G = (x-4)**2/2+(y+1)**2/4-1
sp.plot_implicit(G, (x, 2, 6), (y, -4, 2), aspect_ratio=(1, 1))
Out[30]:
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7760ec6c0380>
InĀ [31]:
sol_x = 5
sol_y = sp.solve(G.subs(x, sol_x))
sol_y
Out[31]:
[-1 + sqrt(2), -sqrt(2) - 1]
InĀ [32]:
diff1 = -sp.diff(G, x)/sp.diff(G,y)
diff1 = diff1.subs(x, sol_x).subs(y, sol_y[0])
tangent1 = sol_y[0] + diff1 * (x-sol_x)
tangent1
Out[32]:
$\displaystyle - \sqrt{2} \left(x - 5\right) - 1 + \sqrt{2}$
InĀ [33]:
diff2 = -sp.diff(G, x)/sp.diff(G,y)
diff2 = diff2.subs(x, sol_x).subs(y, sol_y[1])
tangent2 = sol_y[1] + diff2 * (x-sol_x)
tangent2
Out[33]:
$\displaystyle \sqrt{2} \left(x - 5\right) - \sqrt{2} - 1$
InĀ [34]:
p1 = sp.plot_implicit(G, (x, 2, 6), (y, -4, 2), aspect_ratio=(1, 1), show=False)
p2 = sp.plot(tangent1, (x, 2, 6), show=False)
p3 = sp.plot(tangent2, (x, 2, 6), show=False)
p1.extend(p2)
p1.extend(p3)
p1.show()
Exercise 5¶
InĀ [35]:
H = (x-2)**2/2-(y+2)**2/4-1
sp.plot_implicit(H, (x, -2, 6), (y, -6, 2), aspect_ratio=(1, 1))
Out[35]:
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7760e82f0e30>
InĀ [36]:
sol_x = 4
sol_y = sp.solve(H.subs(x, sol_x))
sol_y
Out[36]:
[-4, 0]
InĀ [37]:
diff1 = -sp.diff(H, x)/sp.diff(H,y)
diff1 = diff1.subs(x, sol_x).subs(y, sol_y[0])
tangent1 = sol_y[0] + diff1 * (x-sol_x)
tangent1
Out[37]:
$\displaystyle 4 - 2 x$
InĀ [38]:
diff2 = -sp.diff(H, x)/sp.diff(H,y)
diff2 = diff2.subs(x, sol_x).subs(y, sol_y[1])
tangent2 = sol_y[1] + diff2 * (x-sol_x)
tangent2
Out[38]:
$\displaystyle 2 x - 8$
InĀ [39]:
p1 = sp.plot_implicit(H, (x, -2, 6), (y, -6, 2), aspect_ratio=(1, 1), show=False)
p2 = sp.plot(tangent1, (x, -2, 6), show=False)
p3 = sp.plot(tangent2, (x, -2, 6), show=False)
p1.extend(p2)
p1.extend(p3)
p1.show()
Exercise 6¶
InĀ [40]:
x = np.linspace(-5, 5, 30)
y = np.linspace(-5, 5, 30)
X, Y = np.meshgrid(x, y)
a)
InĀ [41]:
conditions = [
(X >= 0) & (Y >= 0),
(X >= 0) & (Y < 0),
(X < 0) & (Y >= 0)
]
choices = [
X + Y,
X,
Y
]
Z = np.select(conditions, choices, default=0)
plt.contour(X, Y, Z)
plt.axis('equal')
plt.show()
b)
InĀ [42]:
Z = np.sqrt(X**2 + Y**2)
plt.contour(X, Y, Z)
plt.axis('equal')
plt.show()
c) as b)
d)
InĀ [43]:
Z = np.sqrt(X**2 + 4 * Y**2)
plt.contour(X, Y, Z)
plt.axis('equal')
plt.show()
Exercise 7¶
InĀ [45]:
x, y = sp.symbols("x, y", real = True)
sp.plot_implicit((x**2+y**2)**2-(x**2-y**2), (x, -1, 1), (y, -0.5, 0.5))
Out[45]:
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7760e9d89970>
InĀ [46]:
# we have to make sure that cos(2phi)>=0
# due to numeric computation this can be a problem
# therefore add/subtract a small number 1e-10 in the next lines
phi1 = np.linspace(0*np.pi/4+1e-10, 1*np.pi/4-1e-10, 100)
phi2 = np.linspace(3*np.pi/4+1e-10, 5*np.pi/4-1e-10, 100)
phi3 = np.linspace(7*np.pi/4+1e-10, 8*np.pi/4-1e-10, 1000)
phi = np.concatenate((phi1, phi2, phi3))
x = np.sqrt(np.cos(2*phi))*np.cos(phi)
y = np.sqrt(np.cos(2*phi))*np.sin(phi)
plt.plot(x, y)
Out[46]:
[<matplotlib.lines.Line2D at 0x7760e8102120>]
Exercise 8¶
InĀ [47]:
t = np.linspace(0, 2*np.pi, 400)
x = np.cos(t)
y = np.sin(2*t)
plt.plot(x, y)
Out[47]:
[<matplotlib.lines.Line2D at 0x7760e82e19d0>]
InĀ [48]:
x, y = sp.symbols("x, y", real = True)
sp.plot_implicit(y**2-4*(1-x**2)*x**2, (x, -1, 1), (y, -1, 1))
Out[48]:
<sympy.plotting.backends.matplotlibbackend.matplotlib.MatplotlibBackend at 0x7760e9fb8620>