求导只需将 f 中的函数替换即可。
/*本程序使用理查森外推方法就求微分,程序的核心是运用G[i][j] =
(pow(4, i) * G[i][j-1] - G[i-1][j-1])/(pow(4, i)-1)*/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double le = 1e-10;
template <typename T> T** Allocation2D(int m, int n){
T **a;
a = new T*[m];
for(int i=0; i<m; i++)
a[i] = new T[n];
return a;
}
double f(double x);
void WaiTui(double t, double h);
void Clear(double **G, int m);
int main(){
double t, h;
cout<<"请输入所求值的导数:";
cin>>t;
cout<<"请输入步长的值:";
cin>>h;
WaiTui(t, h);
return 0;
}
/********************************函数f = sin(x)/x *******************************/
double f(double x){
double result;
result = sin(x)/x;
return result;
}
void WaiTui(double t, double h){
int i, k = 0;
double **G;
G = Allocation2D<double>(10, 10);
G[0][0] = (f(t + h) - f(t - h))