如何求解方程两侧具有函数形式的解变量的方程

时间:2018-05-19 10:14:26

标签: python python-3.x numpy sympy

我想在Python中绘制一个方程图,它在某些函数形式的两侧都有一个解决方案变量。 等式是:

i = Ip - Io*(exp((V+i*R1)/(n*Vt)) - 1) - (V +I*R1)/(R2)

其中Ip, Io, n, R1, R2, Vt是一些常量。

我想在V范围内迭代(0,10),并希望使用Python获取i的值并绘制V-i图。

import numpy as np
from sympy import *
import matplotlib.pyplot as plt
r = 50
V = np.linspace(0,10,r)

def current():
    current = []
    for t in V:
        i = np.zeros(r)
        Ipv = 3
        Rs = 0.221
        Rsh = 415
        n = 2
        m = 1.5
        T = 302
        Eg = 1.14
        K = 1.3
        Vt = T/11600
        Io = K*(T**m)*exp(-Eg/(n*Vt))
        i = Ipv - Io *(exp((t + Rs*i)/(n*Vt)) - 1) - (t + Rs * i)/Rsh
        current.append(i)
    return np.array(current)

Icurrent = current()
plt.plot(V,Icurrent)
plt.show()

我这样做了但这不起作用。

欢迎任何建议。

1 个答案:

答案 0 :(得分:1)

看来,您的问题是您将numpy数组与标量math函数混合在一起。 Don't do this.用适当的numpy函数替换它:

import numpy as np
import matplotlib.pyplot as plt
r = 50
V = np.linspace(0,10,r)
print(V)

def current():
    current = []
    for t in V:
        i = np.zeros(r)
        Ipv = 3
        Rs = 0.221
        Rsh = 415
        n = 2
        m = 1.5
        T = 302
        Eg = 1.14
        K = 1.3
        Vt = T/11600
        Io = K*(T**m)*np.exp(-Eg/(n*Vt))
        i = Ipv - Io *(np.exp((t + Rs*i)/(n*Vt)) - 1) - (t + Rs * i)/Rsh
        current.append(i)
    return np.array(current)

Icurrent = current()
plt.plot(V,Icurrent)
plt.show()

输出:
enter image description here

我建议使用scipy.fsolve,但看起来你的方法正在发挥作用。