简体   繁体   English

Python:如何计算2个变量非线性方程或在python中将这些方程绘制在图形上?

[英]Python: How calculate 2 variable nonlinear equation or plot those equation on graph in python?

I hope to gain some solutions (x and y) from two nonlinear equations. 我希望从两个非线性方程中得到一些解(x和y)。 So I write some code, and insert the equations, but It does not work. 因此,我编写了一些代码,并插入了方程式,但是它不起作用。

As I know, The problem is generated at f2=math.acos(~~~) , that is "ValueError: math domain error" (Actually, When I erase math.acos and they show some wrong but specific solution.) 据我所知,问题是在f2 = math.acos(~~~)处生成的,即“ ValueError:数学域错误”(实际上,当我擦除math.acos时,它们显示了一些错误但具体的解决方案。)

So, please I ask some help to know the way, (1) how I gain certain solution of 'f1=~', 'f2=~' as x, y. 因此,请寻求帮助,以了解方法(1)如何获得x,y作为“ f1 =〜”,“ f2 =〜”的某种解。 (2) how I draw some plot for 'sub_equation=~' and 'f1=~'. (2)我如何绘制“ sub_equation =〜”和“ f1 =〜”的图。

I am really looking for some help. 我真的在寻找帮助。 Thank you. 谢谢。

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)

I think the error might be in the fact that when you use an inverse trig function (like arccos (acos), arcsin (asin)). 我认为错误可能是因为您使用了逆向触发函数(例如arccos(acos),arcsin(asin))。 Some inverse trig functions have domains, and if a value that happens to be plugged in that is it out of that domain, it will result in a domain error. 某些逆向触发函数具有域,并且如果恰好插入的值不在该域之内,则将导致域错误。

Below are the domains of each inverse trig function (R = all real nums): 以下是每个反三角函数 (R =所有实数):

反三角函数域

So the solution would be to put some kind of bounds on the parameter that can be entered into the inverse (arc) functions. 因此,解决方案是在可以输入反函数(弧)的参数上设置某种界限。 Or you could try handling the exception using a try except block. 或者,您可以尝试使用tryexcept块来处理异常。 Here's the Python Documentation for that: https://docs.python.org/3/tutorial/errors.html (go to section 8.3). 这是用于此的Python文档: https://docs.python.org/3/tutorial/errors.html : https://docs.python.org/3/tutorial/errors.html (转到8.3节)。

slongo has already explained what the error means: math.acos() was called with an argument either greater than 1 or smaller than -1 -- and that should never happen. slongo已经解释了错误的含义:使用大于1或小于-1的参数调用math.acos() ,这永远不会发生。

In other words: Try directly plotting the value of sub_equation -- that should always stay within [-1;1]. 换句话说:尝试直接绘制sub_equation的值,该值应始终在[-1; 1]之内。 If does not, then there is probably something wrong, either with your definition of sub_equation or with the values of the variables you put into it. 如果不是,则可能是您在sub_equation的定义或放入其中的变量的值有问题。

I assume that you have an idea of what the meaning of those equations and the individual terms is, so if there is an error in the definition of sub_equation but you can't easily find it, I'd suggest looking at the individual parts of it: define and plot them separately, to see which ones are what you'd expect and which ones are not. 我假设您对这些方程式和各个术语的含义有所了解,因此,如果sub_equation的定义有sub_equation但您无法轻易找到它,建议您查看一下它:分别定义和绘制它们,以查看哪些是您期望的,哪些不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM