Search code examples
pythonnumpyscipynumerical-methodsnumerical-computing

Python numerical solution to an exponential function with two variables


I have an exponential function with two known variables, x, and y. I need to find the value of y when I input an x. However, my code could not go through and solve the problem. My function and all relevant constants are given below:

import math
import numpy as np
import scipy.optimize as optimize

x1=np.array([0,20])

Vt = 0.026
Io = 23*math.pow(10,-10)
Iph = 2.282
idf = 1
Ns = 60 
Nm = 1 
Rse = 0.5
Rsh = 1000
x = np.linspace(x1.min(),x1.max(),300)    
def equation(x,Iph,Io,Rse,Rsh,Ns,Nm,Vt):
    return y - Iph + Io*(np.exp((x+y*Rse)/(Ns*Nm*idf*Vt))-1) + x/Rsh + y*Rse/Rsh 
y = optimize.newton(equation(10,Iph,Io,Rse,Rsh,Ns,Nm,Vt), 7)

Present output:

 File "<ipython-input-172-93ede88c9b49>", line 16, in ivcurve_equation
    return y - Iph + Io*(np.exp((x+y*Rse)/(Ns*Nm*idf*Vt))-1) + v/Rsh + I*Rse/Rsh 

TypeError: can't multiply sequence by non-int of type 'float'

Expected output:

y = a real and positive value # >0

Solution

  • Have a quick look at the docs and try to do some 'pattern matching'. The parameters of equation should only be variables and not constants. Here is a working version of your code, that you should tailor to your needs:

    import math
    import numpy as np
    import scipy.optimize as optimize
    
    x1=np.array([0,20])
    
    Vt = 0.026
    Io = 23*math.pow(10,-10)
    Iph = 2.282
    idf = 1
    Ns = 60
    Nm = 1
    Rse = 0.5
    Rsh = 1000
    x_arr = np.linspace(x1.min(),x1.max(),300)
    x = x_arr[0]
    def equation(y):
        return y - Iph + Io*(np.exp((x+y*Rse)/(Ns*Nm*idf*Vt))-1) + x/Rsh + y*Rse/Rsh
    
    result = optimize.newton(equation, 7)
    
    print(result)
    

    Now if you want the output for an array of x's try this:

    def equation(y,x):
        return y - Iph + Io*(np.exp((x+y*Rse)/(Ns*Nm*idf*Vt))-1) + x/Rsh + y*Rse/Rsh
    
    result = [optimize.newton(equation, 7, args = (a,)) for a in x_arr]
    
    print(result)
    

    Hope this helps!