← 返回题库
初级

计算24个向量的和并绘制

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    import matplotlib.pyplot as plt
    import numpy as np
    from mpl_toolkits.mplot3d import Axes3D
    from math import sin, cos, pi
    
    vs = [(sin(pi*t/6), cos(pi*t/6), 1.0/3) for t in range(0, 24)]
    
    v_sum = (sum(v[0] for v in vs), sum(v[1] for v in vs), sum(v[2] for v in vs))
    
    fig = plt.figure(figsize=(10, 8))
    ax = fig.add_subplot(111, projection='3d')
    
    current = (0, 0, 0)
    for v in vs:
        ax.quiver(current[0], current[1], current[2], v[0], v[1], v[2], 
                  arrow_length_ratio=0.1, color='blue', alpha=0.5)
        current = (current[0]+v[0], current[1]+v[1], current[2]+v[2])
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.title('24个向量首尾相连')
    plt.tight_layout()
    plt.show()
    
    print(f"向量和 = {v_sum}")

示例

输入
solve()
期望输出
输出结果
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码