在所有采样点(包括边界点)上的非符号导数 [英] Non-symbolic derivative at all sample points including boundary points

查看:118
本文介绍了在所有采样点(包括边界点)上的非符号导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个向量t = [0 0.1 0.9 1 1.4]和一个向量x = [1 3 5 2 3].如何计算与原始向量相同长度的x相对于时间的导数?

Suppose I have a vector t = [0 0.1 0.9 1 1.4], and a vector x = [1 3 5 2 3]. How can I compute the derivative of x with respect to time that has the same length as the original vectors?

我不应该使用任何符号操作.命令diff(x)./diff(t)不会产生相同长度的向量.我应该先对x(t)函数进行插值,然后取其导数吗?

I should not use any symbolic operations. The command diff(x)./diff(t) does not produce a vector of the same length. Should I first interpolate the x(t) function and then take its derivative?

推荐答案

存在多种方法来计算与原始数据相同的点的导数:

Different approaches exist to calculate the derivative at the same points as your initial data:

  1. 有限差异:在您的内部点使用中心差异方案,并在第一个/最后一个点使用前进/后退方案
  1. Finite differences: Use a central difference scheme at your inner points and a forward/backward scheme at your first/last point

  1. 曲线拟合:通过点拟合曲线,计算该拟合函数的导数,并在与原始数据相同的点处对其进行采样.典型的拟合函数是多项式或样条函数.
  1. Curve fitting: Fit a curve through your points, calculate the derivative of this fitted function and sample them at the same points as the original data. Typical fitting functions are polynomials or spline functions.

请注意,曲线拟合方法可提供更好的结果,但需要更多的调整选项,并且速度较慢(〜100x).

演示

作为一个例子,我将计算一个正弦函数的导数:

As an example, I will calculate the derivative of a sine function:

t = 0:0.1:1;
y = sin(t);

其精确导数是众所周知的:

Its exact derivative is well known:

dy_dt_exact = cos(t);

导数可以近似地计算为:

The derivative can approximately been calculated as:

  1. 有限区别:

  1. Finite differences:

dy_dt_approx = zeros(size(y));
dy_dt_approx(1) = (y(2) - y(1))/(t(2) - t(1)); % forward difference
dy_dt_approx(end) = (y(end) - y(end-1))/(t(end) - t(end-1)); % backward difference
dy_dt_approx(2:end-1) = (y(3:end) - y(1:end-2))./(t(3:end) - t(1:end-2)); % central difference

  1. 多项式拟合:

  1. Polynomial fitting:

p = polyfit(t,y,5); % fit fifth order polynomial
dp = polyder(p); % calculate derivative of polynomial

结果可以如下显示:

figure('Name', 'Derivative')
hold on
plot(t, dy_dt_exact, 'DisplayName', 'eyact');
plot(t, dy_dt_approx, 'DisplayName', 'finite difference');
plot(t, polyval(dp, t), 'DisplayName', 'polynomial');
legend show

figure('Name', 'Error')
hold on
plot(t, abs(dy_dt_approx - dy_dt_exact)/max(dy_dt_exact), 'DisplayName', 'finite difference');
plot(t, abs(polyval(dp, t) - dy_dt_exact)/max(dy_dt_exact), 'DisplayName', 'polynomial');
legend show

第一张图显示了导数本身,第二张图显示了两种方法的相对误差.

The first graph shows the derivatives itself and the second graph plots the relative errors made by both methods.

讨论

一个人清楚地看到,曲线拟合法比有限差分法能提供更好的结果,但速度要慢100倍.曲线拟合方法的相对误差为10^-5.请注意,当对数据进行更密集的采样或使用更高阶的方案时,有限差分方法会变得更好.曲线拟合方法的缺点是必须选择良好的多项式阶数.样条函数通常可能更适合.

One clearly sees that the curve fitting method gives better results than the finite differences, but it is ~100x slower. The curve fitting methods has a relative error of order 10^-5. Note that the finite differences approach becomes better when your data is sampled more densely or you use a higher order scheme. The disadvantage of the curve fitting approach is that one has to choose a good polynomial order. Spline functions may be better suited in general.

采样速度提高10倍的数据集,即t = 0:0.01:1;,结果如下图所示:

A 10x faster sampled dataset, i.e. t = 0:0.01:1;, results in the following graphs:

这篇关于在所有采样点(包括边界点)上的非符号导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆