You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行scipy simps积分代码报TypeError: must be a real number, not a tuple如何修复

问题修复方案

报错根源

  • 你的代码中x = (0, (m.pi/2) + dx, dx)定义的是包含3个元素的元组,不是0到π/2区间的采样点序列。math库的sqrt、sin方法仅支持传入单个实数,直接传入元组会触发类型错误。
  • scipy.integrate.simps要求传入的y、x参数均为等长的一维数值序列,你的写法完全不符合参数调用规范。

修复后代码

from scipy.integrate import simps
import numpy as np
import math as m

# 设定子区间数量为100
sub_interval_num = 100
# 生成0到π/2的等间隔采样点,共sub_interval_num + 1个点,对应100个子区间
x = np.linspace(0, m.pi/2, sub_interval_num + 1)
t1 = np.sqrt(x)
t2 = np.sin(x)
y = t1 * t2
calc = simps(y, x)
print("The approximation with Simpson's Rule for 100 subintervals is", calc)

核心修改说明

  • 引入numpy库,使用其向量化运算能力支持对整个采样点序列同时做开方、求正弦运算
  • 替换原错误的x定义方式,用np.linspace生成符合要求的等间隔采样点一维数组
  • 所有运算均基于数组完成,适配simps方法的参数要求

内容的提问来源于stack exchange,提问作者Qordn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 12:39:04