필터 지우기
필터 지우기

How can I create MEX function from a .m program?

조회 수: 2 (최근 30일)
David López de la Cruz
David López de la Cruz 2019년 5월 20일
댓글: David López de la Cruz 2019년 5월 23일
I have a .m script from MATLAB and I want to convert it into an MEX function, in order to fasten the process. The application MATLAB Coder tells me that it's only possible with functions. However, I convert it into a function but it still give me errors to make the conversion. How can I do that conversion? On the other hand, how can I run this program when it is a MEX function?
In my code there is an "ode45" command that goes really slow in process, and it is giving me problems. Is there any possibility to fasten its execution?
Thanks for all
David
  댓글 수: 2
Guillaume
Guillaume 2019년 5월 20일
"but it still give me errors to make the conversion"
Until you tell use what the errors are, we can't tell you how to solve them. So, please give us the full text of the error message.
"Is there any possibility to fasten its execution"
Again, if you don't show us what you do, how can we tell? So show us your code as well.
David López de la Cruz
David López de la Cruz 2019년 5월 23일
I have a .m script with an ode45 function, which calls a function called "Sistema_Ecuaciones_Diferenciales", which is an differential equation system of 10 equations:
MAIN SCRIPT
...
[t,y] = ode45(@(t,y) Sistema_Ecuaciones_Diferenciales (t,y,ti,Fi,beta), tspan, y0, opts);
...
%Fi and beta are two matrix of (ntot,nb) and ti a vector of ntot points from 0 to 1.35.
FUNCTION
function dydt = Sistema_Ecuaciones_Diferenciales(t,y,ti,Fi,beta)
global ms ks mp kp cp mr kr cr kb Cd n c nb ntot %Known data
gamma=zeros(ntot,nb); delta=zeros(ntot,nb); fx=zeros(1,ntot); fy=zeros(1,ntot);
for i=1:ntot
for j=1:nb
delta(i,j)=(y(1)-y(5))*cos(Fi(i,j))+(y(3)-y(7))*sin(Fi(i,j))-c-beta(i,j)*Cd;
if delta(i,j)>0
gamma(i,j)=1;
else
gamma(i,j)=0;
end
fx(i)=fx(i)+gamma(i,j)*((delta(i,j))^n)*cos(Fi(i,j)); fy(i)=fy(i)+gamma(i,j)*((delta(i,j))^n)*sin(Fi(i,j));
end
fx(i)=kb*fx(i); fy(i)=kb*fy(i);
end
fx = interp1(ti,fx,t); fy = interp1 (ti,fy,t);
dydt = zeros(10,1);
dydt(1) = y(2); dydt(2) = -(ks/ms)*y(1)-fx./ms;
dydt(3) = y(4); dydt(4) = -(ks/ms)*y(3)-fy./ms;
dydt(5) = y(6); dydt(6) = -(cp/mp)*y(6)-(kp/mp)*y(5)+fx./mp;
dydt(7) = y(8); dydt(8) = -((cp+cr)/mp)*y(8)-((kp+kr)/mp)*y(7)+(kr/mp)*y(9)+(cr/mp)*y(10)+fy./mp;
dydt(9) = y(10); dydt(10) = -(kr/mr)*(y(9)-y(7))-(cr/mr)*(y(10)-y(8));
end
First of all, it doesn't let me make Code from the Main Script, because it says it's only possible for functions. Moreover, trying to convert the main script into a funciton, I have another problem. This Main Script calls another script with all the data necessary for the run, and there are errors because the variables are not defined in the main script.
The problem could be that t and y are undefined variables, because they are the output variables of the differential equation solver, but indeed the ode45 must have that form.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by