Optimization Problem with Error using optim.prob​lemdef.Opt​imizationP​roblem/sol​ve

Hi, I'm currrently working on creating an optimization problem that finds the maximum of an equation that has 3 constraints. My code is:
prob = optimproblem('ObjectiveSense','max');
x=optimvar('x',2,1);
prob.Objective = 1+(x(1))^2 * ((x(2)-1)^3) * (exp(-x(1)-x(2)));
cons1 = x(2) >= log(x(1));
cons2 = x(1) + x(2) <= 6;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
show(prob);
sol=solve(prob);
sol.x;
I keep getting the same errors:
Error using optim.problemdef.OptimizationProblem/solve
SOLVE requires a non-empty initial point structure to solve a nonlinear problem.
Please let me know if you have any advice. Thanks!

 Accepted Answer

Since your problem only has two unknowns, you can do a surface plot of the objective to find a decent initial guess.

4 Comments

Thanks so much for the answer. How would you go about creating a surface plot to find a good initial guess? How would I be able to add the constraints into the surface plot?
[X,Y] = meshgrid(0:0.01:6,0:0.01:6);
Z = 1+(x)^2 * ((y-1)^3) * (exp(-x-y));
surf(X,Y,Z)
[X,Y] = meshgrid(0:0.005:6,0:0.005:6);
Z = 1+(X).^2 .* ((Y-1).^3) .* (exp(-X-Y));
Z(Y<log(X))=nan;
Z(X+Y>6)=nan;
[zmax,i]=max(Z(:));
sol0.x=[X(i),Y(i)], zmax %Initial guess
sol0 = struct with fields:
x: [2 4]
zmax = 1.2677
h=surf(X,Y,Z,'EdgeColor','none','FaceAlpha',0.5);
xlabel 'X'; ylabel 'Y';
prob = optimproblem('ObjectiveSense','max');
x=optimvar('x',2,1,'LowerBound',0,'UpperBound',6);
prob.Objective = 1+(x(1))^2 * ((x(2)-1)^3) * (exp(-x(1)-x(2)));
cons1 = x(2) >= log(x(1));
cons2 = x(1) + x(2) <= 6;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
[sol,zopt]=solve(prob,sol0);
Solving problem using fmincon. Initial point is a local minimum that satisfies the constraints. Optimization completed because at the initial point, the objective function is non-decreasing in feasible directions to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
sol.x, zopt
ans = 2×1
2 4
zopt = 1.2677
Thank you so much. That makes a lot more sense. I really appreciate all the help!
You're quite welcome, but please Accept-click the answer to indicate that it resolved your question.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 1 Feb 2021

Commented:

on 1 Feb 2021

Community Treasure Hunt

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

Start Hunting!