필터 지우기
필터 지우기

Simulink Embedded MATLAB Function

조회 수: 1 (최근 30일)
Nick Bruno
Nick Bruno 2011년 3월 17일
Hi, I am trying to use an embedded matlab function in my simulink model and I am running into a problem.
My embedded function is dependent on one input variable, and the input for my embedded matlab function is being produced from a DataStoreRead source block. The embedded function uses this input variable, from the DataStoreRead source block, to solve a system of two transcendental equations for two unknowns using the fsolve function. The output produced by the fsolve funtion is a vector containing two entries "x"; x =[x(1) x(2)]. I need to use both values obtained from solving the system of equations as outputs from my embedded matlab function block. I need to solve the set of transcendental equations for every time step of my simulation.
When I try to do this, I get an error saying the embedded function block does not support mxArray. Is there any way around this error?
function [theta_3_out,theta_4_out,Hy_out,Hx_out]= fcn(u)
% This block supports the Embedded MATLAB subset.
% See the help menu for details.
Msat = 572490;
Dxx = 0.465;
Dyy = 0.065;
rho_K1= 1.9e5;
mu_0 = 4e-7 * pi();
Happ_in = 0.8/mu_0;
xi_in = u;
Msat_str=num2str(Msat);
rho_K1_str=num2str(rho_K1);
mu_0_str=num2str(mu_0);
Dxx_str=num2str(Dxx);
Dyy_str=num2str(Dyy);
xi_str=num2str(xi_in);
Happ_str=num2str(Happ_in);
%First equation
f1=strcat('(',Msat_str,'*',xi_str,'*(',Dyy_str,'-',Dxx_str,')+2*',rho_K1_str,...
'/(',mu_0_str,'*',Msat_str,'))*sin(x(1))*cos(x(1))+',Msat_str,'*',xi_str,...
'*(',Dxx_str,'*sin(x(1))*sin(x(2))+',Dyy_str,'*cos(x(1))*cos(x(2)))-',...
Happ_str,'*cos(x(1))');
%Second equation
f2=strcat('(',Msat_str,'*',xi_str,'*(',Dyy_str,'-',Dxx_str,')-2*',rho_K1_str,...
'/(',mu_0_str,'*',Msat_str,'))*sin(x(2))*cos(x(2))+',Msat_str,'*(1-',xi_str,...
')*(',Dxx_str,'*cos(x(1))*cos(x(2))+',Dyy_str,'*sin(x(1))*sin(x(2)))-',...
Happ_str,'*sin(x(2))');
%Forming a vector for input into fsolve
F=strcat('[',f1,';',f2,']');
options=optimset('Display','off');
[x,y,fval,exitflag,output]=fsolve(F,[0;0],options);
theta_3_out=x(1);
theta_4_out=x(2);
Hx_out=Dxx*Msat*(u*sin(theta_4_out)-(1-u)*cos(theta_3_out));
% if theta_3_out>=pi/2
% Happ_in=495205*xi_in+528700;
% end
Hy_out=Happ_in-Dyy*Msat*((1-u)*sin(theta_3_out)+u*cos(theta_4_out));

채택된 답변

Rob Graessle
Rob Graessle 2011년 3월 18일
When you call FSOLVE from Embedded MATLAB it is called as an extrinsic function, meaning the return type is an mxArray. To use the mxArray in the rest of your function, you must assign the mxArray to a variable that has a defined data type. To do this you can just add the following line before you call FSOLVE:
x=zeros(2,1);
This is covered in more detail in the documentation section on Calling MATLAB Functions from Embedded MATLAB.
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 3월 18일
So I did get it right then? Cool!
Nick Bruno
Nick Bruno 2011년 3월 18일
Thank you so much. You helped me tremendously.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 3월 17일
I am not at all certain that this is the answer, but on some questions that have gone by on related topics, the solution is to pre-allocate all arrays to their maximum return size, even though syntactically they will get completely overwritten by being return values from a call. I gather that embedded Matlab doesn't do dynamic allocation for return values. (Unless maybe for string operations... I don't know.)

카테고리

Help CenterFile Exchange에서 Naming Conventions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by