core/matlab/examples/shrlib/yprime.c

00001 /*=================================================================
00002  *
00003  * YPRIME.C Sample .MEX file corresponding to YPRIME.M
00004  *          Solves simple 3 body orbit problem 
00005  *      Modified function to demonstrate shared library calling
00006  *
00007  * The calling syntax for the mex funcion is:
00008  *
00009  *      [yp] = yprime(t, y)
00010  *
00011  *  You may also want to look at the corresponding M-code, yprime.m.
00012  *
00013  * This is a MEX-file for MATLAB.  
00014  * Copyright 1984-2003 The MathWorks, Inc.
00015  *
00016  *=================================================================*/
00017 /* $Revision: 1.1 $ */
00018 #include <math.h>
00019 #include "mex.h"
00020 #define EXPORT_FCNS
00021 #include "shrhelp.h"
00022 
00023 
00024 /* Input Arguments */
00025 
00026 #define T_IN    prhs[0]
00027 #define Y_IN    prhs[1]
00028 
00029 
00030 /* Output Arguments */
00031 
00032 #define YP_OUT  plhs[0]
00033 
00034 #if !defined(MAX)
00035 #define MAX(A, B)   ((A) > (B) ? (A) : (B))
00036 #endif
00037 
00038 #if !defined(MIN)
00039 #define MIN(A, B)   ((A) < (B) ? (A) : (B))
00040 #endif
00041 
00042 #define PI 3.14159265
00043 
00044 static  double  mu = 1/82.45;
00045 static  double  mus = 1 - 1/82.45;
00046 
00047 
00048 EXPORTED_FUNCTION void yprimefcn(
00049            double   yp[],
00050            double   *t,
00051            double   y[]
00052            )
00053 {
00054     double  r1,r2;
00055     
00056     r1 = sqrt((y[0]+mu)*(y[0]+mu) + y[2]*y[2]); 
00057     r2 = sqrt((y[0]-mus)*(y[0]-mus) + y[2]*y[2]);
00058 
00059     /* Print warning if dividing by zero. */    
00060     if (r1 == 0.0 || r2 == 0.0 ){
00061     mexWarnMsgTxt("Division by zero!\n");
00062     }
00063     
00064     yp[0] = y[1];
00065     yp[1] = 2*y[3]+y[0]-mus*(y[0]+mu)/(r1*r1*r1)-mu*(y[0]-mus)/(r2*r2*r2);
00066     yp[2] = y[3];
00067     yp[3] = -2*y[1] + y[2] - mus*y[2]/(r1*r1*r1) - mu*y[2]/(r2*r2*r2);
00068     return;
00069 }
00070 
00071 EXPORTED_FUNCTION mxArray* better_yprime(
00072            double   t,
00073            mxArray* y_in)
00074 {
00075     double *yp; 
00076     double *y; 
00077     unsigned int m,n;
00078     mxArray* yp_out;
00079      
00080     m = mxGetM(y_in); 
00081     n = mxGetN(y_in);
00082     if (!mxIsDouble(y_in) || mxIsComplex(y_in) || 
00083     (MAX(m,n) != 4) || (MIN(m,n) != 1)) { 
00084     mexErrMsgTxt("YPRIME requires that Y be a 4 x 1 vector."); 
00085     } 
00086     /* Create a matrix for the return argument */ 
00087     yp_out = mxCreateDoubleMatrix(m, n, mxREAL); 
00088     
00089     /* Assign pointers to the various parameters */ 
00090     yp = mxGetPr(yp_out);
00091     
00092     y = mxGetPr(y_in);
00093         
00094     /* Do the actual computations in a subroutine */
00095     yprimefcn(yp,&t,y); 
00096     return yp_out;
00097 }
00098 
00099 void mexFunction( int nlhs, mxArray *plhs[], 
00100           int nrhs, const mxArray*prhs[] )
00101      
00102 { 
00103     double *yp; 
00104     double *t,*y; 
00105     unsigned int m,n; 
00106     
00107     /* Check for proper number of arguments */
00108     
00109     if (nrhs != 2) { 
00110     mexErrMsgTxt("Two input arguments required."); 
00111     } else if (nlhs > 1) {
00112     mexErrMsgTxt("Too many output arguments."); 
00113     } 
00114     
00115     /* Check the dimensions of Y.  Y can be 4 X 1 or 1 X 4. */ 
00116     
00117     m = mxGetM(Y_IN); 
00118     n = mxGetN(Y_IN);
00119     if (!mxIsDouble(Y_IN) || mxIsComplex(Y_IN) || 
00120     (MAX(m,n) != 4) || (MIN(m,n) != 1)) { 
00121     mexErrMsgTxt("YPRIME requires that Y be a 4 x 1 vector."); 
00122     } 
00123     
00124     /* Create a matrix for the return argument */ 
00125     YP_OUT = mxCreateDoubleMatrix(m, n, mxREAL); 
00126     
00127     /* Assign pointers to the various parameters */ 
00128     yp = mxGetPr(YP_OUT);
00129     
00130     t = mxGetPr(T_IN); 
00131     y = mxGetPr(Y_IN);
00132         
00133     /* Do the actual computations in a subroutine */
00134     yprimefcn(yp,t,y); 
00135     return;
00136     
00137 }
00138 
00139 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy