Why my objective function is not evaluating the function for an initial value?
1 次查看(过去 30 天)
显示 更早的评论
I have a function like the following:
function y=testfuncopt(a,b,c)
y=a(1).^2+a(2).^2-b(1).^2-2*b(2).^2+c;
Now I want to evaluate the function for an initial value x0:
x0=[1,2,3,2,1]
objective =@(a,b,c) @testfuncopt;
disp(['Initial Objective: ' num2str(objective(a,b,c))])
The follwoing error happening:
Undefined function or variable 'a'.
Error in testopt1 (line 12)
disp(['Initial Objective: ' num2str(objective(a,b,c))])
0 个评论
采纳的回答
Walter Roberson
2019-5-1
You created a function handle that accepts three parameters with dummy names a b c. The code of the function handle says that when the function handle is invoked it should accept and ignore up to three parameters and should return a handle to testfuncopt but it should not itself invoke that function.
Then you attempt to invoke the function handle you just created passing in variables a b c from the current environment. That is failing because variable a does not exist.
If you had happened to have a b c in your environment then they would have been accepted by the function handle and ignored and the handle to testfuncopt would be returned. num2str would then fail trying to convert the function handle to string.
You talk about evaluating at an initial point and you have an x0 that looks like it might be an initial point for something but you are not passing it to your function handle and it is only one variable not three.
I think what you want is
objective = @(x) testfuncopt(x(1:2), x(3:4), x(5));
disp(['Initial Objective: ' num2str(objective(x0))])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!