I'm trying to solve and reproduce a solution of a system of non-linear equations using scipy.
I've learned a lot from different posts, and I could figure out some issues I had before.
It seems, however, based on the solution I posted above to my own question, that scipy.optimize
is quite sensitive to the different available methods
I'm trying to use nonlinear solvers as someone else pointed this here but I'm getting the same solutions, which I suppose are wrong. I say this because in the scientific paper I'm following (and trying to reproduce the results) they solved the same system with Mathematica
, and got a different result which is more close to what one would expect. In the past, I only posted a minimal work example of my code, but now I decided to post the real thing.
I was wondering if Mathematica
could easily manage the choice of the solver and tackle the problem. I initially wanted to preferably use Python, but right now my priority is to solve this problem, no matter the language. If someone figures out a way to solve with scipy
much better.
Follow my code:
import numpy as np
from scipy import optimize
c1 = 43.524e-3
Z1 = 1
Z2 = 3
b = 1.7e-10
q = 1.6e-19
k_B = 1.38e-23
T = 296.15
N = 6.02e23
lB=0.71e-9
xi = 4.2
v1 = 4*np.pi*np.exp(1.0)*N*(1.0+Z1)*(xi-Z1**(-1.0))*b**(3.0)
v2 = 4*np.pi*np.exp(1.0)*N*(1.0+Z2)*(xi-Z2**(-1.0))*b**(3.0)
epsilon = q**2/(xi*k_B*T*b)
I = 46.311e-3
kappa2 = (3.29e7)*c1**(0.5) * (1e2)
def f(p,*args):
theta1,theta2 = p
c2 = args[0]
eq1 = theta1-(c1*v1/10**3)*np.exp(-1-2*Z1*xi*(1-Z1*theta1-Z2*theta2)*np.log(1-np.exp(-kappa2*b)))
eq2 = theta2-c2*((v2/(10**3*np.exp(1)))*((10**3*theta1*np.exp(1))/(c1*v1))**3)
return(eq1,eq2)
c2 = np.arange(0,150e-6,1e-6)
THETA1 = []
THETA2 =[]
THETA = []
for i in c2:
sol=optimize.root(f,np.array([0.12024367, 0.00062351]),args=(i),method='hybr')
theta1,theta2 = sol.x[0],sol.x[1]
sol.message
theta=3*theta2+theta1
THETA1.append(theta1)
THETA2.append(theta2)
THETA.append(theta)
print (theta1,theta2,theta)
What is supposedly wrong is that theta1
drops down extremelly fast, the numerical values for the two first interaction are:
theta1,theta2,theta
0.12154778645003049, 6.444608966498463e-30, 0.12154778645003049
6.961522754431233e-05, 0.17477403029874458, 0.5243917061237781
And here is an evolution of theta1, theta2 and theta as a function of c2.
EDIT Results from the aforementioned paper: