Modify function_handle in Simulink matlab function

3 ビュー (過去 30 日間)
tianyuan wang
tianyuan wang 2025 年 3 月 23 日
コメント済み: tianyuan wang 2025 年 3 月 23 日
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 件のコメント
tianyuan wang
tianyuan wang 2025 年 3 月 23 日
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.

サインインしてコメントする。

採用された回答

Paul
Paul 2025 年 3 月 23 日
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 件のコメント
tianyuan wang
tianyuan wang 2025 年 3 月 23 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 3 月 23 日
if nonConOption == 1
nonlcon = @circlecon;
else
nonlcon = @(varargin) deal([],[]);
end
  1 件のコメント
tianyuan wang
tianyuan wang 2025 年 3 月 23 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by