Search code examples
numpymathscipyscipy-optimizefsolve

How to solve a set of nonlinear equations multiple times when one of the coefficient of the variable changes


I want to solve for the variables s,L for different values of t. t is a part of my second equation and its values changes, I tried to solve for s,L for different t values then append the values to an empty list so that i could have different values of s, L for diiferent t values. But what i was getting is just an empty list.PLease help me with this enter image description here

from  scipy.optimize import fsolve
import numpy as np
import math as m
q0=0.0011
thetas,thetai,thetar=0.43,0.1,0.05
ks=0.0022#m/hr
psib=-0.15# m
lamda=1
eta=2+3*lamda
ki=8.676*10**(-8)
si=0.13157
t=np.array([3,18,24])

S=0.02/24
delta=-0.1001
b=[]
n=[]
for i in range(3):
    def equations(p):
        s, L = p
        f1=(ks*s**(3+(2/lamda))-(psib/(1-eta))*(((ki*si**(-1/lamda))-(ks*s**(3+(1/lamda))))/L)-q0)
        f2=(L*(s*(thetas-thetar))+S*t[i]*0.5*(m.exp(-delta*psib*(-1+s**(-1/lamda))))-(q0-ki)*t[i])
        return(f1,f2)

        s,L=fsolve(equations,([0.19,0.001]))
        b.append(s)
        n.append(L)
print(b)
print(n)

Solution

  • There are several ways to evaluate this system with an adjustable parameter. You could plug each value in before solving, which would make it compatible with additional solvers if fsolve didn't give you the desired results, or you could utilize the args parameter within fsolve. If I set up a dummy system such that I try to find x,y,z for some initial guess, and step through a parameter, I can append a preallocated solution array with the results

    import numpy as np
    from scipy.optimize import fsolve
    
    a = np.linspace(0,10,21)
    
    def equations(variables, a):
        x,y,z = variables
        eq1 = x+y+z*a
        eq2 = x-y-z
        eq3 = x*y*x*a   
        return tuple([eq1, eq2, eq3])
    
    solutions = np.zeros((21,3))
    for idx, i in enumerate(a):
        solutions[idx] = fsolve(equations, [-1,0,1], args=(i))
    
    print(solutions)
    

    which gives

    [[ 5.00000000e-01 -5.00000000e-01  1.00000000e+00]
     [ 9.86864911e-17 -2.96059473e-16  3.94745964e-16]
     [ 1.62191889e-39 -1.28197512e-16  1.28197512e-16]
     [-2.15414908e-17 -1.07707454e-16  8.61659633e-17]
     [ 2.19853562e-28  6.59560686e-28 -4.39707124e-28]
     [-1.20530409e-28 -2.81237621e-28  1.60707212e-28]
     [-3.34744837e-17 -6.69489674e-17  3.34744837e-17]
     [ 6.53567253e-17  1.17642106e-16 -5.22853803e-17]
     [-3.14018492e-17 -5.23364153e-17  2.09345661e-17]
     [-5.99115518e-17 -9.41467242e-17  3.42351724e-17]
     [ 5.18199815e-29  7.77299722e-29 -2.59099907e-29]
     [-2.70691440e-17 -3.90998747e-17  1.20307307e-17]
     [-2.57288510e-17 -3.60203914e-17  1.02915404e-17]
     [-2.44785120e-17 -3.33797891e-17  8.90127708e-18]
     [-1.27252940e-28 -1.69670587e-28  4.24176466e-29]
     [ 2.24744956e-56  2.93897250e-56 -6.91522941e-57]
     [-2.12580678e-17 -2.73318015e-17  6.07373366e-18]
     [-2.03436865e-17 -2.57686696e-17  5.42498307e-18]
     [-3.89960988e-17 -4.87451235e-17  9.74902470e-18]
     [-1.87148635e-17 -2.31183608e-17  4.40349730e-18]
     [-7.19531738e-17 -8.79427680e-17  1.59895942e-17]]