因此,我正在为物理编写运动学问题。我有一个位移公式。该代码正确地给了我导数(速度)和二阶导数(加速度)。但是,当我尝试从任何导数中求解变量时,都会出现奇怪的错误。它将给我位移的价值!
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
答案 0 :(得分:1)
简短答案:
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)
答案 1 :(得分:0)
您应按以下方式定义函数:
def v(t,t0):
return diff(r(t), t, t0)
,然后使用v(t,2)