简体   繁体   中英

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. 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.)

So, please I ask some help to know the way, (1) how I gain certain solution of 'f1=~', 'f2=~' as x, y. (2) how I draw some plot for 'sub_equation=~' and '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)). 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):

反三角函数域

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. Here's the Python Documentation for that: https://docs.python.org/3/tutorial/errors.html (go to section 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.

In other words: Try directly plotting the value of sub_equation -- that should always stay within [-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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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