问题
i have an assignment on sympy and am struggling with the following question:
"Prove with the help of Sympy that 4*(x2 + y2 -ax)3 = 27a2(x2+y2)2 can be written using r = 4a*cos(theta/3)3".
I have tried to substitute x = r*cos(theta)
and y = r*sin(theta)
.
Then I tried sp.solveset(eq, r)
but I only got a very longset of {}, nothing like the given polar equation.
Does anyone know how to do this (I can use sympy and numpy)?
回答1:
The following code builds the equation from its left hand side and right hand side. Then the change of change of variables to polar coordinates is performed using substitution.
The resulting trigonometric expression is then simplified, and it turns out to be zero after simplification. So any pair/tuple (x,y)=(r*cos(theta),r*sin(theta))
is a solution.
from sympy import *
a,x,y,theta = symbols('a x y \Theta', real=True)
init_printing(use_latex=True)
lhs = 4 * (x**2 + y**2 - a*x) ** 3
rhs = 27 * a**2 * (x**2 + y**2)**2
f = lhs - rhs
r = 4 * a * cos(theta/3)**3
display(f,"----")
f = f.subs(x,r*cos(theta))
f = f.subs(y,r*sin(theta))
display(f,"----")
f1 = f
display(simplify(f))
# format for wolframalpha
t = symbols('t')
f1 = f1.subs(theta,t)
import re
f1 = re.sub("\*\*","^",str(f1))
print("----")
print("wolframalpha expression: solve ", str(f1)," over the reals")
To double-check this, at the end, a wolframalpha query is also generated, which confirms the solutions.
来源:https://stackoverflow.com/questions/65294894/how-to-proof-with-sympy-that-a-given-cartesian-equation-can-be-written-as-a-give