matens
matens

Reputation: 31

Calculating and plotting parametric equations in sympy

So i'm struggling with these parametric equations in Sympy.

𝑓(𝜃) = cos(𝜃) − sin(𝑎𝜃) and 𝑔(𝜃) = sin(𝜃) + cos(𝑎𝜃) with 𝑎 ∈ ℝ∖{0}.

import matplotlib.pyplot as plt
import sympy as sp
from IPython.display import display

sp.init_printing()
%matplotlib inline

This is what I have to define them:

f = sp.Function('f')
g = sp.Function('g')
f = sp.cos(th) - sp.sin(a*th)
g = sp.sin(th) + sp.cos(a*th)

I don't know how to define a with the domain ℝ∖{0} and it gives me trouble when I want to solve the equation 𝑓(𝜃)+𝑔(𝜃)=0 The solution should be:

𝜃=[3𝜋/4,3𝜋/4𝑎,𝜋/2(𝑎−1),𝜋/(𝑎+1)]

Next I want to plot the parametric equations when a=2, a=4, a=6 and a=8. I want to have a different color for every value of a. The most efficient way will probably be with a for-loop. I also need to use lambdify to have a list of values but I'm fairly new to this so it's a bit vague.

This is what I already have:

fig, ax = plt.subplots(1, figsize=(12, 12))
theta_range = np.linspace(0, 2*np.pi, 750)
colors = ['blue', 'green', 'orange', 'cyan']
a = [2, 4, 6, 8]

for index in range(0, 4):
    # I guess I need to use lambdify here but I don't see how
plt.show()

Thank you in advance!

Upvotes: 0

Views: 3006

Answers (1)

JohanC
JohanC

Reputation: 80289

You're asking two very different questions. One question about solving a symbolic expression, and one about plotting curves.

First, about the symbolic expression. a can be defined as a = sp.symbols('a', real=True, nonzero=True) and theta as th = sp.symbols('theta', real=True). There is no need to define f and g as sympy symbols, as they get assigned a sympy expression. To solve the equation, just use sp.solve(f+g, th). Sympy gives [pi, pi/a, pi/(2*(a - 1)), pi/(a + 1)] as the result.

Sympy also has a plotting function, which could be called as sp.plot(*[(f+g).subs({a:a_val}) for a_val in [2, 4, 6, 8]]). But there is very limited support for options such as color.

To have more control, matplotlib can do the plotting based on numpy functions. sp.lambdify converts the expression: sp.lambdify((th, a), f+g, 'numpy').

Then, matplotlib can do the plotting. There are many options to tune the result.

Here is some example code:

import matplotlib.pyplot as plt
import numpy as np
import sympy as sp

th = sp.symbols('theta', real=True)
a = sp.symbols('a', real=True, nonzero=True)

f = sp.cos(th) - sp.sin(a*th)
g = sp.sin(th) + sp.cos(a*th)
thetas = sp.solve(f+g, th)
print("Solutions for theta:", thetas)

fg_np = sp.lambdify((th, a), f+g, 'numpy')

fig, ax = plt.subplots(1, figsize=(12, 12))
theta_range = np.linspace(0, 2*np.pi, 750)
colors = plt.cm.Set2.colors
for a_val, color in zip([2,4,6,8], colors):
    plt.plot(theta_range, fg_np(theta_range, a_val), color=color, label=f'a={a_val}')
plt.axhline(0, color='black')
plt.xlabel("theta")
plt.ylabel(f+g)
plt.legend()
plt.grid()
plt.autoscale(enable=True, axis='x', tight=True)
plt.show()

example plot

Upvotes: 2

Related Questions