Modify function_handle in Simulink matlab function

33 visualizzazioni (ultimi 30 giorni)
Hi there,
I want to modfy the type of function_handle in Simulink Matlab function. Here is the codes
nonConOption = 1;
nonlcon = [];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
lb = [0,0.2];
ub = [0.5,0.8];
if nonConOption == 1
nonlcon = @circlecon;
end
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
function [c,ceq] = circlecon(x)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2;
ceq = [];
end
But the error reports
It is not possible to write a value of type function_handle to a variable of type double. Code generation does not support changing the type by assignment. To investigate the cause of the type mismatch, check the previous assignment or input type setting.
How to create an empty function_handle?
I don't want to repeat the call fmincon like
if nonConOption == 1
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@circlecon)
else
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,[])
end
best regards,
Tianyuan
  1 Commento
tianyuan wang
tianyuan wang il 23 Mar 2025
When I try to use
nonlcon = @(x)[];
if nonConOption == 1
nonlcon = @circlecon;
end
I reports:
It is not possible to write a value of type function_handle to a variable of type function_handle. Code generation does not support changing the type by assignment. To investigate the cause of the type mismatch, check the previous assignment or input type setting.

Accedi per commentare.

Risposta accettata

Paul
Paul il 23 Mar 2025
Assuming the ultimate goal is to have nonConOption as an input to the MatlabFunction block, I got this to work in the MatlabFunction block
function x = fcn(nonConOption)
%nonConOption = 1;
%nonlcon = [];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
lb = [0,0.2];
ub = [0.5,0.8];
%{
if nonConOption == 1
nonlcon = @circlecon;
else
nonlcon = @(x) deal([],[]);
end
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,optimoptions('fmincon','Algorithm','sqp'));
%}
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@(x) foofcn(x,nonConOption),optimoptions('fmincon','Algorithm','sqp'));
end
function [c,ceq] = foofcn(x,nonConOption)
if nonConOption == 1
[c,ceq] = circlecon(x);
else
c = [];
ceq = [];
end
end
function [c,ceq] = circlecon(x)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2;
ceq = [];
end
The commented code worked fine for the case as posted where nonConOption = 1 is hard-coded, but it doesn't build if nonConOption is an input to the function.
I'm actually pleasantly surprised that this code is operable. I'm kind of curious about how the code for foofcn is generated where c is not empty on return from circlecon but is empty otherwise.
  1 Commento
tianyuan wang
tianyuan wang il 23 Mar 2025
Thank you for your suggestions.
I changed the codes to
function x = fcn(nonConOption)
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
lb = [0,0.2];
ub = [0.5,0.8];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@(x) circlecon(x,nonConOption),optimoptions('fmincon','Algorithm','sqp'));
end
function [c,ceq] = circlecon(x,nonConOption)
c = [];
ceq = [];
if nonConOption == 1
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2;
end
end
It also works well.

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 23 Mar 2025
if nonConOption == 1
nonlcon = @circlecon;
else
nonlcon = @(varargin) deal([],[]);
end
  1 Commento
tianyuan wang
tianyuan wang il 23 Mar 2025
Thank you @Walter Roberson for your suggestions.
Actually in @circlecon, it contains a complex if function. So I use the strategy posted above.
Best reagrds,
Tianyuan

Accedi per commentare.

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by