Modify function_handle in Simulink matlab function
显示 更早的评论
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 个)
Walter Roberson
2025-3-23
if nonConOption == 1
nonlcon = @circlecon;
else
nonlcon = @(varargin) deal([],[]);
end
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!