#optimalizace
function cviceni8

pkg load optim

osax=linspace(0,6,100);
plot(osax,fce1(osax));
 
nrm(@fce1,3)
printf("presny vysledek: %f\n",(8+sqrt(13))/3)

#volba metody
options(6)=0; #simplex
options(10)=20; # max pocet volani funkce
fmins(@fce2,[0,0],options)

options(10)=100; # max pocet volani funkce
fmins(@fce2,[0,0],options)


[xx,yy]=meshgrid(linspace(-4,6,30));
surf(xx,yy,fce3(xx,yy))

#normalni optimalizace najde lokalni minimum
fmins(@fce4,[0.5,0.5])

#simulated annealing najde globalni minimum
#usage: [x, obj, convergence, details] = samin("f", {args}, {control})
#Arguments:
#* "f": function name (string)
#* {args}: a cell array that holds all arguments of the function,
#* {control}: a cell array with 11 elements
#        * LB  - vector of lower bounds
#        * UB - vector of upper bounds
#        * nt - integer: # of iterations between temperature reductions
#        * ns - integer: # of iterations between bounds adjustments
#        * rt - (0 < rt <1): temperature reduction factor
#        * maxevals - integer: limit on function evaluations
#        * neps - integer:  number of values final result is compared to
#        * functol -   (> 0): the required tolerance level for function value
#                           comparisons
#        * paramtol -  (> 0): the required tolerance level for parameters
#        * verbosity - scalar: 0, 1, or 2.
#                * 0 = no screen output
#                * 1 = only final results to screen
#                * 2 = summary every temperature change
#        * minarg - integer: which of function args is minimization over?

#samin
samin("fce4",{[0.5;0.5]},{[-10;-10],[10;10],10,10,0.8,20000,1,1e-3,1e-3,1,1})

break

#linearni programovani

# max x1+2*x2
# 2*x1+x2<=2
# x1-x2<=1/2
# 2*x1+10*x2=11

c=[1;2]
A=[2,1;1,-1;2,10]
b=[2;0.5;11]
ctype="UUS"
vartype="CC"
sense=-1
glpk(c,A,b,[],[],ctype,vartype,sense)




endfunction

function y=fce1(x)
y=(x-1).*(x-2).*(x-5);
endfunction

function y=fce2(x)
y=(x(1)-1).^2+(x(2)-2).^2;
endfunction

function z=fce3(x,y)
z=-exp(-x.^2-y.^2)-2*exp(-(x-3).^2-(y-3).^2);
endfunction

function y=fce4(x)
y=-exp(-x(1)^2-x(2)^2)-2*exp(-(x(1)-3)^2-(x(2)-3)^2);
endfunction


