首页 > 解决方案 > 具有两个变量的指数函数的 Python 数值解

问题描述

我有一个具有两个已知变量 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

标签: pythonnumpyscipynumerical-methodsnumerical-computing

解决方案


快速查看文档并尝试进行一些“模式匹配”。的参数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)

希望这可以帮助!


推荐阅读