首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Octave中找到函数的导数?

如何在Octave中找到函数的导数?
EN

Stack Overflow用户
提问于 2016-03-27 11:39:50
回答 3查看 24.9K关注 0票数 7

输入:

Xf=和保存点的x值的数组

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

输出:

X=包含可实际使用所选方法的有效x值的数组(例如,不能在Xf数组,因为它后面没有值)

DF=这些点处的导数

我需要给一个脚本一组点,然后计算这些点的导数使用4种不同的方法而不使用内置的派生函数,比如diff..。我需要一些帮助来编写其中的一个代码,然后我想我应该能够弄清楚如何完成其余的工作。

2-point forward difference

我的尝试是:

代码语言:javascript
复制
[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
EN

回答 3

Stack Overflow用户

发布于 2017-11-26 00:15:36

https://wiki.octave.org/Symbolic_package

代码语言:javascript
复制
% 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  
...
票数 9
EN

Stack Overflow用户

发布于 2016-03-27 22:16:02

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

代码语言:javascript
复制
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.

下面是另一个示例:

代码语言:javascript
复制
xp= diff(xf);
yp= diff(yf);

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

Stack Overflow用户

发布于 2021-02-27 12:22:50

用于计算导数的演示倍频程函数:

代码语言:javascript
复制
#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 
 
#If this code scares you then this video goes into the eli5 detail about  
#what is going on and why every piece plays a role .  "The slope of a  
#tangent line to a point on your curve" is what a derivative is defined  
#to be.  And you can use octave (or any comptuer language) to calculate  
#it for your given function, and if you do this right and plot them,  
#double checking your work, you will find what your highschool teacher  
#was teaching by wrote: 'The derivative of x squared is x, and the  
#derivative of sin(x) is cos(x).'  It's beautiful masterpiece when you see  
#it fully because this is how physics around you isolated and captured  
#the information-gain to yield the nano machines you're using to read this sentence. 
# https://youtu.be/gtejJ3RCddE?t=5393
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36243590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档