出现错误Error Raised: unsupported operand type(s) for *: ‘function‘ and ‘float‘,该怎么修正?

在尝试计算双重积分时遇到一个错误:'function' 和 'float' 类型无法相乘。错误出现在 `x_weight = hx*weights` 和 `y_weight = hy*weights` 行。原因是 `hx` 和 `hy` 是浮点数,而 `weights` 应该是一个数组,但在赋值为 `3/4` 和 `9/8` 时,它被错误地当作标量处理了。修正此问题的方法是确保 `weights` 数组在更新时保持为数组类型,而不是标量。
摘要由CSDN通过智能技术生成

import numpy as np

def double_int_approx(c,d,x1,x2,f,n):

    xvals = np.linspace(x1,x2,n+1)
    #Evaluation points in x direction
    yvals = np.linspace(c,d,n+1)
    #Evaluation points in y direction

    hx = (x2-x1)/n 
    #Mesh width in x direction
    hy = (d-c)/n 
    #Mesh width in y direction

# ================================================================
    
    ### SIMPSON'S RULE WEIGHTS ###

    weights = np.ones((n+1))
    
    weights[0] = 3/8
    weights[n] = 3/8
    
    for k in range(1,n):
        if k % 3 == 0:
            w

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值