Thursday, 20 August 2009

Matlab: Mex example in C way

There is a yprime.c for mex in Matlab. Here I translate to C, in order to understand mex better.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void yprime(
double* yp,
double* y
)
{
double r1,r2;

double mu = 1/82.45;
double mus = 1 - 1/82.45;

r1 = sqrt((y[0]+mu)*(y[0]+mu) + y[2]*y[2]);
r2 = sqrt((y[0]-mus)*(y[0]-mus) + y[2]*y[2]);

yp[0] = y[1];
yp[1] = 2*y[3]+y[0]-mus*(y[0]+mu)/(r1*r1*r1)-mu*(y[0]-mus)/(r2*r2*r2);
yp[2] = y[3];
yp[3] = -2*y[1] + y[2] - mus*y[2]/(r1*r1*r1) - mu*y[2]/(r2*r2*r2);
return;
}

int main(){
double yo[4];
double yi[4] = {100, 2, 3, 4};

yprime(yo, yi);
printf("print out %f",yo[0]);



// some conversion practice
double a = yi[0];
char t1[7],t2[8],t3[8];

*t1= (char) a;
strcpy(t2,t1);

printf(strcat(t1, t2));

gcvt(a,7,t3);
puts(t3);

// i = sprintf(str, "%d", 15); //for interger to string


return 0;
}

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter