Execution of script fmincon as a function is not supported

Hello everyone, I'm trying to understand the basics of the fmincon function by implementing the example given on:
openExample('optim/LinearInequalityConstraintExample')
However, while trying to execute the aforementioned command, MatLab doesn't recognize the execution of this function.
fun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
x0 = [-1,2];
A = [1,2];
b = 1;
x = fmincon(fun,x0,A,b);
And the output reads as follows:
Execution of script fmincon as a function is not supported:
G:\My Drive\_COLLEGE_\4. U OF A\2022\2. FALL TERM\PET E 631\fmincon.m
Error in fmincon (line 7)
x = fmincon(fun,x0,A,b);
Note that I'm also using MATLAB R2022a.
Thanks for your help

 Accepted Answer

you named your file fmincon.m
When you go to execute the file and it encounters the fmincon() call, it is going to search the path to figure out which potential fmincon implementation is being referred to. And the very first thing on the path is going to be the very file that is running, which you named fmincon. So it is going to try to call itself recursively, but will fail because it is a script file instead of a function file.
To summarize: do not name your file the same as one of the Mathworks supplied functions, or else you risk your code being invoked instead of the Mathworks code

More Answers (0)

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!