How to verify my optimal solution given by Matlab

3 views (last 30 days)
Hello,
I am trying to verify that the solution that Matlab provides as my optimal objective value (fval) is correct.
For some reason when I put the values that matlab generates back into my problem just to verify that the same optimal solution will be returned, I can never get the same answer. Is there a logical reason for this or am I doing something wrong?
For instance, My Objective function may be: 4x + 6t + 8q and matlab may return the values: 3,4, and 5 respectively along with an fval as the optimal solution. But when I try and put the values 3,4, and 5 back into my function to verify the answer, I don't get the same solution.
Of course, my actual problem is way more complicated than the above example, but any type of explanation would help. I am trying to publish this code in a book.
Thanks, Kam
  3 Comments
Kam Selley
Kam Selley on 24 Oct 2012
By same solution , I mean that I don't get the same optimal value (fval) that the program generates after I plug the solution values for x back into the program.
I am using fmincon as my solver.
Matt J
Matt J on 24 Oct 2012
Walter's suggestion sounds like a possibility. Show us an example of the 2 different fvals that you get.

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 23 Oct 2012
Possibly round-off in the display of the solutions, if you are relying on what is displayed instead of assigning the values to variables and passing in those variables.

Kam Selley
Kam Selley on 24 Oct 2012
Below is an example of a solution I would get. Now if I plugged the x variables (x = 1.0001 1.0000 1.4779 9.9994 1.4779) back into the problem then it gives the same answers for the components: ExpUnitProfit = 0.71676 ExpIncMarketShare = 0.0098135 UnitPrice = 1.0001 MarShareWOExtWar = 0.13953
These components are the initial optimization function where the optimization function = -(ExpUnitProfit*ExpIncMarketShare)/(UnitPrice*MarShareWOExtWar) = .0070339243/.139543953 = .0504065146
But the fval (optimal solution) given by matlab = -.0962
Not sure if this matters, but within the function for the optimization problem there is a random variable that is generated which is included within finding ExpUnitProfit. I know the program has to do many interations before it finds a solution and so the variable may change, but since I display the random variable at the end then I believe that takes care of knowing which one it uses.
I am having a bit of trouble trying to round everything in the below display to 4 decimal places. Any suggestions on a generic method?
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 6 0.131685 -1
1 12 -0.00667422 0 1 -6.92e+06 7e+06
2 27 -0.0105627 0 0.00195 -6.87e+05 1.36e+06
3 33 -0.0886607 0 1 -3.17e+05 1.15e+07
4 47 -0.0943893 0 0.00391 -4.17e+06 1.77e+07 Hessian modified
5 64 -0.0954622 0 0.000488 -4.14e+06 1.53e+08
6 78 -0.0961024 0 0.00391 -1.11e+08 1.54e+08 Hessian modified
7 85 -0.0961913 0 0.5 -8.66e+06 1.86e+07
8 124 -0.096216 -4.441e-16 1.16e-10 -9.2e+06 6.36e+06 Hessian modified
Local minimum possible. Constraints satisfied.
fmincon stopped because the predicted change in the objective function is less than the default value of the function tolerance and constraints are satisfied to within the default value of the constraint tolerance.
Active inequalities (to within options.TolCon = 1e-06): lower upper ineqlin ineqnonlin 3 9
fval =
-0.0962
x =
1.0001 1.0000 1.4779 9.9994 1.4779
P =
1.0001
W =
1.0000
U =
1.4779
Wprime =
9.9994
Uprime =
1.4779
ExpUnitProfit = 0.71676 ExpIncMarketShare = 0.0098135 UnitPrice = 1.0001 MarShareWOExtWar = 0.13953 r = -2.8326 Theta0 = 0.005 Theta0 = 0.01 Theta0 = 0.01 Theta0 = 0.02 L = 10 D = 0.11684 Qprime = 0.1886 e = 2 v = 0.02
ans =
x: [9x5 double]
fval: [9x1 double]
  2 Comments
Matt J
Matt J on 29 Oct 2012
Edited: Matt J on 29 Oct 2012
I can't see how it makes sense to try to minimize a function that is randomly changing. It's also unclear to me where/how you display the random variable "at the end".
In any case, the expression you gave for the objective function
(ExpUnitProfit*ExpIncMarketShare)/(UnitPrice*MarShareWOExtWar)
looks like it should always be positive, so the question is how the actual objective function that you're feeding to fmincon is different from the above. You should use DBSTOP or set breakpoints to trigger a stop in the code whenever fval<0. Then you can examine what your objective function is doing wrong.
Kam Selley
Kam Selley on 29 Oct 2012
Hi Matt,
Actually I am trying to maximize this function so the actual optimization problem is changed to a negative by multiplying it by negative 1:
-1[(ExpUnitProfit*ExpIncMarketShare)/(UnitPrice*MarShareWOExtWar)]
Therefore, I do expect the fval to come out negative. Do you believe the problem could be on how I made the maximization function into a perceived minimization function? In addition to adding the negative one, I also reversed my constraint to compensate for it. I was thinking this was the correct procedure to use since I was really trying to maximize the function and matlab on provides minimization options.
I displayed the random variable at the end by adding a numstr2 with the random variable within the function in which it would display it at the end with the optimization value.
I can tell that the random variable is correct because when I go and plug the optimal X-values and the random variable back into the program, I get the same values for each component ( ExpUnitProfit; ExpIncMarketShare; UnitPrice; MarShareWOExtWar) that the optimal values gave in the optimization run. This is why I am trying to understand if there is something that fmincon may specifically do additionally that makes the optimal value slightly different. I find it hard to believe that the rounding would be that much off from the original answer but this could be the case as was suggested earlier. Only thing is I am trying to figure out how to actually round everything to 4 decimal places.
Kam

Sign in to comment.


Kam Selley
Kam Selley on 31 Oct 2012
I figured out the problem! Had to eliminate my random variable within one of the integrals. Now the answer is verified. Thanks for bringing that to light!

Community Treasure Hunt

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

Start Hunting!