Simulink Embedded MATLAB Function
1 回表示 (過去 30 日間)
古いコメントを表示
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));
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (1 件)
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.)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Naming Conventions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!