Optimization of Rosenbrock function
39 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Austin Thai
2021-4-17
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.
0 个评论
更多回答(1 个)
Matt J
2021-4-17
fun = @(x)100*(x(:,2) - x(:,1).^2).^2 + (1 - x(:,1)).^2 - 108.32;
[x,fval]=lsqnonlin(fun,[0,0],[1,1])
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!