#include <stdio.h> #include <chplot.h> #include <math.h> #define M 20 #define N 30 #define NUM_X 40 #define NUM_Y 50 int main() { int i,j; array double z_s1[NUM_X*NUM_Y], z_l1[NUM_X*NUM_Y]; array double x[NUM_X],y[NUM_Y],xa[M],ya[N]; array double za[M][N],za1d[M*N], z_s[NUM_X][NUM_Y], z_l[NUM_X][NUM_Y]; class CPlot plot; /* Construct data set of the peaks function */ linspace(xa, -3, 3); linspace(ya, -4, 4); for(i=0; i<M; i++) { for(j=0; j<N; j++) { za[i][j] = 3*(1-xa[i])*(1-xa[i])* exp(-(xa[i]*xa[i])-(ya[j]+1)*(ya[j]+1)) - 10*(xa[i]/5 - xa[i]*xa[i]*xa[i]- pow(ya[j],5))*exp(-xa[i]*xa[i]-ya[j]*ya[j]) - 1/3*exp(-(xa[i]+1)*(xa[i]+1)-ya[j]*ya[j]); } } za1d = (array double[M*N])za; linspace(x, -2.9, 2.9); linspace(y, -3.9, 3.9); /* test 2-dimensional cubic spline interpolation*/ interp2(z_s,x,y,xa,ya,za,"spline"); /* test 2-dimensional linear interpolation */ interp2(z_l,x,y,xa,ya,za,"linear"); /* add offset for display */ z_l1 = (array double[NUM_X*NUM_Y])z_l + (array double[NUM_X*NUM_Y])50; z_s1 = (array double[NUM_X*NUM_Y])z_s + (array double[NUM_X*NUM_Y])100; plot.SetDimension(3); plot.Label(PLOT_AXIS_X, "x"); plot.Label(PLOT_AXIS_Y, "y"); plot.Label(PLOT_AXIS_Z, "z"); plot.Data3D(xa, ya, za1d); plot.Data3D(x, y, z_s1); plot.Data3D(x, y, z_l1); plot.TicsLevel(0); plot.Text("Spline", PLOT_TEXT_RIGHT,3.5,3.5,120); plot.Text("Linear", PLOT_TEXT_RIGHT,3.5,3.5,70); plot.Text("Original", PLOT_TEXT_RIGHT,3.5,3.5,20); plot.PlotType(PLOT_PLOTTYPE_LINES,0,1,1); plot.PlotType(PLOT_PLOTTYPE_LINES,1,1,1); plot.PlotType(PLOT_PLOTTYPE_LINES,2,1,1); plot.Plotting(); }