简体   繁体   English

MATLAB中微分实现的差异

[英]Differences in Differentiation Implementations in MATLAB

I'm trying to find the (numerical) curvature at specific points. 我正在尝试在特定点处找到(数值)曲率。 I have data stored in an array, and I essentially want to find the local curvature at every separate point. 我将数据存储在数组中,并且我本质上是想在每个单独的点上找到局部曲率。 I've searched around, and found three different implementations for this in MATLAB: diff , gradient , and del2 . 我四处搜寻,并在MATLAB中找到了三种不同的实现: diffgradientdel2

If my array's name is arr I have tried the following implementations: 如果我的数组名称是arr我尝试了以下实现:

curvature = diff(diff(arr));
curvature = diff(arr,2); 
curvature = gradient(gradient(arr)); 
curvature = del2(arr); 

The first two seem to output the same values. 前两个似乎输出相同的值。 This makes sense, because they're essentially the same implementation. 这是有道理的,因为它们本质上是相同的实现。 However, the gradient and del2 implementations give different values from each other and from diff . 但是, gradientdel2实现彼此之间以及从diff给出不同的值。

I can't figure out from the documentation precisely how the implementations work. 我无法从文档中确切地了解实现的工作方式。 My guess is that some of them are some type of two-sided derivative, and some of them are not two-sided derivatives. 我的猜测是,其中有些是某种类型的双向导数,而有些则不是双向导数。 Another thing that confuses me is that my current implementations use only the data from arr . 令我困惑的另一件事是,我当前的实现仅使用来自arr的数据。 arr is my y-axis data, the x-axis essentially being time. arr是我的y轴数据,x轴本质上是时间。 Do these functions default to a stepsize of 1 or something like that? 这些函数是否默认为1或类似的步长?

If it helps, I want an implementation that takes the curvature at the current point using only previous array elements. 如果有帮助,我想要一个仅使用以前的数组元素来获取当前点曲率的实现。 For context, my data is such that a curvature calculation based on data in the future of the current point wouldn't be useful for my purposes. 就上下文而言,我的数据使得基于当前点的未来数据进行曲率计算对我的目的没有用。

tl;dr I need a rigorous curvature at a point implementation that uses only data to the left of the point. tl; dr我在点实现中需要严格的曲率,该实现仅使用该点左侧的数据。

Edit: I kind of better understand what's going on based on this, thanks to the answers below. 编辑:由于以下答案,我更好地了解了基于此的情况。 This is what I'm referring to: 这就是我指的是:

gradient calculates the central difference for interior data points. gradient计算内部数据点的中心差。 For example, consider a matrix with unit-spaced data, A, that has horizontal gradient G = gradient(A). 例如,考虑具有单位间隔数据A的矩阵,该矩阵具有水平梯度G =梯度(A)。 The interior gradient values, G(:,j), are 内部梯度值G(:,j)为

G(:,j) = 0.5*(A(:,j+1) - A(:,j-1)); The subscript j varies between 2 and N-1, with N = size(A,2). 下标j在2到N-1之间变化,其中N = size(A,2)。

Even so, I still want to know how to do a "lefthand" computation. 即使这样,我仍然想知道如何进行“左手”计算。

diff is simply the difference between two adjacent elements in arr, which is exactly why you lose 1 element for using diff once. diff只是arr中两个相邻元素之间的差异,这正是为什么使用diff一次丢失1个元素的原因。 For example, 10 elements in an array only have 9 differences. 例如,数组中的10个元素仅具有9个差异。

gradient and del2 are for derivatives. 梯度和del2用于导数。 Of course, you can use diff to approximate derivative by dividing the difference by the steps. 当然,您可以使用diff通过将差除以步长来近似导数。 Usually the step is equally-spaced, but it does not have to be. 通常,步骤之间的距离是相等的,但不必一定要这样。 This answers your question why x is not used in the calculation. 这回答了您的问题,为什么在计算中不使用x。 I mean, it's okay that your x is not uniform-spaced. 我的意思是,您的x不是等距的也可以。

So, why gradient gives us an array with the same length of the original array? 那么,为什么渐变会给我们一个与原始数组长度相同的数组? It is clearly explained in the manual how the boundary is handled, 手册中明确说明了边界的处理方式,

The gradient values along the edges of the matrix are calculated with single->sided differences, so that 沿矩阵边缘的梯度值是用单边差计算的,因此

G(:,1) = A(:,2) - A(:,1); G(:,1)= A(:,2)-A(:,1);

G(:,N) = A(:,N) - A(:,N-1); G(:,N)= A(:,N)-A(:,N-1);

Double-gradient and del2 are not necessarily the same, although they are highly correlated. 尽管双梯度和del2高度相关,但不一定相同。 It's all because how you calculate/approximate the 2nd-oder derivatives. 这是因为您如何计算/近似二阶导数。 The difference is, the former approximates the 2nd derivative by doing 1st derivative twice and the latter directly approximates the 2nd derivative. 不同之处在于,前者通过两次一阶导数来近似二阶导数,而后者直接近似于二阶导数。 Please refer to the help manual, the formula are documented. 请参考帮助手册,该公式已记录在案。

Okay, do you really want curvature or the 2nd derivative for each point on arr? 好吧,您是否真的想要曲率或arr上每个点的二阶导数? They are very different. 他们有很大的不同。 https://en.wikipedia.org/wiki/Curvature#Precise_definition https://en.wikipedia.org/wiki/Curvature#Precise_definition

You can use diff to get the 2nd derivative from the left. 您可以使用diff从左侧获取二阶导数。 Since diff takes the difference from right to left, eg x(2)-x(1), you can first flip x from left to right, then use diff. 由于diff从右到左求差,例如x(2)-x(1),因此您可以先从左向右翻转x,然后使用diff。 Some codes like, 一些代码,例如

x=fliplr(x)
first=x./h
second=diff(first)./h

where h is the space between x. 其中h是x之间的间隔。 Notice I use ./, which idicates that h can be an array (ie non-uniform spaced). 注意,我使用./,它表示h可以是一个数组(即非均匀间隔)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM