python - 具有两个变量的指数函数的 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
解决方案
快速查看文档并尝试进行一些“模式匹配”。的参数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)
希望这可以帮助!
推荐阅读
- javascript - 强制结束nodejs中的路由
- azure - 从 Octopus 部署 Azure 功能
- java - 在滑动窗口上重复有效地计算线性回归模型
- postgresql - 基于 Postgresql 中的日期/时间的行到列
- ruby - RSpec 在 POST 中失败并出现错误:失败/错误未实现:调用
- javascript - js json如何从json中获取值并将值传递给其他json
- php - 使用 Laravel 计算列表中的年龄
- angular - 'typeof import 不可分配给类型'Provider'
- r - 如何在 R 中使用 read.excel() 指定列 as.factor?
- c# - c# 为属性添加逻辑