Why is MATLAB giving me a wrong answer when solving a minimization problem?

In summary: The more complicated the function and constraints the more thought needs to go into this.In summary, the conversation discusses using the fmincon function in MATLAB to solve a minimization problem with nonlinear constraints. The problem is that the initial feasible point chosen affects the final solution, with one point giving a correct answer and another giving an incorrect answer. This highlights the limitations of optimization algorithms and the importance of choosing the starting point carefully. The solution also includes a demonstration of the issue using a plotted function.
  • #1
math8
160
0
In MATLAB, I am using fmincon to solve a minimization problem with nonlinear constraints. The problem is that, it is giving me a wrong answer, a point that is not a minimizer (not even close), and that is not within the tolerance. I made sure to use a feasible initial point.

However, when I use another initial feasible point, it gives me a correct answer.

Here is the problem:

min f = 100*(x(2)-x(1)^2)^2+(1-x(1))^2;
s.t. constraints= [1-(x(1)*x(2));-x(1)-(x(2))^2;x(1)-0.5] <= 0

The minimizer is [0.5 ; 2] with optimal objective value: 306.5000

However when I use the feasible initial point x0=[-1 ; -2], this gives me the answer: [-0.7921; -1.2624] (which is not feasible and has an f value of 360.3798 !) (to within options.TolCon = 1e-06)

and when I use the initial point x0=[0.4;4], this gives me the correct answer [0.5 ; 2].

Note that my constraints do not require the variables to be non-negative.

Any idea what I am doing wrong here?
 
Physics news on Phys.org
  • #2
You're not doing anything "wrong" , but you are being given a demonstration of the potential limitations of optimization algorithms and the need to choose the starting point carefully. I don't have Matlab so I've implemented your function in Mathcad and used its Solve Block capability to minimize the function given the constraints.

If you look at the left hand plot in image below you should see black contour lines (the function), two clusters of blue contour lines (the constraint boundaries) and 4 coloured dots (2 initial points and 2 solutions).

You should note that the first 'incorrect' minimum is within the larger constrained area of the x-y plane and the solution has migrated towards the constraint boundary. The right hand plot may show the descent slightly more clearly. What's happened is that the minimization algorithm (Conjugate Gradient) hits the constraint if it tries to move further down the function slope, so it halts with the best value it can find (which has strayed slightly over the boundary).

The 'correct' minimum starts from the second constrained area and follows the same process of descending the slope until it hits the boundary and can't progress any further. It also is just over the boundary. (The algorithms don't necessarily treat constraints as hard conditions but will try to meet them as closely as possible).

A useful technique is to plot your function first to get a feel for where to start - you may even need to calculate it for several points and choose the minimum.

attachment.php?attachmentid=55019&stc=1&d=1359204745.jpg
 

Attachments

  • phys - 13 01 26 optimization 01.jpg
    phys - 13 01 26 optimization 01.jpg
    48.2 KB · Views: 934
Last edited:

FAQ: Why is MATLAB giving me a wrong answer when solving a minimization problem?

1. How can I determine if the solution given by MATLAB is actually wrong?

One way to check if the solution given by MATLAB is wrong is to manually solve the minimization problem using the same inputs and compare the results. If there is a significant difference between the two solutions, then it is likely that there is an error in the MATLAB code or the inputs.

2. What are some common reasons for MATLAB giving a wrong answer in minimization problems?

Some common reasons for MATLAB giving a wrong answer in minimization problems include incorrect input values, incorrect use of functions or algorithms, and programming errors such as typos or logical mistakes.

3. How can I troubleshoot and fix a wrong answer in a minimization problem in MATLAB?

To troubleshoot and fix a wrong answer in a minimization problem in MATLAB, you can try checking your inputs, reviewing your code for any errors, and verifying that you are using the correct functions and algorithms. You can also try debugging your code or seeking help from other MATLAB users or online forums.

4. Is it possible for MATLAB to give a wrong answer in a minimization problem due to numerical errors?

Yes, it is possible for MATLAB to give a wrong answer in a minimization problem due to numerical errors. This can occur when the problem is ill-conditioned or when the algorithm used by MATLAB is not suitable for the problem at hand. In such cases, it may be necessary to use a different algorithm or adjust the precision settings in MATLAB.

5. How can I prevent MATLAB from giving a wrong answer in minimization problems in the future?

To prevent MATLAB from giving a wrong answer in minimization problems in the future, it is important to carefully check and verify your inputs, use appropriate functions and algorithms, and regularly test and debug your code. It can also be helpful to seek guidance from experienced MATLAB users or consult the official documentation for best practices and tips.

Similar threads

Replies
8
Views
1K
Replies
3
Views
4K
Replies
1
Views
2K
Replies
18
Views
3K
Replies
10
Views
2K
Replies
2
Views
1K
Replies
8
Views
2K
Replies
2
Views
1K
Back
Top