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

标签 matlab for-loop octave derivative

输入:

Xf = 和包含点 x 值的数组

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

输出:

X = 包含实际可以使用所选方法的有效 x 值的数组(例如,您不能在 Xf 的上限使用前向差分法 数组因为后面没有值)

DF = 这些点的导数

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

2-point forward difference

我的尝试:

[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 - 如何在 Octave 中找到函数的导数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36243590/

相关文章:

ios - 如何有效地填充我的 imageViews 而不会因错误 : Array index out of range 而崩溃

linux - 让 GNU Octave 与多核处理器一起工作。 (多线程)

matlab - 如何使用 Octave 对信号进行下采样?

performance - 如何优化以下 Octave 双循环?

MATLAB fwrite with skip slow

java - 在 MATLAB 中从 Java 加载库时出现 UnsatisfiedLinkError

matlab - 在 MATLAB 中为不同矩阵中的每一行应用相交

javascript - 在 for 循环中每两个 block 打印数字

arrays - 用先前的非零值替换向量中的所有零

java - Setter 和 Getter 到不同的类