How would one write the parametric function r=e ^cos(θ) −2cos(4θ)+sin ^5 ( θ/12) in Python?

784 Views Asked by At

I've tried a few formats and ideas, but the syntax is kind of confusing. Help is appreciated, danke.

The function itself

4

There are 4 best solutions below

0
Pippet39 On
from math import exp, cos, sin

theta = 0.1  # Just as an example
r = exp(cos(theta)) - 2 * cos(4 * theta) + sin(theta / 12) ** 5
1
Simon B On

A very straightforward, but naive approach would be to just use the math library, which is a standard library in python.

import math

def r(theta):
    return math.pow(math.e, math.cos(theta)) - 2 * math.cos(4*theta) + math.pow(math.sin(theta/12), 5)

better results are possible with libraries for scientific computing, like numpy or other members of the scipy ecosystem.

0
user32882 On

How about something like this?

import matplotlib.pyplot as plt
import numpy as np

theta = np.linspace(-2*np.pi, 2*np.pi, 10)
r = np.exp(np.cos(theta)) - 2*np.cos(4*theta) + np.sin(theta/12)**5

plt.plot(theta,r)
plt.savefig('parametric.jpg')

enter image description here

1
Hugo Amorim On

Only with python built-in functios:

import math
r = math.e**(math.cos(theta)) - 2 * math.cos(4 * theta) + math.sin(theta/12)**5

With Sympy(for symbolic computation):

from sympy import Symbol, cos, sin, E
t = Symbol('Θ')
E**(cos(t)) - 2 * cos(4 * t) + sin(t/12)**5

Result: