我有一个向量 x: Nx1 相对于另一个与 x 大小相同的向量 t(时间)的数值导数的问题。 我执行以下操作(例如选择 x 为正弦函数):
t=t0:ts:tf;
x=sin(t);
xd=diff(x)/ts;
但答案 xd 是 (N-1)x1,我发现它不计算对应于 x 的第一个元素的导数。
还有其他方法可以计算这个导数吗?
最佳答案
您正在寻找数值 gradient
我假设。
t0 = 0;
ts = pi/10;
tf = 2*pi;
t = t0:ts:tf;
x = sin(t);
dx = gradient(x)/ts
此函数的用途不同(向量场),但它提供了 diff
没有的功能:等长的输入和输出向量。
gradient
calculates the central difference between data points. For an array, matrix, or vector with N values in each row, the ith value is defined by
The gradient at the end points, where i=1 and i=N, is calculated with a single-sided difference between the endpoint value and the next adjacent value within the row. If two or more outputs are specified, gradient also calculates central differences along other dimensions. Unlike the diff function, gradient returns an array with the same number of elements as the input.
关于matlab - 向量的数值导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245365/