Supplied objective function must return a scalar value

I have to minimize a function with FMINCON:
f = @(x,y) k*x.*(teta_in-y)
i create this script to apply fmincon:
x0 = [2000,10];
A = [1 0;0 -1];
b = [25;2273];
f = cell(2,1)
f{1} = @(x) k*x.*(teta_in-y)
f{2} = @(x) k*x.*(teta_in-y)
[xmin,fval] =fmincon(f,x0,A,b)
But the programme give me the error: Supplied objective function must return a scalar value
If you have a different way to minimize that function with fmincon i appreciate that

2 件のコメント

Dyuman Joshi
Dyuman Joshi 2024 年 1 月 8 日
編集済み: Dyuman Joshi 2024 年 1 月 8 日
What are the values of k and teta_in?
Why do you use the same function twice?
Also, share the mathematical definition of the objective function you have to minimize.
EDOARDO GELMI
EDOARDO GELMI 2024 年 1 月 8 日
K and teta_in are two constants. Previously i had an error which said that i had to use a double in order to solve the problem with fmincon, so i created a cell (i searched on the internet but i wasn't sure about it).
What do you mean by "share the mathematical definition..."? I don't understand your request.
Thank you!

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2024 年 1 月 8 日

0 投票

The function needs to return one parameter vector. One way to do that is to create a second function to map the inpouts to to such a vector:
ffcn = @(b)f(b(1),b(2));
Then, providing random values for the missing constants ‘k’ and ‘teta_in’ and running it produces —
k = rand
k = 0.0666
teta_in = rand
teta_in = 0.3083
f = @(x,y) k*x.*(teta_in-y);
ffcn = @(b)f(b(1),b(2));
x0 = [2000,10];
A = [1 0;0 -1];
b = [25;2273];
[xmin,fval] =fmincon(ffcn,x0,A,b)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
xmin = 1×2
1.0e+07 * 0.0000 7.7169
fval = -1.2840e+08
.

11 件のコメント

EDOARDO GELMI
EDOARDO GELMI 2024 年 1 月 8 日
I tried to use a solution which was similiar to the one you are offering but maybe it wasn't correct at all, what @(b) stands for? Why i cannot use @(x) f(x(1),x(2))?
I really appreciate your answer, now i try to use it and i'll let you know.
Thanks!
EDOARDO GELMI
EDOARDO GELMI 2024 年 1 月 8 日
It works so i really want to thank you. Nevertheless i cannot understand why it didn't work when i used a code like this @(x) f(x(1),x(2)), isn't it the same?
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 8 日
"what @(b) stands for?"
It's a placeholder. You can use any valid variable instead.
"Why i cannot use @(x) f(x(1),x(2))?"
You can. But since you have used x already in defining f before, to avoid confusion a different placeholder is used.
k = rand
k = 0.0208
teta_in = rand
teta_in = 0.9567
f = @(x,y) k*x.*(teta_in-y);
ffcn = @(x)f(x(1),x(2));
x0 = [2000,10];
A = [1 0;0 -1];
b = [25;2273];
[xmin,fval] =fmincon(ffcn,x0,A,b)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
xmin = 1×2
1.0e+15 * 0.0000 1.2777
fval = -6.6576e+14
EDOARDO GELMI
EDOARDO GELMI 2024 年 1 月 8 日
Understood, thank you very much for the explanation!
Torsten
Torsten 2024 年 1 月 8 日
You can. But since you have used x already in defining f before, to avoid confusion a different placeholder is used.
b is also in use :
b = [25;2273];
:-)
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 8 日
@Torsten, I meant x as a placeholder, whereas b is a variable, but your point is valid nonetheless.
Best to use different names (as much as possible) to avoid confusion.
Matt J
Matt J 2024 年 1 月 8 日
Understood, thank you very much for the explanation!
@EDOARDO GELMI If so, and everything is clear now, you should Accept-click the answer
EDOARDO GELMI
EDOARDO GELMI 2024 年 1 月 8 日
I already did! I cannot find another button
Star Strider
Star Strider 2024 年 1 月 8 日
Thank you!
In:
ffcn = @(b)f(b(1),b(2));
the ‘b’ argument is local to the function, and does not refer to any other value outside it. (I generally use ‘b’ for parameters because standard optimisantion convention is to define parameters as β, however any variable that makes sense in the context would work.)
.
EDOARDO GELMI
EDOARDO GELMI 2024 年 1 月 8 日
It makes sense, thank you very much!
Star Strider
Star Strider 2024 年 1 月 8 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by