"User supplied objective function must return a scalar value." error using fminbnd
14 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Torsten
2022 年 12 月 21 日
編集済み: Torsten
2022 年 12 月 21 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!