【问题标题】:How can I integrate this function using the scipy.integrate library?如何使用 scipy.integrate 库集成此功能?
【发布时间】:2020-04-16 00:43:35
【问题描述】:

所以我编写了这个公式

我得到了什么:

def sumAN(theta,CoefAN,n_cl):
    # this function give us the sumatory in the right side of the formula
    Sumatorio = np.array([])
    for count,i in enumerate(theta):
        sumatorio = 0
        for count2,j in enumerate(n_cl):
            sumatorio = sumatorio +CoefAN[count2]*sin(int(count2+1)*i)
        Sumatorio = np.append(Sumatorio,sumatorio)
    return Sumatorio

cl= 4*((np.radians(alpha)+A0)*tan(theta/2)+sumAN(theta,CoefAN,n_cl))

稍微解释一下:
- 阿尔法:常数
- A0:常数
- AN : np.array([])(n 个值)
- theta:自变量
在这之后,我需要计算下一个积分:

这是我遇到问题的地方:

ch = integrate.quad(lambda theta:(4*((alpha_char+A0)*tan(theta/2)+sumAN(theta,CoefAN,n_charl)))*(cos(theta)-cos(xa))*sin(theta),0,xa)[0]

我有所有的限制和一切。 但我得到下一个错误:

'float' 对象不可迭代

我不知道如何继续。所以我的问题是:如何使用integrate.quad 方法集成这个函数?也许我会改变总结的方式?我如何以其他方式编写函数? 提前致谢

【问题讨论】:

  • 我在 ch=Integrate.quad(lambda theta:(4*((alpha_char+A0)*tan(theta/2)+sumAN(theta,CoefAN,n_charl)) 中发现了一个错误)*(cos(theta)-cos(xa))*sin(theta),0,xa)[0],应该是:ch=integrate.quad(lambda theta:-(((alpha_char+A0)*tan (theta/2)+sumAN(theta,CoefAN,n_charl)))*(cos(theta)-cos(xa))*sin(theta),0,xa)[0]
  • 你的问题有错别字,我认为第一个函数中应该是 sin 而不是 sen。
  • 是的,你是对的。第一个公式是我演示文稿中的图片,在西班牙,将鼻窦称为“sen”而不是“sin”是很常见的。无论如何谢谢;)!

标签: python numpy scipy


【解决方案1】:

这应该可行

import numpy as np
from scipy.integrate import quad

def integrand(theta, theta_a, alpha, A):
    sum = 0
    # get sum
    for index, value in enumerate(A):
        if index == 0:
            sum += (alpha + A[index]) * np.tan(0.5 * theta)
        else:
            sum += A[index] * np.sin(index * theta)
    # note that multiplication with 4 and multiplication with 1/4
    # result in one as prefactor
    return -sum * (np.cos(theta) - np.cos(theta_a))

# calculate integral 
theta_a = 0
alpha = 0 
array_coefficients = np.array([1, 2, 3, 4])
integral = quad(integrand, 0, 1, args=(theta_a , alpha, array_coefficients))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2021-08-26
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多