Obtain multiple function handles as outputs from one single function for optimisation.
古いコメントを表示
Hello,
I am in the process of writing a code for an optimisation problem. However, I am stuck with the following problem.
%% Function that generates the cost function and constraint
function [obj_func, constraint]=functiongenerator(a,b,c)
% parameters a, b and c are passed to this function from anotherscript. They are scalar values
[obj_func,constraint]=first_func; %% THIS IS THE LINE OF CONCERN %%
function [fun, cons]=first_func(x)
p=a*x(:,1);
q=b/c*x(:,2);
% more computations with p and q
P=[p q p*q];
Q=[q p p/q];
PQ=Q-P;
fun=@second_func;
cons=@third_func;
function f=second_func(x)
f=norm(PQ); %some dummy operation here for representation. The actual operation is a matrix calculation
end
function ct=third_func(x)
ct=PQ(2,1); %some dummy operation here for representation. The actual operation is a matrix calculation
end
end
end
The above code block represents the function file and the following code block represents the script i am running to execute the function file.
a=10;
b=15;
c=20;
[obj_func, constraint]=functiongenerator(a,b,c)
xval = ga(obj_func,2,[],[],[],[],[],[],constraint);
However when I run this code, I get the error that
---not enough input arguments in "THE LINE OF CONCERN"---
The above code is only for representation. My actual problem involves matrix multiplications.
How would I be able to execute my code? Any help is much appreciated.
Thank you
2 件のコメント
You defined FIRST_FUNC with one input argument (required):
function [fun, cons]=first_func(x)
and then call it with zero input arguments (thus the error):
[obj_func,constraint]=first_func;
I presume that x is the array being optimized. In which case returning the function handles is not the problem, the problem is that the code of FIRST_FUNC uses x, but at that point x has not been defined... nor will it be defined until the GA call your functions. So your data-flow is contradictory: if x is that supplied by GA then it cannot be used in code which is called once before you even call GA. Or lets phrase it as a question: given that FIRST_FUNC gets called only once before GA gets called, what value of x do you expect it to have?
As far as I can tell, FIRST_FUNC is superfluous anyway.
Suhas Raghavendra Kulkarni
2022 年 3 月 3 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!