Nonlinear Constraint with Secondary Variable fmincon
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Sai Sumanth Korthiwada
2023-3-27
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!