# Optimization of Rosenbrock function

조회 수: 33(최근 30일)
John 17 Apr 2021 12:28
답변: Matt J 17 Apr 2021 14:14
Hi, so I'm trying to optimize a Rosenbrock function such that the function value should be 108.32 and I need to find the value of x1 and x2.
Rosenbrock function is defined as:
f=100*(x2 - x1^2)^2 + (1 - x1)^2
according to the definition of the function x1 and x2 have a minimum values of 1 for f=0. What I need is the value of x1 and x2 so that my function is f=108.32. The code I have so far is:
rosenbrock = @(x)100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2; % Vectorized function
x = optimvar('x',1,2);
obj = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
%Create an optimization problem named prob having obj as the objective function.
prob = optimproblem('Objective',obj);
%Create the nonlinear constraint as a polynomial in the optimization variable.
cons1 = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2 ==108.32
cons2 = x(2)>=1;
cons3 = x(1)>=1 ;
prob.Constraints.circlecons1 = cons1;
prob.Constraints.circlecons2 = cons2;
prob.Constraints.circlecons3 = cons3;
show(prob)
x0.x = [0 0];
[sol,fval,exitflag,output] = solve(prob,x0)
The thing is the function isn't able to converge and this is the result I'm getting:
OptimizationProblem :
Solve for:
x
minimize :
((100 .* (x(2) - x(1).^2).^2) + (1 - x(1)).^2)
subject to circlecons1:
((100 .* (x(2) - x(1).^2).^2) + (1 - x(1)).^2) == 108.32
subject to circlecons2:
x(2) >= 1
subject to circlecons3:
x(1) >= 1
Solving problem using fmincon.
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the value of the step size tolerance but constraints are not
satisfied to within the value of the constraint tolerance.
<stopping criteria details>
sol =
struct with fields:
x: [-0.3960 -0.8745]
fval =
108.3121
exitflag =
NoFeasiblePointFound
output =
struct with fields:
iterations: 175
funcCount: 190
constrviolation: 1.8745
stepsize: 7.7904e-12
algorithm: 'interior-point'
firstorderopt: 0
cgiterations: 183
message: '↵Converged to an infeasible point.↵↵fmincon stopped because the size of the current step is less than↵the value of the step size tolerance but constraints are not↵satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization stopped because the relative changes in all elements of x are↵less than options.StepTolerance = 1.000000e-10, but the relative maximum constraint↵violation, 1.746622e-02, exceeds options.ConstraintTolerance = 1.000000e-06.↵↵'
bestfeasible: []
solver: 'fmincon'
Clearly the second and third constraints aren't being satisfied in this solution x: [-0.3960 -0.8745]. Any ideas on what I should do? BTW I already know that the solution I'm supposed to get is x: [0.6 1.4] for this particular solution, but I need to make a matlab code so I can solve for other data points.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Austin Thai 17 Apr 2021 14:11
I believe you just need to start with a better guess (non-zero) for x0.x, e.g.
x0.x = [0.1 0.1];
Then, depending on your constraints, you will get different solutions.

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Matt J 17 Apr 2021 14:14
fun = @(x)100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2 - 108.32;
[x,fval]=lsqnonlin(fun,[0,0],[1,1])
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
Local minimum possible. lsqnonlin stopped because the relative size of the current step is less than the value of the step size tolerance.
x = 1×2
1.5920 1.4953
fval = 1.3809e-16

댓글을 달려면 로그인하십시오.

R11.1

### Community Treasure Hunt

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

Start Hunting!