Simulink Embedded MATLAB Function
Ältere Kommentare anzeigen
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));
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 17 Mär. 2011
0 Stimmen
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.)
Kategorien
Mehr zu Texas Instruments C2000 Processors finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!