如何在 Python 中编写参数函数 r=e ^cos(θ) −2cos(4θ)+sin ^5 ( θ/12)?

问题描述

我尝试了一些格式和想法,但语法有点令人困惑。感谢帮助,丹克。

The function itself

解决方法

仅使用 python 内置函数:

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

使用 Sympy(用于符号计算):

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

结果:

,
from math import exp,sin

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

一个非常简单但天真的方法是只使用 math 库,它是 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)

使用用于科学计算的库(例如 numpy 或 scipy 生态系统的其他成员)可能会获得更好的结果。

,

这样的事情怎么样?

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