输入:
Xf
= 和包含点 x 值的数组
Yf
= 一个保存 points 方法的 y 值的数组 = 2 点前向差分,2 点后向差分,3 点中心差分,5 点中心差分
输出:
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" #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/