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 CenterFile Exchange에서 Solver 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