core/matlab/examples/mex/yprime.c

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

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