【问题标题】:Python Integration: to calculate area under the curvePython集成:计算曲线下的面积
【发布时间】:2021-10-24 03:06:39
【问题描述】:

我想在下面的代码中求输出功率Po的积分:

Vo = 54.6

# defining a function for duty cycle, output current and output power
def duty_cycle(output_voltage, array_voltage):
    duty_cycle = np.divide(output_voltage, array_voltage)
    return duty_cycle

def output_current(array_current, duty_cycle):
    output_current = np.divide(array_current, duty_cycle)
    return output_current

def output_power(output_voltage, output_current):
    output_power = np.multiply(output_voltage, output_current)
    return output_power

#calculating duty cycle, output current and output power
D = duty_cycle(Vo, array_params['arr_v_mp'])
Io = output_current(array_params['arr_i_mp'], D)
Po = output_power(Vo, Io)

#plot ouput power
plt.ylabel('Output Power [W]')
Po.plot(style='r-')

上面的代码只是脚本的一部分。 array_params 是 pandas 时间序列数据框。绘制熊猫系列Po时,是这样的:

这是我第一次使用 python 计算积分。通过互联网阅读后,我认为 Python 的 scipy 模块可能会有所帮助,但不知道如何实现以及使用哪种方法。感谢您以任何方式解决上述问题。

【问题讨论】:

  • 既然数组中有离散值,你不只想要那个数组的总和吗?
  • @Mark 我也有同样的想法,但根据我的任务,我必须绘制 ``` Po ``` 的积分,就像我绘制 Po 的方式一样。总之,我只会得到一个值。

标签: python pandas numpy scipy


【解决方案1】:

要计算从 x0 到 x1 的 int y(x) dx 形式的积分,数组 x_array 的值从 x0 到 x1 以及对应的相同长度的 y_array,可以使用 numpy 的梯形积分:

integral = np.trapz(y_array, x_array)

这也适用于非恒定间距x_array[i+1]-x_array[i]

【讨论】:

    【解决方案2】:

    如果需要不定积分(即积分F(t) = integral f(t) dt),请使用scipy.integrate.cumtrapz(而不是numpy.trapz 用于定积分)。

    integrated = scipy.integrate.cumtrapz(power, dx=timestep)
    

    integrated = scipy.integrate.cumtrapz(power, x=timevalues)
    

    要使integratedpower 具有相同的长度,请指定积分的初始值,通过可选参数initial(例如initial=0)到scipy.integrate.cumtrapz

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 2018-01-28
      • 2021-04-18
      • 1970-01-01
      • 2023-01-12
      • 2012-01-29
      • 2021-06-08
      相关资源
      最近更新 更多