Advertisement
makispaiktis

Optimization - Nonlinear - fmincon

Oct 31st, 2022 (edited)
1,836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.27 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5.  
  6. %% fmincon for: min( 100*(x_2 - x_1^2)^2 + (1-x_1)^2 )
  7. display('********************************************');
  8. display('fmincon');
  9. fun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
  10. A = [1 2];
  11. b = 1;
  12. Aeq = [2 1];
  13. beq = 1;
  14. x0 = [0.5 0];                   % It must satisfy the constraints
  15. [x_opt, f_min] = fmincon(fun, x0, A, b, Aeq, beq)
  16. display('********************************************');
  17. display(' ');
  18.  
  19.  
  20. %% fmincon for: min( 100*(x_2 - x_1^2)^2 + (1-x_1)^2 )
  21. % We also insert a non-linear constraint
  22. display('********************************************');
  23. display('fmincon with non-linear constraint and options');
  24. options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
  25. lb = [];
  26. ub = [];
  27. nonlcon = @unitdisk;
  28. [x_opt, f_min] = fmincon(fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
  29. display('********************************************');
  30. display(' ');
  31.  
  32.  
  33. %% CVX for: min( 100*(x_2 - x_1^2)^2 + (1-x_1)^2 )
  34. cvx_begin quiet
  35.     variables x(2);
  36.     minimize(100*(x(2)-x(1)^2)^2 + (1-x(1))^2);
  37.     subject to
  38.         x(1) + 2*x(2) <= 1;
  39.         2*x(1) + x(2) == 1;
  40. cvx_end
  41. cvx_optval
  42.  
  43.  
  44. %% Auxiliary Function for non-linear constraints
  45. function [c,ceq] = unitdisk(x)
  46.     c = x(1)^2 + x(2)^2 - 1;
  47.     ceq = [];
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement