Artifact 61cef996f87054f600ba9edcc5f6ed7a5978afb7a953e74d0892ffb59ba4c961:
- File
mttroot/mtt/lib/rep/sfun_rep/ae.c.tmpl
— part of check-in
[062029b187]
at
2002-05-19 13:01:22
on branch origin/master
— Numerical solution of algebraic equations implemented for S-function target.
Equation solving requires the Matlab Optimization Toolbox to be installed.
Code has been changed from C++ to C to allow mex files to be built with LCC,
the compiler bundled with Matlab.Parameters are now obtained from numpar.c instead of a dialogue box.
`mtt <sys> sfun zip` creates all necessary files for building the model mex files. (user: geraint@users.sourceforge.net, size: 1942) [annotate] [blame] [check-ins using] [more...]
/* -*-c-*- Put emacs into c-mode */ #include <stdio.h> #include <stdlib.h> #include <mex.h> #include "<mtt_model_name>_def.h" /* utility procedures */ double * array_of_double (size_t n) { void *p = calloc (n, sizeof (double)); if (! p) { fprintf (stderr, "*** Error: failed to allocate memory\n"); } return (double *) p; } /* system equations */ static double * <mtt_model_name>_ae (double *mttyz, const double *mttx, const double *mttu, const double mttt, const double *mttpar) { #include "<mtt_model_name>_ae.c" } /* generic mex function */ #ifdef __cplusplus extern "C" { #endif void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *mttyz; /* residuals */ double *mttx; /* states */ double *mttu; /* known + unknown inputs */ double mttt; /* time */ double *mttpar; /* parameters */ unsigned int i; double *p; mttyz = array_of_double (MTTNYZ); mttx = array_of_double (MTTNX); mttu = array_of_double (MTTNU + MTTNYZ); mttpar = array_of_double (MTTNPAR); /* get trial values */ p = mxGetPr (prhs[0]); for (i = 0; i < MTTNYZ; i++) { mttu[MTTNU + i] = p[i]; } /* get states */ p = mxGetPr (prhs[1]); for (i = 0; i < MTTNX; i++) { mttx[i] = p[i]; } /* get known inputs */ p = mxGetPr (prhs[2]); for (i = 0; i < MTTNU; i++) { mttu[i] = p[i]; } /* get time */ p = mxGetPr (prhs[3]); mttt = *p; /* get parameters */ p = mxGetPr (prhs[4]); for (i = 0; i < MTTNPAR; i++) { mttpar[i] = p[i]; } /* evaluate residuals */ <mtt_model_name>_ae (mttyz, mttx, mttu, mttt, mttpar); /* return residuals */ plhs[0] = mxCreateDoubleMatrix (MTTNYZ, 1, mxREAL); p = mxGetPr (plhs[0]); for (i = 0; i < MTTNYZ; i++) { p[i] = mttyz[i]; } /* release memory */ free (mttx); free (mttu); free (mttpar); free (mttyz); } #ifdef __cplusplus } #endif