MWMCR: Evaluate function error Inner Matrix Dimensions must agree (fmincon issue ?)
4 views (last 30 days)
Show older comments
I have written a matlab code for an optimization problem. The code runs perfectly in MATLAB. But running the deployed code(.NET assembly) I get the following error : "MWMCR: Evaluate function error Inner Matrix Dimensions must agree". I suspect that the problem comes from my objective function for the fmincon function after deployment. But I don't know how to solve it. I have tried to work without nested functions but it doesn't help and I have checked the input argument of my UpdateControlBP method and that seems also not to be the problem. (My c# code may still have some bugs but they should be minor and easy to solve)
Thanksyou in advance
MATLAB CODE: function [Kp_out,Kd_out] = UpdateControlBP(a) %UNTITLED Summary of this function goes here % Detailed explanation goes here
K = -7;
x0 = [2,0.456]; % initial guess;
options =optimset('Algorithm','active-set');
[x,fval] = fmincon(@(x_in)objfun(x_in),x0,[],[],[],[],[],[],@confun,options);
Kp_out = x(1)^(2)/K;
Kd_out = (2*x(2)*x(1)-a)/K;
function fun = objfun(x_in)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
%a=0.5;
%K=-7;
Kp = x_in(1)^(2)/K;
Kd = (2*x_in(2)*x_in(1)-a)/K;
term1 = (1+ (K*(Kd*a-Kp)/(a^(2)+x_in(1)^(2))))^(2);
term2 = Kp*(Kd*x_in(1)^(2)+Kp*a)^(2)/(x_in(1)^(2)*(a^(2)+x_in(1)^(2))^(2));
fun = (term1+term2)^(1/2);
end
function [c,ceq ] = confun(x_in2)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
Ts =2;
zeta_m =0.456;
% Nonlinear inequality constraints
c = [ x_in2(2)*x_in2(1)-4/Ts;zeta_m-x_in2(2)];
% Nonlinear equality constraints
ceq = [];
end
end
C#:
private void UpdateController()
{
objControl = new RecLeaSq();
MWArray[] pole_temp = objControl.FindPole(1, Theta_ident);
pole = (MWNumericArray)pole_temp[0];
result_control = objControl.UpdateControlBP(2,(MWArray)pole);
MWNumericArray ControlPar = (MWNumericArray)result_control;
String Kp = ControlPar.ToVector(MWArrayComponent.Real).GetValue(0).ToString();
String Kd = ControlPar.ToVector(MWArrayComponent.Real).GetValue(0).ToString();
String new_PD_Parameters = Kp + " " + "0" + " " + Kd + " " + Kp + " " + "0" + " " + Kd;
using (StreamWriter newParam = new StreamWriter("controller.txt", false)) {
newParam.WriteLine(new_PD_Parameters);
}
MessageBox.Show(" The controller Parameters have been changed to:" + "\r\n" + "Kp =" + Kp + "\r\n" + "Kd =" + Kd + "\r\n" + "Ki =0");
}
0 Comments
Answers (0)
See Also
Categories
Find more on Deploy to .NET Applications Using MWArray API in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!