Nonlinear Constraint with Secondary Variable fmincon

3 views (last 30 days)
Hello All;
I want to use nonlinear constarint in my fmincon command, however I want to define such equation
c=b*(x^2)+4
x= optimized coefficient
b=value calculated anaother function, this value also depends on the optimized x values, it is calculated using the x values.
However , it can not get the value of 'b' from within the function and use it so gives an error.
"Unrecognized function or variable 'b'
Even though I call the function from which I get the 'b' values, it still gives the same error.
How can I solve this problem?

Answers (1)

Sai Sumanth Korthiwada
Sai Sumanth Korthiwada on 27 Mar 2023
Hi Yasin,
As the error log suggests, " Unrecognized function or variable 'b' " describes that the variable 'b' is not yet created while the execution is in that line.
And, as you are calling a function to get the 'b' values, the 'b' values may not have returned after the function execution. Please ensure that the 'b' values are returned from the function and are stored in a global variable 'b', so that c=b*(x^2)+4 works. If 'b' is declared within a function, its scope will be within that function only. It cannot be accessed outside the function, it has to be returned to global/main. You can have breakpoints to verify whether 'b' is created while the execution is in line c=b*(x^2)+4.
Hope this answers your query!

Community Treasure Hunt

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

Start Hunting!