# source:branches/devel-peter/Ipopt/contrib/MatlabInterface/examples/examplehs038.m@1338

Last change on this file since 1338 was 1338, checked in by pcarbo, 5 years ago

Changed some of the examples to reflect the changes to the MATLAB interface.

File size: 2.6 KB
Line
1% Test the "ipopt" Matlab interface on the Hock & Schittkowski test problem
2% #38. See: Willi Hock and Klaus Schittkowski. (1981) Test Examples for
3% Nonlinear Programming Codes. Lecture Notes in Economics and Mathematical
4% Systems Vol. 187, Springer-Verlag.
5%
7% This code is published under the Common Public License.
8%
9% Author: Peter Carbonetto
10%         Dept. of Computer Science
11%         University of British Columbia
12%         May 19, 2007
13function [x, info] = examplehs038
14
15  x0         = [-3  -1  -3  -1];   % The starting point.
16  options.lb = [-10 -10 -10 -10];  % Lower bound on the variables.
17  options.ub = [+10 +10 +10 +10];  % Upper bound on the variables.
18
19  % The callback functions.
20  funcs.objective        = @computeObjective;
22  funcs.hessian          = @computeHessian;
23  funcs.hessianstructure = @getHessianStructure;
24  funcs.iterfunc         = @callback;
25
26  % Set the IPOPT options.
28  options.ipopt.print_level = 0;
29  options.ipopt.tol         = 1e-7;
30  options.ipopt.max_iter    = 100;
31
32  % Run IPOPT.
33  [x info] = ipopt(x0,funcs,options);
34
35% ----------------------------------------------------------------------
36function f = computeObjective (x)
37
38  x1 = x(1);
39  x2 = x(2);
40  x3 = x(3);
41  x4 = x(4);
42
43  f = 100*(x2-x1^2)^2 + (1-x1)^2 + 90*(x4-x3^2)^2 + (1-x3)^2 + ...
44      10.1*(x2-1)^2 + 10.1*(x4-1)^2 + 19.8*(x2-1)*(x4-1);
45
46% ----------------------------------------------------------------------
48
49  x1 = x(1);
50  x2 = x(2);
51  x3 = x(3);
52  x4 = x(4);
53
54  g(1) = -400*x1*(x2-x1^2) - 2*(1-x1);
55  g(2) = 200*(x2-x1^2) + 20.2*(x2-1) + 19.8*(x4-1);
56  g(3) = -360*x3*(x4-x3^2) -2*(1-x3);
57  g(4) = 180*(x4-x3^2) + 20.2*(x4-1) + 19.8*(x2-1);
58
59% ----------------------------------------------------------------------
60function H = getHessianStructure()
61  H = sparse([ 1  0  0  0
62               1  1  0  0
63               0  0  1  0
64               0  1  1  1 ]);
65
66% ----------------------------------------------------------------------
67function H = computeHessian (x, sigma, lambda)
68
69  x1 = x(1);
70  x2 = x(2);
71  x3 = x(3);
72  x4 = x(4);
73
74  H = sigma*[ 1200*x1^2 - 400*x2 + 2  0      0                       0
75              -400*x1                 220.2  0                       0
76              0                       0      1080*x3^2 - 360*x4 + 2  0
77              0                       19.8   -360*x3                 200.2];
78  H = sparse(H);
79
80% ----------------------------------------------------------------------
81function b = callback (t, f, x)
82  fprintf('%3d  %0.3g \n',t,f);
83  b = true;
Note: See TracBrowser for help on using the repository browser.