#include <stdio.h> #include <math.h> #include <chplot.h> #include <numeric.h> #define N 36 double func(double x) { return x*sin(x)*cos(x); } int main() { array double x[N], y[N], yprime[N]; double err, h; int i; class CPlot plot; linspace(x, 0, 2*M_PI); printf(" x derivative() value Cal. value\n"); // /* Specify the initial step h and return estimate error */ // yprime =derivatives(func,x,&err,0.8); yprime =derivatives(func,x); /* use default step value and do not return err */ fevalarray(y,func,x); for (i=0; i<2; i++) printf("%12.6f %12.6f %12.6f\n",x[i],yprime[i], (0.5*sin(2*x[i])+x[i]*cos(2*x[i]))); printf(" -------- --------- --------\n"); for (i=34; i<36; i++) printf("%12.6f %12.6f %12.6f\n",x[i],yprime[i], (0.5*sin(2*x[i])+x[i]*cos(2*x[i]))); plot.Label(PLOT_AXIS_X,"x"); plot.Label(PLOT_AXIS_Y,"f(x) & d(f(x))/dx"); plot.Data2D(x,y); plot.Data2D(x,yprime); plot.Legend("f(x)",0); plot.Legend("d(f(x))/dx",1); plot.Plotting(); }
x derivative() value Cal. value 0.000000 0.000000 0.000000 0.179520 0.343760 0.343760 -------- --------- -------- 6.103666 5.538777 5.538777 6.283185 6.283185 6.283185