MATLAB Answers

# How can I maximize a function in Matlab?

40 views (last 30 days)
Abdullah Türk on 9 Sep 2019
Commented: John D'Errico on 9 Sep 2019
Hi everyone,
A(i,1) = sum(B(i,:)) + C*3;
I have function A as above. I wan t to maksimize this function. How can I do this?
Thanks.

#### 4 Comments

Show 1 older comment
Abdullah Türk on 9 Sep 2019
Actually, C is nor an important value. I want to maximize the value A using genetic algorithm. For example, while iteration = 50, I obtained the following values:
Iteration1: Best_value = 5870
Iteration2: Best_value = 5548
Iteration3: Best_value = 4870
Iteration4: Best_value = 4870
Iteration5: Best_value = 4870
Iteration6: Best_value = 4870
Iteration7: Best_value = 4870
Iteration8: Best_value = 4870
Iteration9: Best_value = 4870
Iteration10: Best_value = 4870
Iteration11: Best_value = 4870
Iteration12: Best_value = 4870
Iteration13: Best_value = 4870
Iteration14: Best_value = 4870
Iteration15: Best_value = 4870
Iteration16: Best_value = 4870
Iteration17: Best_value = 4870
Iteration18: Best_value = 4870
Iteration19: Best_value = 4870
Iteration20: Best_value = 4870
Iteration21: Best_value = 4870
Iteration22: Best_value = 4870
Iteration23: Best_value = 4870
Iteration24: Best_value = 4870
Iteration25: Best_value = 4870
Iteration26: Best_value = 4870
Iteration27: Best_value = 4870
Iteration28: Best_value = 4870
Iteration29: Best_value = 4870
Iteration30: Best_value = 4870
Iteration31: Best_value = 4870
Iteration32: Best_value = 4870
Iteration33: Best_value = 4378
Iteration34: Best_value = 4378
Iteration35: Best_value = 4378
Iteration36: Best_value = 4378
Iteration37: Best_value = 4378
Iteration38: Best_value = 4378
Iteration39: Best_value = 3683
Iteration40: Best_value = 3683
Iteration41: Best_value = 3683
Iteration42: Best_value = 3683
Iteration43: Best_value = 3683
Iteration44: Best_value = 3683
Iteration45: Best_value = 3683
Iteration46: Best_value = 3683
Iteration47: Best_value = 3683
Iteration48: Best_value = 3683
Iteration49: Best_value = 3683
Iteration50: Best_value = 3683
Here it seems as if I'm minimizing the problem, but I want to achieve the opposite of this process, to maximize.
John D'Errico on 9 Sep 2019
Why did you unaccept Jan's answer? It is the correct solution. What was wrong with it? Just negate the function, exactly as he said. Then you are minimizing the negative of your function, which maximizes it! So WTP?
Abdullah Türk on 9 Sep 2019
I didn't say Jan's answer was wrong. I applied the Jan's answer for my code and obtained the right result. So, there are no problems left.

Sign in to comment.

### Accepted Answer

Jan on 9 Sep 2019
Matlab contains some tools for minimization. A maximization does exactlly the same, if you change the sign of the optimization criterion.

#### 4 Comments

Show 1 older comment
John D'Errico on 9 Sep 2019
Yes.
Abdullah Türk on 9 Sep 2019
Jan and John D'Errico thank you both. I applied the solution you suggested above and after a bit of work I maximized the my problem. Thanks again.
John D'Errico on 9 Sep 2019
In fact, most optimizers seem to be minimization codes. It just seems a logical standard, since often one wants to minimize a sum of squares, perhaps. That may acttually be the most common general class of optimization problem overall anyway. So it makes sense that all optimizers will be consistent, thus minimizers. That way you need not remember which direction some tool works, as opposed to others. And of course, negating a function makes a min into a max, and vice versa.

Sign in to comment.