簡體   English   中英

如何在 Octave 中找到函數的導數?

[英]How do I find the derivative of a function in Octave?

輸入:

Xf = 和保存點 x 值的數組

Yf = 保存點的 y 值的數組方法 = 2 點前向差、2 點后向差、3 點中心差、5 點中心差

輸出:

X = 包含有效 x 值的數組,其中選擇的方法可以實際使用(例如,您不能在Xf數組的上限使用前向差分方法,因為它之后沒有值)

DF = 這些點的導數

我需要為腳本提供一組點,然后使用 4 種不同的方法計算這些點的導數,而不使用像diff這樣的內置導數函數 我需要一些幫助來編寫其中一個代碼,然后我想我應該能夠弄清楚如何完成其​​余的工作。

2分前差

我的嘗試:

[a, minidx] = min(Xf);
[b, maxidx] = max(Xf);
n = 10;
h = (b-a)/n;
f = (x .^3) .* e.^(-x) .* cos(x);

If method = "forward" #Input by user

    X = [min(Xf), Xf(maxidx-1)];
    for k = min(Xf):n # not sure if this is the right iteration range...

        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);

    endfor
endif

https://wiki.octave.org/Symbolic_package

% this is just a formula to start with,  
% have fun and change it if you want to.  
f = @(x) x.^2 + 3*x - 1 + 5*x.*sin(x);  
% these next lines take the Anonymous function into a symbolic formula  
pkg load symbolic  
syms x;  
ff = f(x);  
% now calculate the derivative of the function  
ffd = diff(ff, x)  
% answer is ffd = (sym) 5*x*cos(x) + 2*x + 5*sin(x) + 3  
...  

下面是一些關於 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)

演示八度函數來計算導數:

#This octave column vector is the result y axis/results of your given function  
#to which you want a derivative of.  Replace this vector with the results of  
#your function. 
observations = [2;8;3;4;5;9;10;5] 
 
#dy (aka the change in y) is the vertical distance (amount of change) between  
#each point and its prior.  The minus sign serves our purposes to "show me the  
#vertical different per unit x."  The NaN in square brackets does a 1 position 
#shift, since I want my tangent lines to be delimited by 1 step each. 
dy = observations - [NaN; observations(1:end-1,:)] 
 
#dx (aka the change in x) is the amount of horizontal distance (amount of  
#change) between each point and its prior.  for simplicity we make these all 1,  
#however your data points might not be constant width apart, that's okay  
#to populate the x vector here manually using the parameter-inputs to the  
#function that you want the derivative of. 
dx = ones(length(observations), 1) 
 
# -- alternative: if your x vector is subjective, then do something like: -- 
#function_parameters = [1;3;5;9;13;90;100;505] 
#dx = function_parameters 
 
#The derivative of your original function up top is "the slope of the  
#tangent line to the point on your curve.  The slope is calculated with the  
#equation: (rise / run) such that 'Rise' is y, and 'run' is x.  The tangent  
#line is calculated above with the dy variable.  'The point' is each point  
#in the observations, and 'the curve' is simply your function up top that  
#yielded those y results and x input parameters. 
dy_dx_dv1_macd = dy ./ dx 

如果這段代碼讓你感到害怕,那么這個視頻會詳細介紹正在發生的事情以及為什么每件作品都起作用: https ://youtu.be/gtejJ3RCddE?t=5393

曲線上一點的切線的斜率就是導數的定義。 您可以使用八度(或任何計算機語言)為給定的函數計算它。 作為練習,繪制這些圖,您應該看到 x 平方的導數是 x,而 sin(x) 的導數是 cos(x)。 當您看到它時,它很漂亮,因為這是大多數機器學習算法的核心。 導數會根據您所在的位置告訴您在哪里可以找到獎勵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM