-1

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
meph
  • 662
  • 3
  • 10
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 1
    can you let me know which one is a list or sequence in this line `return y - Iph + Io*(np.exp((x+y*Rse)/(Ns*Nm*idf*Vt))-1) + v/Rsh + I*Rse/Rsh ` – gsb22 Feb 13 '20 at 22:03
  • @GauriShankarBadola Please, I edited equation correctly now. Yes! both x and y are lists. But, at the moment I am inputing one x value. I am trying to find value of y for that x. – Mainland Feb 13 '20 at 22:05
  • 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 In this method, where y is coming from? – gsb22 Feb 13 '20 at 22:09
  • and also `v`...? – Alexander Feb 13 '20 at 22:10
  • @Alexander it was a typo. sorry for that. I edited for that. – Mainland Feb 13 '20 at 22:12
  • @GauriShankarBadola That's! right. y is the value of variable that I need to find. True that I could not make `y` part of function variables or maybe I am defining the function wrongly. – Mainland Feb 13 '20 at 22:14
  • if there is no variable inside the function, then how come you are subtracting something from that? Your code is not correct I suppose. You can share a snip of the code you are running. – gsb22 Feb 13 '20 at 22:22
  • Basically I am trying to solve 'y - Iph + Io*(np.exp((x+y*Rse)/(Ns*Nm*idf*Vt))-1) + x/Rsh + y*Rse/Rsh =0'. – Mainland Feb 13 '20 at 22:36

1 Answers1

1

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!

meph
  • 662
  • 3
  • 10
  • Great! it worked. I have a question. Whether inputing `fprime`, `fprime2` helps above equation in any way? – Mainland Feb 13 '20 at 23:31
  • 1
    Regarding `fprime` and `fprime2`: Sure, it helps. The newton method converges better, if the first and second derivatives are set, but the tricky part is calculating them. Regarding the array question: I will update my answer – meph Feb 14 '20 at 01:29