"User supplied objective function must return a scalar value." error using fminbnd
14 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Torsten
on 21 Dec 2022
Edited: Torsten
on 21 Dec 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
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!