diff --git a/Mex/a612.cpp b/Mex/a612.cpp new file mode 100644 index 0000000..59a40db --- /dev/null +++ b/Mex/a612.cpp @@ -0,0 +1,51 @@ +#include "mex.h" +#include "matrix.h" +#include "stdlib.h" + + + +void mexFunction(int nlhs, mxArray *plhs[], // Output + int nrhs, const mxArray *prhs[]) // Input +{ + + int num_data_points = *mxGetPr(prhs[0]); + + double* x = (double *)mxCalloc(num_data_points, sizeof(double)); + double* y = (double *)mxCalloc(num_data_points, sizeof(double)); + double* temp = (double *)mxCalloc(num_data_points, sizeof(double)); + + for(int j=0; j < num_data_points; j++) + { + x[j] = mxGetPr(prhs[1])[j]; + y[j] = mxGetPr(prhs[2])[j]; + } + +//Mid-diff + int i = 0; + + temp[i] = (y[i+1] - y[i]) / (x[i+1] - x[i]); + i++; + temp[i] = (y[i+1] - y[i]) / (x[i+1] - x[i]); + temp[i] += (y[i+1] - y[i-1]) / (x[i+1] - x[i-1]); + temp[i] /= 2; + + for (; i < num_data_points - 2; ++i) + { + temp[i] = (y[i+1] - y[i-1] ) / (x[i+1] - x[i-1]); + } + + temp[i] += (y[i] - y[i-1]) / (x[i] - x[i-1]); + temp[i] /= 2; + i++; + temp[i] = (y[i] - y[i-1]) / (x[i] - x[i-1]); + +// Output + nlhs = num_data_points; + for(int j=0; j < num_data_points; j++){ + plhs[j]=mxCreateDoubleScalar(temp[j]); + } + + + + return; +} \ No newline at end of file diff --git a/Mex/a612.m b/Mex/a612.m new file mode 100644 index 0000000..ccafbd3 --- /dev/null +++ b/Mex/a612.m @@ -0,0 +1,14 @@ +% function accepts 1 double and 2 vectors of double +% a611(#num_of_points, vector_a, vector_b) +% number of datapoints in both vectors must be equal, +% or else the result will not be correct +% +%exp.: +% a=[0, 5, 10, 15, 20 ] +% b=[1, 0.8811, 0.7366, 0.5430, 0.1698] +% [a1,a2,a3,a4,a5]=a611(5,a,b) +% a1 = -0.023780 +% a2 = -0.026340 +% a3 = -0.033810 +% a4 = -0.019360 +% a5 = -0.074640 \ No newline at end of file