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

混合numpy浮点数与Python常规浮点数引发TypeError错误求助

问题描述

我正在尝试编写一个Python函数来预测重力作用下抛射体的运动,但运行时出现错误。查阅的多数文章都无法帮助我解决该问题。注:我是Python新手,若有批评还请手下留情。

错误信息

TypeError: 无法将序列与numpy.float64类型的非整数相乘

原始代码

import numpy as np
import matplotlib.pyplot as plt
def projectile_motion(initial_velocity, angle, time_step, total_time):
    m = 1.0
    g = 9.8
    k = 0.42
    x=[0]
    y=[0]
    angle_rad = angle/180 * np.pi
    new_velocity = 0
    
    #Calculate initial vx and vy
    vx = [initial_velocity * np.cos(angle_rad)]
    vy = [initial_velocity * np.sin(angle_rad)]      
    
    #Initial Drag Force due to air friction
    drag_force = k * initial_velocity**2
    
    #Calculate acceleration components
    ax =[-(drag_force * np.cos(angle_rad))  / m]
    ay = [-g - (drag_force * np.sin(angle_rad)) / m]    
    
    
    list_size = int(total_time/time_step)      
   
    for i in range(list_size):
       vx.append(vx[i] + time_step * ax[i])
       vy.append(vy[i] + time_step * ay[i])
       x.append(x[i] + time_step * vx[i])
       y.append(y[i] + time_step * vy[i])
      
       #Calculate magnitude of new velocity 
       new_velocity = np.sqrt(vx[i+1]**2 + vy[i+1]**2)
       
       #Calculate magnitude of new drag_force
       drag_force = k * new_velocity**2
       
       ax.append([-(drag_force * np.cos(angle_rad))  / m])
       ay.append([-g - (drag_force * np.sin(angle_rad)) / m])        
       print(x)
       print(y)
    
    plt.plot(x,y)
    plt.show
    return

projectile_motion(10, 45.0, 0.01, 2)

问题解决

核心错误分析

报错的直接原因是循环中更新ax和ay时,你用方括号把单个数值包成了列表(比如ax.append([xxx])),导致ax列表里混合了普通数值和嵌套小列表。第二次循环执行time_step * ax[i]时,ax[i]变成了列表,触发了类型不匹配的错误。

另外还有两个物理逻辑和语法小问题:

  1. 空气阻力方向应与当前速度方向相反,而非一直沿用初始角度的余弦/正弦值,这会导致阻力方向完全错误。
  2. plt.show缺少括号,必须写成plt.show()才能弹出图像窗口。

修正后的代码

import numpy as np
import matplotlib.pyplot as plt
def projectile_motion(initial_velocity, angle, time_step, total_time):
    m = 1.0
    g = 9.8
    k = 0.42
    x = [0]
    y = [0]
    angle_rad = angle / 180 * np.pi
    
    # 初始化速度分量
    vx = [initial_velocity * np.cos(angle_rad)]
    vy = [initial_velocity * np.sin(angle_rad)]      
    
    # 初始化速度大小和阻力
    current_velocity = initial_velocity
    drag_force = k * current_velocity ** 2
    
    # 初始化加速度分量(阻力方向与初始速度相反)
    ax = [-(drag_force * np.cos(angle_rad)) / m]
    ay = [-g - (drag_force * np.sin(angle_rad)) / m]    
    
    list_size = int(total_time / time_step)      
   
    for i in range(list_size):
        # 更新速度和位置
        vx.append(vx[i] + time_step * ax[i])
        vy.append(vy[i] + time_step * ay[i])
        x.append(x[i] + time_step * vx[i])
        y.append(y[i] + time_step * vy[i])
        
        # 计算当前速度大小和方向
        current_velocity = np.sqrt(vx[i+1] ** 2 + vy[i+1] ** 2)
        # 避免速度为0时出现除以0的情况
        if current_velocity == 0:
            cos_theta, sin_theta = 0, 0
        else:
            cos_theta = vx[i+1] / current_velocity
            sin_theta = vy[i+1] / current_velocity
        
        # 计算当前阻力
        drag_force = k * current_velocity ** 2
        
        # 更新加速度(直接存数值,不要用方括号包裹)
        ax.append(-(drag_force * cos_theta) / m)
        ay.append(-g - (drag_force * sin_theta) / m)        
    
    plt.plot(x, y)
    plt.title("抛射体运动轨迹")
    plt.xlabel("水平距离 (m)")
    plt.ylabel("竖直高度 (m)")
    plt.show()
    return

projectile_motion(10, 45.0, 0.01, 2)

修正细节说明

  1. 移除ax.append([...])和ay.append([...])中的方括号,确保ax、ay列表元素均为单一数值,解决类型错误。
  2. 修正阻力方向:通过当前速度分量计算方向余弦/正弦,让阻力始终与运动方向相反,符合真实物理规律。
  3. 补上plt.show()的括号,确保图像正常显示。
  4. 增加速度为0的判断,避免除以0的异常。
  5. 添加图像标题和坐标轴标签,结果更直观。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 14:25:23