具有两个变量的指数函数的Python数值解

问题描述 投票:-1回答:1

我有一个带两个已知变量x和y的指数函数。输入x时,我需要找到y的值。但是,我的代码无法解决该问题。我的函数和所有相关常量如下:

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)

当前输出:

 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'

预期输出:

y = a real and positive value # >0
python numpy scipy numerical-methods numerical-computing
1个回答
1
投票

快速浏览docs并尝试进行一些“模式匹配”。 equation的参数只能是变量,不能是常数。这是您的代码的有效版本,应根据需要进行调整:

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)

现在,如果要输出x的数组,请尝试以下操作:

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)

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.