Modify function_handle in Simulink matlab function
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 23 Mar 2025
if nonConOption == 1
nonlcon = @circlecon;
else
nonlcon = @(varargin) deal([],[]);
end
1 commentaire
tianyuan wang
le 23 Mar 2025
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!