00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <math.h>
00018 #include "mex.h"
00019
00020
00021
00022 #define T_IN prhs[0]
00023 #define Y_IN prhs[1]
00024
00025
00026
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;
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
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
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
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
00093 YP_OUT = mxCreateDoubleMatrix(m, n, mxREAL);
00094
00095
00096 yp = mxGetPr(YP_OUT);
00097
00098 t = mxGetPr(T_IN);
00099 y = mxGetPr(Y_IN);
00100
00101
00102 yprime(yp,t,y);
00103 return;
00104
00105 }
00106
00107