Matlab genetic algorithm plotobjective error
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi! I used Matlab ga (genetic algorithm) for optimization. I wrote my objective function in a separate .m file. when I run the main code there is no problem and I get the optimal response; However, I cannot run the objfun on its own by pressing Run button. Now, I want to use ''plotobjective'' command in my main code, but the error appears about objective function. My first question is: can I use plotobjective as a command in my main .m file? If yes, how can I fix the problem of my objective function to plot my objective function?
The objective function that I wrote in a separate file is :
function f = objfun(x,a,b)
x1 = x(1:24);
y1 = x(25:end);
f = -mean (x1*a')+mean(y1*b');
end
''x1'' and ''y1'' are 1x24 vectors and ''a'' and ''b'' are calculated in my main file and are constant 1x24 vectors. so, at the end f (objective function) is a scalar.
In the main file I wrote: fun = @(x)objfun(x,a,b);
and I use below command to get the plot of objective function: plotobjective(fun,[-2 2; -2 2]);
Would you please tell me why the error appears about the objective function and how can I fix it? Thanks!
댓글 수: 0
채택된 답변
Alan Weiss
2017년 7월 19일
plotobjective is for functions of two variables only, not for functions of 48 variables. It is not easy to plot a function of 48 variables.
Also, why are you using ga to optimize a linear objective function? Unless your constraints are discontinuous, you should be using an Optimization Toolbox solver. And if your constraints are discontinuous, you would almost certainly be better served with patternsearch.
Alan Weiss
MATLAB mathematical toolbox documentation
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!