在MATLAB中使用移位矩阵查找数值导数 [英] Finding Numerical Derivative using a shift matrix in MATLAB

查看:103
本文介绍了在MATLAB中使用移位矩阵查找数值导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们被分配以使用给定的步骤来找到数字一阶和二阶导数:

We were given an assignment to find the numerical first and second derivative using given steps:

1)定义两个数组,x和y = f(x)(使用任何函数)

1) Define two arrays, x and y=f (x) (Use any function)

2)将微分算子定义为矩阵(例如对于f',定义矩阵D =(1/2h)(U-L),其中U是上移矩阵,L是下移矩阵.

2) Define the differential operator as a matrix (e.g. for f' , define the matrix D= (1/2h)(U-L) where U is an Uppershift Matrix, and L is Lowershift Matrix.

3)通过将微分算子矩阵乘以函数数组来计算导数.

3) Calculate the derivative by multiplying the differential operator matrix by the function array.

我已编写此代码:

%QUESTION 3- DIFFERENTIAL OPERATOR
h=2;
x = 2:h:8
y = x.^2 %the chosen function


n=length(x);
shift = 1; 
U = diag(ones(n-abs(shift),1),shift);
s = -1;
L= diag(ones(n-abs(shift),1),s);
% % the code above creates the upper and lower shift matrix
%
D= ((U-L))/(2*h) %differential operator 
d = y*D %approximates the first derivative of each element in vector -x
d2 = d*D %approximates the second derivative of each element in vector -x

我用于创建移位矩阵这篇文章

现在我将其作为解决方案:

Now I got this as a solution:

>> mat2q3

x =

     2     4     6     8


y =

     4    16    36    64


D =

         0    0.2500         0         0
   -0.2500         0    0.2500         0
         0   -0.2500         0    0.2500
         0         0   -0.2500         0


d =

    -4    -8   -12     9


d2 =

    2.0000    2.0000   -4.2500   -3.0000

请告诉我哪里出了问题,或者如何改善它.

Please tell me where I went wrong, or how can I improve it.

推荐答案

该代码大部分都完成了预期的工作:计算对导数的数值近似.但是,您计算导数的方式略有不正确.基本上,对于数组中的每个点,您都希望将左边的点减去右边的点,然后除以2*h.如果要执行此操作,则需要发布-将向量y乘以并将其转换为列向量.

This code for the most part does what it intends to do: compute the numerical approximation to the derivative. However, the way you're computing the derivative is slightly incorrect. Basically, for each point in your array, you want to subtract the point to the left with the point to the right and divide by 2*h. If you want to do that, you need to post-multiply the vector y and transform it into a column vector.

转置y,然后取D并乘以y:

>> d = D*y.'

d =

     4
     8
    12
    -9

但是,我想指出,由于您的D矩阵,第一个条目和最后一个条目没有任何意义:

However, I'd like to point out that the first and last entry don't make any sense because of your D matrix:

D =

         0    0.2500         0         0
   -0.2500         0    0.2500         0
         0   -0.2500         0    0.2500
         0         0   -0.2500         0

您要在此处计算的是数值导数的中心差,该差要求数组中要评估的点的左边和右边.基本上,对于第一个点,您只有右边的点,在您越界时,左边的点是未定义的.同样,对于最后一点,您只在左侧有一个点,而在右侧的点也超出了范围,因为它不存在.

What you are computing here is the central difference for the numerical derivative, which requires a point to the left and a point to the right of the point you are evaluating in your array. Basically, for the first point, you only have the point to the right where the left point is undefined as you are going out of bounds. Similarly for the last point, you only have a point to the left and the point to the right is also out of bounds as it doesn't exist.

此处的数值导数矩阵假设超出范围的点为0.结果证明,此时得到的导数正确,为fl幸点(即x = 1处的2*x为2).原因是因为x = 0上的点的确给出了x.^2的输出等于0,所以在数组中省略该点的行为就好像它是在您的点数组中开始的.

The numerical derivative matrix here assumes that the points going out of bounds are 0. It just turns out that you get the derivative correct at this point which is a fluke (i.e. 2*x at x = 1 is 2). The reason why is because the point at x = 0 does give the output of x.^2 being equal to 0, so the omission of this point in the array acts as if it was in your point array to begin with.

二阶导数需要相同的东西:

The same thing is required for the second derivative:

>> d2 = D*d

d2 =

    2.0000
    2.0000
   -4.2500
   -3.0000

但是,请记住,对于您先前的一阶导数结果d,最后一个条目是垃圾,因此,如果计算此结果的导数,则当您查看第三阶时,您将使用最不正确的最右项d的条目,因此d2的最后两个条目以及前两个条目都是垃圾.碰巧d2的第二个条目是正确的.这归因于x = 0在先前的结果中实际上并不存在,但是假设点数组中超出范围的值为0,我们确实得到了正确的结果.

However, bear in mind that for your previous first derivative result d, the last entry is garbage and so if you computed the derivative of this result, you would be using the incorrect right most term when you look at the third entry of d, and so the last two and also the first two entries of d2 are garbage. It just so happens that the second entry of d2 is correct. This is attributed to x = 0 not actually existing in the previous result, but with the assumption that values out of bounds in your point array are 0, we do get the right result.

您应该尝试更长的时间.例如,尝试执行以下操作:

You should try this on a longer sequence. For example, try doing it on this:

h = 2;
x = 2:h:20;
y = x.^2;

这定义了一个序列,该序列从2到20,以h = 2为步长.这是我们从xd获得的:

This defines a sequence going from 2 to 20 in steps of h = 2. This is what we get for x, and d:

x =

    2     4     6     8    10    12    14    16    18    20

d =

     4
     8
    12
    16
    20
    24
    28
    32
    36
   -81

如您所见,第一个和最后一个条目没有意义,因为我们没有考虑第一个条目的最左点,也没有考虑第二个条目的最右点.但是,如前所述,福禄克的第一点为我们提供了正确的结果.通常,第一个条目和最后一个条目不应为您提供正确的结果,因为我们没有左或右点来说明导数.对于其余的要点,您可以看到我们正在正确计算导数或2*x.

As you can see the first and last entry are meaningless as we don't have a left most point for the first entry and a right most point for the second entry to consider. However, the first point by fluke gives us the correct result as we talked about earlier. In general, the first and last entry should not give you the correct results as we don't have a left or right point to account for the derivative. For the rest of the points, you can see that we are correctly computing the derivative, or 2*x.

让我们看一下第二个导数d2:

Let's take a look at the second derivative, d2:

d2 =

    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
  -28.2500
   -9.0000

碰巧前两个元素是正确的,尽管从理论上讲它们是不正确的,但我们是通过fl幸获得了这一点.但是,最后两个元素不正确.

It so happens that the first two elements are correct, even though they theoretically shouldn't, but we got it by fluke. However, the last two elements aren't correct.

您需要记住的是一件事.请记住,在取具有中心差的数值导数时,必须修剪掉第一个n元素和最后一个n元素,其中n是要考虑的导数的顺序.

What you need to take away from this is to remember one thing. Remember that when taking numerical derivatives with the central difference, you have to trim off the first n elements and the last n elements, where n is the order of the derivative you are considering.

这篇关于在MATLAB中使用移位矩阵查找数值导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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