logo资料库

MATLAB 中mex 应用.doc

第1页 / 共18页
第2页 / 共18页
第3页 / 共18页
第4页 / 共18页
第5页 / 共18页
第6页 / 共18页
第7页 / 共18页
第8页 / 共18页
资料共18页,剩余部分请下载后查看
4.在命令行输入mymex,将会 输出c语言编译为mex文件例子
matlab和c语言混合编程 一个简单例子。 1. mex 配置 在命令行输入 mex –setup 然后根据提示操作即可。 2. 编写c语言文件 /* mymex.c*/ #include "mex.h" void mexFunction (int nlhs,mxArray* plhs[],int nrhs,const mxArray* prhs[]) {mexPrintf("c语言编译为mex文件例子"); } 说明 mexfunction 是c语言编写mex文件必须要有的函数,相当于main()函数。 参数说明:nlhs:输出参数个数 plhs: 输出参数的mxArray 数组 nrhs:输入参数个数 prhs:输入参数的mxArray数组 3.将c程序编译为mex文件 mex mymex.c 编译成功后,将生成一个mymex.mexw32或mymex.mexw64文件 4.在命令行输入mymex,将会 输出c语言编译为mex文件例子 例子二 储油罐问题计算 储油罐问题中计算最优参数时,影响速度的主要因素为函数的计算和积分的计 算,我们可以用C语言实现,将其编译为mex文件,供matlab 调用。 #include "mex.h" double surf(double x,double h,double alpha,double beta) {double rx,HH,hx,s; 1
alpha=alpha/180*3.1416;beta=beta/180*3.1416; if ((x<=-2.0)&&(x>-3.0) ) rx=sqrt(2.6406-(x+11.0/8.0)*(x+11.0/8.0)); else if ((x>=6.0)&&(x<7.0)) rx=sqrt(2.6406-(x-43.0/8.0)*(x-43.0/8.0)); else rx=1.5; if (x==-3.0||x==7) rx=0; hx=h-x*tan(alpha)/cos(beta)-(1.5-rx); HH=rx-(rx-hx)*cos(beta); if (HH>2.0*rx ) HH=2.0*rx; if (rx==0.0||HH<=0.0) s=0.0; else s=rx*rx*acos((rx-HH)/rx)-sqrt(rx*rx-(rx-HH)*(rx-HH))*(rx-HH) ; return s; 2
} double Simpson(double(*f)(double,double,double,double),double a,double b,int n,double hh,double alpha,double beta) { int k; double s,s1,s2; double h=(b-a)/n; s1=f(a+h/2,hh,alpha,beta); s2=0.0; for(k=1;k<=n-1;k++) { s1+=f(a+k*h+h/2,hh,alpha,beta); s2+=f(a+k*h,hh,alpha,beta); } s=h/6*(f(a,hh,alpha,beta)+4*s1+2*s2+f(b,hh,alpha,beta)); return s; } void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { 3
int i; double x,h,alpha,beta,v; if (nrhs<3) return ; h=*mxGetPr(prhs[0]); alpha=*mxGetPr(prhs[1]); beta=*mxGetPr(prhs[2]); v=1000*Simpson(surf,-3,7,200,h,alpha,beta); plhs[0]=mxCreateDoubleScalar(v); } 应用例子三 模拟退火法计算tsp问题 编译为.mexw64或mexw32文件 输入参数为53个城市的邻接矩阵,输出最优解和最优距离 #include "mex.h" #define N 53 // 城市个数 #define Markov_length 5000 #define TFIRST 100 #define TEND 1 #define TFRAC 0.99 #define NN 624 #define M 397 4
#define MATRIX_A 0x9908b0dfUL /* constant vector a */ #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ static unsigned long mt[NN]; /* the array for the state vector */ static int mti=NN+1; /* mti==NN+1 means mt[NN] is not initialized */ void anneal(double w[N][N]); double calobj(double w[N][N],int *iorder); void exchange1(int iorder[N], int, int); void exchange2(int iorder[N], int, int, int); void exchange3(int iorder[N], int, int); int metrop(double de,double t); double genrand_real1(void); unsigned long genrand_int32(void); unsigned int genrand(void); double ran(); double best; double best_sol[N]; void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { 5
double w[N][N]; int iorderc[N]; double *x = mxGetPr(prhs[0]); for (int i = 0; i
y[Markov_length], z[Markov_length], u[Markov_length]; for (int i=0;iTEND) { // // // // // c1 = 0; c2 = 0; c3 = 0; srand((unsigned)time(NULL)); for (int i = 0; i < Markov_length; i++)x[i] = rand() % N; for (int i = 0; i < Markov_length; i++)y[i] = rand() % N; for (int i = 0; i < Markov_length; i++)z[i] = rand() % N; for (int i = 0; i < Markov_length; i++)u[i] = rand() % 3; for (int i = 0; i < Markov_length; i++)x[i] = genrand() % N; for (int i = 0; i < Markov_length; i++)y[i] = genrand() % N; for (int i = 0; i < Markov_length; i++)z[i] = genrand() % N; for (int i = 0; i < Markov_length; i++)u[i] = genrand() % 3; 7
for (int i=0;i< Markov_length;i++) { long idum; // x=ran(&idum); if (u[i] < 1) { } exchange1(new_sol,x[i],y[i]); c1++; else if (u[i] < 2) { } exchange2(new_sol,x[i],y[i],z[i]); c2++; else { exchange3(new_sol,x[i],y[i]); c3++; } mynew=calobj(w,new_sol); de=mynew-current; if (metrop(de, t)) { for (int j = 0; j
分享到:
收藏