Advertisement
makispaiktis

Optimization - Nonlinear - Optimproblem struct and options

Nov 1st, 2022 (edited)
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.74 KB | None | 0 0
  1. clear all
  2. close all
  3. clc
  4.  
  5. %% Function and plot
  6. fcn = @(x, y) log(1 + 3*(y - (x.^3 - x)).^2 + (x-4/3).^2);
  7. fsurf(fcn, [-2.5 2.5], 'ShowContours', 'on');
  8. xlabel('x');
  9. ylabel('y');
  10. title('My function f(x, y)');
  11.  
  12.  
  13. %% Problem-based solution
  14. prob = optimproblem;
  15. x = optimvar('x', 'LowerBound', -2.5, 'UpperBound', 2.5);
  16. y = optimvar('y', 'LowerBound', -2.5, 'UpperBound', 2.5);
  17. prob.Objective =  (1 + 3*(y - (x.^3 - x)).^2 + (x-4/3).^2);
  18. initialpt.x = -1;
  19. initialpt.y = 2;
  20. options = optimoptions(prob, 'Display', 'iter');
  21. % options = optimoptions(prob, 'Display', 'iter', 'OutputFcn', @plotUpdate);
  22. [sol, fval, exitflag, output] = solve(prob, initialpt, 'Options', options)
  23. disp("Number of function evaluations = " + output.funcCount);
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement