Matlab 如何求函数的八度导数?

Matlab 如何求函数的八度导数?,matlab,for-loop,octave,derivative,Matlab,For Loop,Octave,Derivative,输入: diff Difference and approximate derivative. diff(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]. diff(X), for a matrix X, is the matrix of row differences, [X(2:n,:) - X(1:n-1,:)]. diff(X), for an N-D arra

输入:

diff Difference and approximate derivative.
     diff(X), for a vector X, is [X(2)-X(1)  X(3)-X(2) ... X(n)-X(n-1)].
     diff(X), for a matrix X, is the matrix of row differences,
           [X(2:n,:) - X(1:n-1,:)].
     diff(X), for an N-D array X, is the difference along the first
          non-singleton dimension of X.
     diff(X,N) is the N-th order difference along the first non-singleton 
          dimension (denote it by DIM). If N >= size(X,DIM), diff takes 
          successive differences along the next non-singleton dimension.
     diff(X,N,DIM) is the Nth difference function along dimension DIM. 
         If N >= size(X,DIM), diff returns an empty array.

Examples:
   h = .001; x = 0:h:pi;
   diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
   diff((1:10).^2) is 3:2:19

   If X = [3 7 5
           0 9 2]
   then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2
                                                  9 -7],
   diff(X,2,2) is the 2nd order difference along the dimension 2, and
   diff(X,3,2) is the empty matrix.
Xf
=和保存点的x值的数组

Yf
=保存点方法y值的数组=2点向前差分、2点向后差分、3点中心差分、5点中心差分

输出:

diff Difference and approximate derivative.
     diff(X), for a vector X, is [X(2)-X(1)  X(3)-X(2) ... X(n)-X(n-1)].
     diff(X), for a matrix X, is the matrix of row differences,
           [X(2:n,:) - X(1:n-1,:)].
     diff(X), for an N-D array X, is the difference along the first
          non-singleton dimension of X.
     diff(X,N) is the N-th order difference along the first non-singleton 
          dimension (denote it by DIM). If N >= size(X,DIM), diff takes 
          successive differences along the next non-singleton dimension.
     diff(X,N,DIM) is the Nth difference function along dimension DIM. 
         If N >= size(X,DIM), diff returns an empty array.

Examples:
   h = .001; x = 0:h:pi;
   diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
   diff((1:10).^2) is 3:2:19

   If X = [3 7 5
           0 9 2]
   then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2
                                                  9 -7],
   diff(X,2,2) is the 2nd order difference along the dimension 2, and
   diff(X,3,2) is the empty matrix.
X
=包含有效X值的数组,在该数组中可以实际使用所选方法(例如,您不能在
Xf
数组的上限使用前向差分方法,因为它后面没有值)

DF
=这些点的导数

我需要给脚本一组点,然后使用4种不同的方法计算这些点的导数,而不使用内置的导数函数,如
diff
。我希望在编写其中一个代码方面得到一些帮助,然后我想我应该能够找出如何完成其余的工作

我的尝试:

[a,minidx]=min(Xf);
[b,maxidx]=max(Xf);
n=10;
h=(b-a)/n;
f=(x.^3)。*e.^(-x)。*cos(x);
If method=“forward”#用户输入
X=[min(Xf),Xf(maxidx-1)];
对于k=min(Xf):n#不确定这是否是正确的迭代范围。。。
f(1)=f(x-2*h)+8*f(x+h);
f(2)=8*f(x-h)+f(x+2*h);
DF=(f1-f2)/(12*h);
外循环
恩迪夫

以下是一些关于Matlab如何计算导数的文档:

diff Difference and approximate derivative.
     diff(X), for a vector X, is [X(2)-X(1)  X(3)-X(2) ... X(n)-X(n-1)].
     diff(X), for a matrix X, is the matrix of row differences,
           [X(2:n,:) - X(1:n-1,:)].
     diff(X), for an N-D array X, is the difference along the first
          non-singleton dimension of X.
     diff(X,N) is the N-th order difference along the first non-singleton 
          dimension (denote it by DIM). If N >= size(X,DIM), diff takes 
          successive differences along the next non-singleton dimension.
     diff(X,N,DIM) is the Nth difference function along dimension DIM. 
         If N >= size(X,DIM), diff returns an empty array.

Examples:
   h = .001; x = 0:h:pi;
   diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
   diff((1:10).^2) is 3:2:19

   If X = [3 7 5
           0 9 2]
   then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2
                                                  9 -7],
   diff(X,2,2) is the 2nd order difference along the dimension 2, and
   diff(X,3,2) is the empty matrix.
下面是另一个例子:

xp= diff(xf);
yp= diff(yf);

% derivative:
dydx=yp./xp;
% also try:
dydx1=gradient(yf)./gradient(xf)

用于计算导数的演示倍频程函数: 如果这段代码吓到了你,那么这段视频将进入eli5的详细信息中,介绍正在发生的事情以及为什么每一件作品都会扮演一个角色:


切线到曲线上某点的斜率就是导数的定义。您可以使用倍频程(或任何计算机语言)来计算给定函数的倍频程。作为练习,画出这些曲线,你应该看到x平方的导数是x,sin(x)的导数是cos(x)。“当你看到它时,它很漂亮,因为这是大多数机器学习算法的核心。导数告诉您可以根据您所在的位置找到奖励的位置。

在找到导数后,我将如何评估该导数的特定值?