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!