gpt4 book ai didi

python - Sympy值错误: First variable cannot be a number

转载 作者:行者123 更新时间:2023-12-01 07:08:48 25 4
gpt4 key购买 nike

所以我正在编写物理运动学问题。我有一个位移公式。代码正确地给出了导数(速度)和二阶导数(加速度)。但是当我尝试从任何导数中求解变量时,我会遇到奇怪的错误。它会给我位移的值!

import numpy as np
import sympy
import matplotlib.pyplot as plt

from sympy import *
x, y, z, t = symbols('x y z t')
i, j, k = symbols('i j k')
init_printing(use_unicode=True)

# **********************************************************************************

#Question 1 - The position of an electron is given by r = 3.0t(i) − 4.0t^2(j) + 2.0(k) (where t is in
#seconds and the coefficients have the proper units for r to be in meters).
#(a) What is v(t) for the electron?
#(b) In unit–vector notation, what is v at t = 2.0 s?
#(c) What are the magnitude and direction of v just then?

def r(t):
return (3*t)*i-(4*t*t)*j+2*k

def v(t):
return diff(r(t), t)

def a(t):
return diff(v(t), t)

print("Questions 1 -")
print("a)")
print("r(t) = ", r(t))
print("v(t) = ", v(t))
print("a(t) = ", a(t))
print("")
print('b)')
print("R(2) = ", r(2))
print("v(2) = ", v(2))

当我点击运行时,这是输出:

Questions 1 -
a)
r(t) = 3*i*t - 4*j*t**2 + 2*k
v(t) = 3*i - 8*j*t
a(t) = -8*j

b)
R(2) = 6*i - 16*j + 2*k
Traceback (most recent call last):
File "/tmp/sessions/b12e4bdebdf741f3/main.py", line 48, in <module>
print("v(2) = ", v(2))
File "/tmp/sessions/b12e4bdebdf741f3/main.py", line 35, in v
return diff(r(t), t)
File "/usr/local/lib/python3.6/dist-packages/sympy/core/function.py", line 1979, in diff
return Derivative(f, *symbols, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/sympy/core/function.py", line 1156, in __new__
raise ValueError("First variable cannot be a number: %i" % v)
ValueError: First variable cannot be a number: 2

最佳答案

简短回答:

v(2) 表示区分 r(2)2

2 不是一个符号,因此您会收到错误。 您的函数 v() 中需要一个额外的参数。

def v(t,at_point):
return diff(r(t), t, at_point)

v(t,2)
#-8⋅j
<小时/>

长答案:

问题在于,当您这样做时:

v(2)

您要求的是:

diff(r(2), 2)

最后一种方法是对 r(2) 相对于 2 进行微分。 2 不是一个符号,因此您会收到错误。 您需要一个额外的参数。

def v(t,at_point):
return diff(r(t), t, at_point)

关于python - Sympy值错误: First variable cannot be a number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58324988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com