我正在实现一个 RKF4(5) 积分器,我无法确定我的代码是否正常工作并且我不明白本地截断错误,或者我的代码是否无法正常工作。
对于代码块的大小,我深表歉意,但在这种情况下,最小可重现示例相当大。
import numpy as np
def RKF45(state, derivative_function, h):
"""
Calculate the next state with the 4th-order calculation, along with a 5th-order error
check.
Inputs:
state: the current value of the function, float
derivative_function: A function which takes a state (given as a float)
and returns the derivative of a function at that point
h: step size, float
"""
k1 = h * derivative_function(state)
k2 = h * derivative_function(state + (k1 / 4))
k3 = h * …Run Code Online (Sandbox Code Playgroud) python physics numerical-methods numerical-integration runge-kutta
我一直在使用 len 和 tuples 遇到这个问题。我想找出元组中有多少元素。到目前为止,它适用于至少有两个元素的元组。
例如:
> data = “cat”, “dog”
> len(data)
2
Run Code Online (Sandbox Code Playgroud)
这将返回 2。但是如果我只有一个元素
> data = “cat”
> len(data)
3
Run Code Online (Sandbox Code Playgroud)
这将返回 3,因为它计算字母。我希望它返回 1,因为数据中有一个字符串。
关于我如何做到这一点的任何想法?谢谢。