summaryrefslogtreecommitdiff
path: root/demos/fminimax.dem.sce
blob: d8c462c686bde187797fd79cffa69cd13bdfdd8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
mode(1)
//
// Demo of fminimax.sci
//

// A basic case :
// we provide only the objective function and the nonlinear constraint
// function
function f = myfun(x)
f(1)= 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304;     //Objectives
f(2)= -x(1)^2 - 3*x(2)^2;
f(3)= x(1) + 3*x(2) -18;
f(4)= -x(1) - x(2);
f(5)= x(1) + x(2) - 8;
endfunction
// The initial guess
x0 = [0.1,0.1];
// The expected solution : only 4 digits are guaranteed
xopt = [4 4]
fopt = [0 -64 -2 -8 0]
maxfopt = 0
// Run fminimax
[x,fval,maxfval,exitflag,output,lambda] = fminimax(myfun, x0)
// Press ENTER to continue
halt()   // Press return to continue
 
// A case where we provide the gradient of the objective
// functions and the Jacobian matrix of the constraints.
// The objective function and its gradient
function f = myfun(x)
f(1)= 2*x(1)^2 + x(2)^2 - 48*x(1) - 40*x(2) + 304;
f(2)= -x(1)^2 - 3*x(2)^2;
f(3)= x(1) + 3*x(2) -18;
f(4)= -x(1) - x(2);
f(5)= x(1) + x(2) - 8;
endfunction
// Defining gradient of myfun
function G = myfungrad(x)
G = [ 4*x(1) - 48, -2*x(1), 1, -1, 1;
2*x(2) - 40, -6*x(2), 3, -1, 1; ]'
endfunction
// The nonlinear constraints and the Jacobian
// matrix of the constraints
function [c,ceq] = confun(x)
// Inequality constraints
c = [1.5 + x(1)*x(2) - x(1) - x(2), -x(1)*x(2) - 10]
// No nonlinear equality constraints
ceq=[]
endfunction
// Defining gradient of confungrad
function [DC,DCeq] = cgrad(x)
// DC(:,i) = gradient of the i-th constraint
// DC = [
//   Dc1/Dx1  Dc1/Dx2
//   Dc2/Dx1  Dc2/Dx2
//   ]
DC= [
x(2)-1, -x(2)
x(1)-1, -x(1)
]'
DCeq = []'
endfunction
// Test with both gradient of objective and gradient of constraints
minimaxOptions = list("GradObj",myfungrad,"GradCon",cgrad);
// The initial guess
x0 = [0,10];
// The expected solution : only 4 digits are guaranteed
xopt = [0.92791 7.93551]
fopt = [6.73443  -189.778  6.73443  -8.86342  0.86342]
maxfopt = 6.73443
// Run fminimax
[x,fval,maxfval,exitflag,output] = fminimax(myfun,x0,[],[],[],[],[],[], confun, minimaxOptions)
//========= E N D === O F === D E M O =========//