"User supplied objective function must return a scalar value." error using fminbnd

14 Ansichten (letzte 30 Tage)
Hello everyone. I am trying to implement the steepest descent algorithm to a convex and nonquadratic function "fun". I need to find the a (alpha) by using fminbnd function in the interval [1,10]. However, I am getting the error "Error using fminbnd: User supplied objective function must return a scalar value." Since xnew and dk matrices also change everytime while loop runs, I could not define the xnew2 function as function handle (I guess, I am not quite sure about the definition of function handle.) I would be glad if someone could help me out with this problem. Thanks a lot.
syms fun(x,y) xnew2(a)
fun(x,y) =(18.*y-0.2.*(x-20).^2).^2 + (20.*x-0.1.*(y-10).^2).^2; %function is defined
g=(gradient(fun,[x,y])); %gradient vector
x0=[0.2 0.2]'; %initial points
xnew=x0; %I don't want to change initial points but I need another variable to start with
iter=0;
dk=-g(xnew(1),xnew(2));
while(norm(dk)>1e-05)
iter=iter+1;
xnew2(a)= xnew+a*dk; %xnew2 is defined with respect to a which as unknown for now
a_new=fminbnd(xnew2,1,10); %a_new will be the local min in the interval 1,10
xnew=xnew+a_new*dk; %xnew will get a new value with the variable
X=['The x vector at iteration ',num2str(iter)];
disp(X)
display(double(xnew));
dk=-g(xnew(1),xnew(2));
end
Error using fminbnd
User supplied objective function must return a scalar value.

Akzeptierte Antwort

Torsten
Torsten am 21 Dez. 2022
Bearbeitet: Torsten am 21 Dez. 2022
syms a
x = sym('x',[2,1]);
fun(x) =(18.*x(2)-0.2.*(x(1)-20).^2).^2 + (20.*x(1)-0.1.*(x(2)-10).^2).^2; %function is defined
g = gradient(fun); %gradient vector
x0 = [0.2 0.2]'; %initial points
xnew = x0; %I don't want to change initial points but I need another variable to start with
iter = 0;
dk = -g(xnew(1),xnew(2));
while norm(dk) > 1e-05 && iter < 9
iter = iter+1;
xnew2 = xnew+a*dk; %xnew2 is defined with respect to a which as unknown for now
f = matlabFunction(fun(xnew2(1),xnew2(2)));
a_new = fminbnd(f,0,10) %a_new will be the local min in the interval 1,10
xnew = xnew+a_new*dk; %xnew will get a new value with the variable
%X=['The x vector at iteration ',num2str(iter)];
%disp(X)
%display(double(xnew));
dk = -g(xnew(1),xnew(2));
double(xnew)
double(fun(xnew(1),xnew(2)))
end
a_new = 0.0010
ans = 2×1
1.6416 2.9776
ans = 969.1248
a_new = 0.0016
ans = 2×1
0.1870 3.6446
ans = 166.7014
a_new = 0.0010
ans = 2×1
0.4113 4.1267
ans = 28.8816
a_new = 0.0017
ans = 2×1
0.1559 4.2564
ans = 4.6244
a_new = 0.0010
ans = 2×1
0.1976 4.3348
ans = 0.7119
a_new = 0.0017
ans = 2×1
0.1578 4.3565
ans = 0.1070
a_new = 0.0010
ans = 2×1
0.1642 4.3684
ans = 0.0163
a_new = 0.0017
ans = 2×1
0.1581 4.3718
ans = 0.0024
a_new = 9.9955e-04
ans = 2×1
0.1591 4.3735
ans = 3.4421e-04

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by