#include "mex.h" void rosenbrock(double x[], double cof, double *j1, double grad[]) { double t1, t2; t1 = x[1] - x[0]*x[0]; t2 = 1e0 - x[0]; *j1 = cof*t1*t1 + t2*t2; grad[0] = -4e0*cof*x[0]*t1 - 2e0*t2; grad[1] = 2e0*cof*t1; return; } /* The gateway function. */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int n, m; double *x, cof, *j1, *grad; /* Check for proper number of arguments */ if (nrhs !=2) mexErrMsgTxt("Two inputs required."); else if (nlhs != 2) mexErrMsgTxt("Only two outputs is allowed."); else if (!mxIsNumeric(prhs[0]) || !mxIsNumeric(prhs[1])) mexErrMsgTxt("One of the inputs not numeric."); /* Create a pointer to a copy of the first input, vector x. */ x = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input a. */ n = mxGetN(prhs[0]); m = mxGetM(prhs[0]); /* Create a scalar copy of the second input. */ cof = mxGetScalar(prhs[1]); /* Create the outputs j1 and grad. */ plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); j1 = mxGetPr(plhs[0]); plhs[1] = mxCreateDoubleMatrix(m,n,mxREAL); grad = mxGetPr(plhs[1]); /* Compute the outputs using C subroutine. */ rosenbrock(x,cof,j1,grad); }