フィルターのクリア

Error: Operator '-' is not supported for operands of type 'function_handle'.

167 ビュー (過去 30 日間)
Daniela Würmseer
Daniela Würmseer 2022 年 2 月 10 日
コメント済み: Star Strider 2022 年 2 月 10 日
Hello, i have the following Code:
f1 = [-1,-1];
f2=@(x)-10*x(1)+x(1)^2-4*x(2)+x(2)^2;
A = [3 1;
2 1 ;
1 2];
b = [12; 9; 12];
z = [0,0];
d = [-1,0];
Method(f1,f2,A,b,z,d);
function [x,alpha] = Method(f1,f2,A,b,z,d)
f =@(x)-x(size(A,2)+1);
x0 = (zeros(1,size(A,2) + 1));
lb = [(zeros(1,size(A,2))),-Inf];
if (~isempty(f1))
Anew = [f1,-d(1)];
Anew(2:size(A,1)+1,1:size(A,2)) = A;
bnew = [z(1);b];
c1 =@(x) f2 - d(2)*x(size(A,2)+1) - z(2);
(else ... %in this case Anew,bnew and c1 would look different)
end
nonlincon = @constr;
function [c,ceq] = constr(x)
c = c1;
ceq = [];
end
[x,alpha]=fmincon(f,x0,Anew,bnew,[],[],lb,[],nonlincon);
end
This gives me the following error: Operator '-' is not supported for operands of type 'function_handle'.
I know that it is because of the line "c1 =@(x) f2 - d(2)*x(size(A,2)+1) - z(2);". Does someone knows how i could solve this problem? Thanks.
  2 件のコメント
David Hill
David Hill 2022 年 2 月 10 日
A general description of what you are trying to do with inputs and expected outputs would be helpful. I cannot follow your code (lots of problems).
Daniela Würmseer
Daniela Würmseer 2022 年 2 月 10 日
It is a part of a specific Algorithm. I want to use the fmincon function to solve a nonlinear Problem. At the beginning f1, f2, A,b,z and d are given. But inside the function "Method" we want to change this problem still a bit before giving it into fmincon.

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

採用された回答

Star Strider
Star Strider 2022 年 2 月 10 日
To be used in calculations, functions must be evaluated, and that means providing them with an argument.
See if this provides the desired result:
c1 =@(x) f2(x) - d(2)*x(size(A,2)+1) - z(2);
I am confindent that will work and not throw the previous error. I have not tested the code with this correction.
.
  6 件のコメント
Daniela Würmseer
Daniela Würmseer 2022 年 2 月 10 日
Thank you again. Now it is working :D
Star Strider
Star Strider 2022 年 2 月 10 日
As always my (our with the contribution by @Steven Lord) pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by