Matlab genetic algorithm plotobjective error

5 views (last 30 days)
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!

Accepted Answer

Alan Weiss
Alan Weiss on 19 Jul 2017
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
  1 Comment
Sara
Sara on 19 Jul 2017
Thanks for your reply Alan. I have a non-linear constraint. I solved the problem by fmincon before, but just wanted to compare the results using ga.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!