我希望从两个非线性方程中获得一些解(x和y)。 所以我写了一些代码,并插入方程,但它不起作用。Python:如何计算2个变量非线性方程或在python中绘制这些方程?
据我所知,问题产生于f2 = math.acos(~~~),即“ValueError:math domain error” (实际上,当我擦除math.acos并显示一些错误但特定解决方案。)
所以,请问一些帮助知道方法, (1)我如何获得'f1 =〜','f2 =〜'的某些解作为x,y。 (2)我如何绘制'sub_equation =〜'和'f1 =〜'的图。
我真的很想找一些帮助。谢谢。
from scipy.optimize import fsolve
import math
import numpy as np
import matplotlib.pyplot as plt
###Input###
Angle = 120.0
length_Porpyrin =18.6
length_linker = 12.5
###parameter###
length_1 = length_Porpyrin/2.0
lenght_2 = length_linker/2.0
delta = np.pi*Angle/180.0/2.0
ramda = 30.18/180.0*np.pi
bond_angle = 2.0*np.pi/3.0
length_d = 1.35
def equations(p):
x,y = p
### modified Variable ###
atr1 = np.arctan(length_1/x)
atr2 = np.arctan(lenght_2/y)
sub_equation = (length_d ** 2+(y/np.cos(np.arctan(lenght_2/y))) ** 2-(x/np.cos(np.arctan(length_1/x))) ** 2)*np.cos(np.arctan(lenght_2/y))/(2 * length_d * y)
##########################
f1 = ( (x/np.cos(np.arctan(length_1/x))) ** 2 + (y/np.cos(np.arctan(lenght_2/y))) ** 2 - 2 *(x/np.cos(np.arctan(length_1/x))) * (y/np.cos(np.arctan(length_1/x))) * np.cos(ramda-np.arctan(length_1/x)-np.arctan(lenght_2/y)) ) - length_d ** 2
f2 = math.acos(sub_equation) - (bond_angle -(np.pi-np.arctan(lenght_2/y)-delta))
return (f1, f2)
solution = fsolve(equations, (25,25))
radius1 = solution[0]
radius2 = solution[1]
print('[solution]')
print(solution)
print('radius1', radius1)
print('radius2', radius2)
是的,我明白了。我需要将math.acos()的内部项限制为[-1,1]。但是如何?定义每个方程和变量(x,y)。它犯了一些错误。 – Dasol