Sunteți pe pagina 1din 17

# ANEXA

Programele Matlab
pentru metodele prezentate

## A. Metode de minimizare pentru funcii de o singur variabil

1. Metoda seciunii de aur
gold.m
function [x,l]=gold(F,x0,t1,p1,d,s)
alfa=(sqrt(5)-1)/2;
lamb(1,1)=t1+(1-alfa)*(p1-t1);
miu(1,1)=t1+alfa*(p1-t1);
e(1,1)=feval(F,x0+lamb(1,1)*d);
r(1,1)=feval(F,x0+miu(1,1)*d);
a(1,1)=t1; b(1,1)=p1; k=1;
while b(1,k)-a(1,k)>s;
if feval(F,x0+lamb(1,k)*d)>feval(F,x0+miu(1,k)*d);
a(1,k+1)=lamb(1,k);
b(1,k+1)=b(1,k);
lamb(1,k+1)=miu(1,k);
miu(1,k+1)=a(1,k+1)+alfa*(b(1,k+1)-a(1,k+1));
e(1,k+1)=r(1,k);
r(1,k+1)=feval(F,x0+miu(1,k+1)*d);
else
a(1,k+1)=a(1,k);
b(1,k+1)=miu(1,k);
miu(1,k+1)=lamb(1,k);
lamb(1,k+1)=a(1,k+1)+(1-alfa)*(b(1,k+1)-a(1,k+1));
e(1,k+1)=feval(F,x0+lamb(1,k+1)*d);
r(1,k+1)=e(1,k);
end
k=k+1;
end
[c,t]=size(a);
x=x0+((a(1,t)+b(1,t))/2)*d;
l=(a(1,t)+b(1,t))/2;
incer.m
function [a,b]=incer(F,x0,d)
% subrutina ce permite stabilirea unui interval de
% incertitudine pentru functia F='fun' pentru o initializare
% x0 si o directie d impusa.
137

x=x0;
[n,g]=size(d);
if x~=0;
l=(min(x))/100;
else
l=0.001;
end
y=x+l*d;
if (feval(F,x))>(feval(F,y));
y=x;
x=x+l*d;
else
z=x+(x-y);
end
z=x+(x-y);
while (feval(F,z))<(feval(F,x));
x=z;
z=x+(x-y);
end
I=find(d); i=min(I);
a1=(y(i,1)-x0(i,1))/(d(i,1));
b1=(z(i,1)-x0(i,1))/d(i,1);
a=min(a1,b1); b=max(a1,b1);
gold1.m
function [x]=gold1(F,x0,d,s)
[a1,b1]=incer(F,x0,d);
alfa=(sqrt(5)-1)/2;
lamb(1,1)=a1+(1-alfa)*(b1-a1);
miu(1,1)=a1+alfa*(b1-a1);
e(1,1)=feval(F,x0+lamb(1,1)*d);
r(1,1)=feval(F,x0+miu(1,1)*d);
a(1,1)=a1; b(1,1)=b1; k=1;
while b(1,k)-a(1,k)>s;
if feval(F,x0+lamb(1,k)*d)>feval(F,x0+miu(1,k)*d);
a(1,k+1)=lamb(1,k);
b(1,k+1)=b(1,k);
lamb(1,k+1)=miu(1,k);
miu(1,k+1)=a(1,k+1)+alfa*(b(1,k+1)-a(1,k+1));
e(1,k+1)=r(1,k);
r(1,k+1)=feval(F,x0+miu(1,k+1)*d);
else
a(1,k+1)=a(1,k);
b(1,k+1)=miu(1,k);
miu(1,k+1)=lamb(1,k);
lamb(1,k+1)=a(1,k+1)+(1-alfa)*(b(1,k+1)-a(1,k+1));
e(1,k+1)=feval(F,x0+lamb(1,k+1)*d);
r(1,k+1)=e(1,k);
end
138

k=k+1;
end
[c,t]=size(a);
x=x0+((a(1,t)+b(1,t))/2)*d;
gold2.m
function [x,l]=gold1(F,x0,d,s)
[a1,b1]=incer(F,x0,d);
alfa=(sqrt(5)-1)/2;
lamb(1,1)=a1+(1-alfa)*(b1-a1);
miu(1,1)=a1+alfa*(b1-a1);
e(1,1)=feval(F,x0+lamb(1,1)*d);
r(1,1)=feval(F,x0+miu(1,1)*d);
a(1,1)=a1;
b(1,1)=b1;
k=1;
while b(1,k)-a(1,k)>s;
if feval(F,x0+lamb(1,k)*d)>feval(F,x0+miu(1,k)*d);
a(1,k+1)=lamb(1,k);
b(1,k+1)=b(1,k);
lamb(1,k+1)=miu(1,k);
miu(1,k+1)=a(1,k+1)+alfa*(b(1,k+1)-a(1,k+1));
e(1,k+1)=r(1,k);
r(1,k+1)=feval(F,x0+miu(1,k+1)*d);
else
a(1,k+1)=a(1,k);
b(1,k+1)=miu(1,k);
miu(1,k+1)=lamb(1,k);
lamb(1,k+1)=a(1,k+1)+(1-alfa)*(b(1,k+1)-a(1,k+1));
e(1,k+1)=feval(F,x0+lamb(1,k+1)*d);
r(1,k+1)=e(1,k);
end
k=k+1;
end
[c,t]=size(a);
x=x0+((a(1,t)+b(1,t))/2)*d;
l=(a(1,t)+b(1,t))/2;

## 2. Metoda cutrii dihotomice

dihotomic.m
function [min,af,bf] = dihotomic(teta,a1,b1,z,e)
% z este lungimea maxima a intervalului de incertitudine
% finala e este nivelul de discernabilitate a valorilor
% functiei (ales 0.001)
k =1;
a(1,1) = a1;
b(1,1) = b1;
139

## while b(k,1) - a(k,1) > z

l(k,1) = (a(k,1)+b(k,1))/2 - e;
m(k,1) = (a(k,1)+b(k,1))/2 + e;
if feval(teta,l(k,1)) <= feval(teta,m(k,1)),
a(k+1,1) = a(k,1);
b(k+1,1) = m(k,1);
else
a(k+1,1) = l(k,1);
b(k+1,1) = b(k,1);
end
k = k+1;
end
min = (a(k)+b(k))/2;
af = a(k,1);
bf = b(k,1);

4. Metoda Fibonacci
fibonacci.m
function [af,bf,x_min] = fibonacci(teta,a1,b1,e,e1)
a(1,1) = a1;
b(1,1) = b1;
F(1) = 1; F(2) = 1;
p = 1;
while (b1-a1)/F(p) > e
F(p+2) = F(p+1) + F(p);
p = p + 1;
end
n = p;
l(1,1) = a1+(F(n-1)/F(n+1))*(b(1,1)-a(1,1));
m(1,1) = a(1,1)+(F(n)/F(n+1))*(b(1,1)-a(1,1));
k = 1;
teta1(1,1) = feval(teta,l(1,1));
teta2(1,1) = feval(teta,m(1,1));
while k <= n-2,
if teta1(k,1) > teta2(k,1),
a(k+1,1) = l(k,1);
b(k+1,1) = b(k,1);
l(k+1,1) = m(k,1);
m(k+1,1) = a(k+1,1)+(F(n-k)/F(n-k+1))*(b(k+1,1)-a(k+1,1));
teta1(k+1,1) = teta2(k,1);
teta2(k+1,1) = feval(teta,m(k+1,1));
else
a(k+1,1) = a(k,1);
b(k+1,1) = m(k,1);
l(k+1,1) = a(k+1,1)+(F(n-k-1)/F(n-k+1))*(b(k+1,1)-a(k+1,1));
m(k+1,1) = l(k,1);
teta1(k+1,1) = feval(teta,l(k+1,1));
teta2(k+1,1) = teta1(k,1);
end
k = k + 1;
end
140

l(n,1) = l(n-1,1);
m(n,1) = l(n,1) + e1;
teta1(n,1) = feval(teta,l(n,1));
teta2(n,1) = feval(teta,m(n,1));
if teta1(n,1) > teta2(n,1),
a(n,1) = l(n,1);
b(n,1) = b(n-1,1);
else
a(n,1) = a(n-1,1);
b(n,1) = l(n,1);
end
af = a(n,1);
bf = b(n,1);
x_min = (af + bf)/2;

derivl.m

141

## B. Metode de minimizare pentru funcii de mai multe variabile

1. Cutarea ciclic dup axele de coordonate
coord.m
function [y,A] = coord(F,x0,epsilon)
n = length(x0);
D=eye(n);
x(:,1)=x0;
y(:,1)=x0;
for k=1:n,
x(:,k+1)=gold1(F,x(:,k),D(:,k),0.0001);
end
A=x;
y(:,2)=x(:,n+1);
i=1;
x(:,1)=y(:,2);
while norm(y(:,i+1)-y(:,i)) > epsilon
for k=1:n,
x(:,k+1)=gold1(F,x(:,k),D(:,k),0.0001);
end
A=[A x(:,2:n+1)]
i=i+1;
y(:,i+1)=x(:,n+1);
x(:,1)=x(:,n+1);
end
y=y(:,i+1)

## 2. Cutarea ciclic dup axele de coordonate cu pas accelerat

(Hooke i Jeeves)
coord_acc.m
function [y,A] = coord_acc(F,x0,e)
%etapa de initializare[y,A] = coord(F,x0,epsilon)
n = length(x0);
D = eye(n);
x(:,1) = x0;
y(:,1) = x0;
for k=1:n
x(:,k+1) = gold1(F,x(:,k),D(:,k),0.00001);
end
A=x;
y(:,2) = x(:,n+1);
i = 1;
x(:,1) = y(:,2);

142

%etapa de baza
while norm(y(:,i+1) - y(:,i),inf) > e
for k=1:n,
x(:,k+1)=gold1(F,x(:,k),D(:,k),0.00001);
end
A=[A x(:,2:n+1)];
i = i + 1;
d = x(:,n+1) - x(:,1);
if norm(d,inf) >e
d=d;
else
break
end
z = gold1(F,x(:,n+1),d,0.00001);
A=[A z];
y(:,i+1) = z;
x(:,1) = z;
end
[p,q]=size(A);
y=A(:,q);

4. Metoda Rosenbrock
rosenbrock.m
function [M,y,x] = rosenbrock(F,x0,e)
%etapa de initializare
n = length(x0);
D = eye(n);
x(:,1) = x0;
y(:,1) = x0;
for k=1:n,
[y(:,k+1),l] = gold2(F,y(:,k),D(:,k),0.00001);
L(k,1) = l;
end
M = y;
x(:,2) = y(:,n+1);
y(:,1) = y(:,n+1);
i = 1;
%etapa de baza
while norm(x(:,i+1) - x(:,i),inf) > e,
for k=1:n,
[y(:,k+1),l] = gold2(F,y(:,k),D(:,k),0.00001);
L(k,1) = l;
end
M = [M,y(:,2:n+1)];
A = y;
for j=1:n,
if L(j,1) == 0
a(:,j) = D(:,j);
143

else
a(:,j) = A(:,n+1) - A(:,j);
end
end
for j=1:n,
if j == 1
b(:,j) = a(:,j);
else
r = 1;
while r<j,
b(:,j) = a(:,j) - (a(:,j)'*D(:,r))*D(:,r);
r = r + 1;
end
end
D(:,j) = b(:,j)/norm(b(:,j));
end
i = i + 1;
x(:,i+1) = y(:,n+1);
y(:,1) = y(:,n+1);
end
[r,s] = size(y);
y = y(:,s);

## 5. Program de evaluare a gradientului unei funcii de mai multe variabile

% Subrutina de evaluare a gradientului functiei F in
% punctul x0.
n=length(x0);
H=h*eye(n);
xold=x0;
a=feval(F,xold);
for k=1:n;
xnou=xold+H(:,k);
b=feval(F,xnou);
m(k,1)=(b-a)/h;
end
f=m;

## 6. Metode de tip gradient

x(:,1)=x0;
k=1;
A=x0;
d(:,1)=-feval(G,x(:,k));
D(:,k)=d;
144

while norm(D(:,k),inf)>e
x(:,k+1)=gold1(F,x(:,k),D(:,k),0.01*e);
x(:,k+1);
A=[A,x(:,k+1)];
D(:,k+1)=-feval(G,x(:,k+1));
k=k+1;
end
x=x(:,k);
function [f]=nabla(F,x0,h)
n=length(x0);
H=h*eye(n);
xold=x0;
a=feval(F,xold);
for k=1:n
xnou=xold+H(:,k);b=feval(F,xnou);
m(k,1)=(b-a)/h;
end
f=m;
k=1;
x(:,1)=x0;
A=x0;
d=-nabla(F,x(:,1),h);
D(:,k)=d;
while norm(D(:,k),inf)>e
x(:,k+1)=gold1(F,x(:,k),D(:,k),0.1*e);
A=[A,x(:,k+1)];
D(:,k+1)=-nabla(F,x(:,k+1),h);
k=k+1;
end
x=x(:,k);

7. Metoda Newton
newton.m
function [x,fval,g,nfe,nge,nhe,xs]=...
newton(func,dfunc,ddfunc,params,x,c1,rho,tol,itmax,trace)
nfe=0; nge=0; nhe=0;
if nargin<=9
trace=0
end
xs=x;
g=feval(dfunc,x);
nge=nge+1;
145

[m,n]=size(g);
if m<n
g=g';
end
for k=1:itmax
if norm(g)<tol
return
end
H=feval(ddfunc,x);
nhe=nhe+1;
p=-H\g;
slope=g'*p;
if slope>=0
p=-g;
if trace~=0
fprintf('newton:utilizeaza directia descendenta\n');
end
end
alpha=1;
fval0=feval(func,x);
fval1=feval(func,x+alpha*p);
nfe=nfe+2;
while fval1>fval0+c1*alpha*slope
alpha=rho*alpha;
fval1=feval(func,x+alpha*p);
nfe=nfe+1;
end
x=x+alpha*p;
fval=fval1;
if trace~=0
fprintf('newton:pas cu alfa=%g\n',alpha);
end
g=feval(dfunc,x);
nge=nge+1;
[m,n]=size(g);
if m<n
g=g';
end
if(trace~=0)
xs=[xs,x];
end
end

8. Metoda Fletcher-Reeves
function [y,M]=FReeves1(F,G,x0,e)
n=length(x0);
x(:,1)=x0;
146

y(:,1)=x0;
M=y(:,1);
d(:,1)=-feval(G,y(:,1));
k=1;
j=1;
while norm(feval(G,y(:,j)))>e;
y(:,j+1)=gold1(F,y(:,j),d(:,j),0.0001);
M=[M y(:,j+1)];
if j<n;
a(j)=(norm(feval(G,y(:,j+1)))^2)/(norm(feval(G,y(:,j)))^2);
d(:,j+1)=-feval(G,y(:,j+1))+a(j)*d(:,j);
j=j+1;
else
y(:,1)=y(:,n+1);
x(:,k+1)=y(:,n+1);
d(:,1)=-feval(G,y(:,j));
j=1;
k=k+1;
end
end
[r,s]=size(M);
y=M(:,s);
function [y,M]=FReeves2(F,x0,e)
n=length(x0);
x(:,1)=x0;
y(:,1)=x0;
M=y(:,1);
d(:,1)=-nabla(F,y(:,1),0.0001);
k=1;
j=1;
while norm(nabla(F,y(:,j),0.0001))>e;
y(:,j+1)=gold1(F,y(:,j),d(:,j),0.00001);
M=[M y(:,j+1)];
if j<n,
a(j)=(norm(nabla(F,y(:,j+1),0.0001)))^2/(norm(nabla(F,y(:,j),
0.0001)))^2;
d(:,j+1)=-nabla(F,y(:,j+1),0.0001)+a(j)*d(:,j);
j=j+1;
else
y(:,1)=y(:,n+1);
x(:,k+1)=y(:,n+1);
d(:,1)=-nabla(F,y(:,j),0.0001);
j=1;
k=k+1;
end
147

end
[r,s]=size(M);
y=M(:,s);

8. Metoda Davidon-Fletcher-Powell
function [x,N] = dfp(F,G,x0,e)
n = length(x0);
D = eye(n);
y(:,1) = x0;
x(:,1) = x0;
M = x(:,1);
N = y(:,1);
k = 1;
j = 1;
while norm(feval(G,y(:,j)))>e
d(:,j) = -D*feval(G,y(:,j));
[y(:,j+1),l(:,j)] = gold2(F,y(:,j),d(:,j),0.0001);
N = [N y(:,j+1)]
if j < n,
p(:,j) = l(:,j)*d(:,j);
q(:,j) = feval(G,y(:,j+1)) - feval(G,y(:,j));
D = D + (p(:,j)*p(:,j)')/(p(:,j)'*q(:,j))-...
(D*q(:,j)*q(:,j)'*D)/(q(:,j)'*D*q(:,j));
j = j + 1;
else
y(:,1) = y(:,n+1);
x(:,k+1) = y(:,n+1);
M = [M x(:,k+1)];
k = k + 1;
j = 1;
end
D
end
[r,s] = size(N);
x = N(:,s);
function [x,N] = dfp1(F,x0,e)
%Algoritmul DFP - gradient determinat on-line
n = length(x0);
D = eye(n);
y(:,1) = x0;
x(:,1) = x0;
M = x(:,1);
N = y(:,1);
k = 1;
148

j = 1;
while norm(nabla(F,y(:,j),0.0001))>e,
d(:,j) = -D*nabla(F,y(:,j),0.0001);
[y(:,j+1),l(:,j)] = gold2(F,y(:,j),d(:,j),0.001);
N = [N y(:,j+1)];
if j < n,
p(:,j) = l(:,j)*d(:,j);
q(:,j) = nabla(F,y(:,j+1),0.0001) nabla(F,y(:,j),0.0001);
D = D + (p(:,j)*p(:,j)')/(p(:,j)'*q(:,j)) - ...
(D*q(:,j)*q(:,j)')/(q(:,j)'*D*q(:,j));
j = j + 1;
else
y(:,1) = y(:,n+1);
x(:,k+1) = y(:,n+1);
M = [M x(:,k+1)];
k = k + 1;
j = 1;
end
D = eye(n);
end
[r,s] = size(N);
x = N(:,s);

## 9. Metoda direciilor admisibile de coborre

dac.m
function [x]=dac(F,G,x0,A,b)
x(:,1)=x0;
n=length(x);
k=1;
r=1;
while r<100;
Z=A*x(:,k)-b;
Z=(1/1000)*round(1000*Z);L=Z==0;
K=Z<0;
A1=A(L,:);
A2=A(K,:);
b1=b(L,:);
b2=b(K,:);
p=feval(G,x(:,k));
[c,v]=size(A1);
Be=[A1;eye(n);-eye(n)];
be=[zeros(c,1);ones(n,1);ones(n,1)];
d(:,k)=linprog(p,Be,be);
q=(feval(G,x(:,k)))'*d(:,k);
if norm(q)<0.0001;
break
end
149

if d(:,k)<=0
lm=NaN;
else
P=b2-A2*x(:,k);
M=A2*d(:,k);
Q=M>0;
P1=P(Q,:);
M1=M(Q,:);
lm=min(P1./M1);
end
[x(:,k+1),l]=gold(F,x(:,k),0,lm,d(:,k),0.00001);
r=r+1;
k=k+1;
end

rosen.m
function [A,x]=rosen(F,x0,A,b,E,e)
n=length(x0);
x(:,1)=x0
Z=(1/10000)*round(10000*(A*x(:,1)-b));
L=Z==0
K=Z<0
A1=A(L,:)
A2=A(K,:)
b1=b(L,:)
b2=b(K,:)
k=1;
j=1;
while j<10
M=[A1;E]
if isempty(M)==1
P=eye(n)
else
P=eye(n)-M'*inv(M*M')*M
end
d(:,k)=-P*nabla(F,x(:,k),0.0001)
if any(d(:,k))~=0;
lm=NaN
else
T=b2-A2*x(:,k);
N=A2*d(:,k);
Q=N>0;
T1=T(Q,:);
N1=N(Q,:);
lm=min(T1./N1)
end
[x(:,k+1),q]=gold(F,x(:,k),0,lm,d(:,k),0.0001);
150

m(:,k)=x(:,k+1)-x(:,k);
if norm(d(:,k))<0.001&norm(m(1,k))<0.001)
break
else
Z=(1/10000)*round(10000*(A*x(:,k+1)-b));
L=Z==0;
K=Z<0;
A1=A(L,:);
[c,v]=size(A1);
A2=A(K,:);
b1=b(L,:);
b2=b(K,:);
k=k+1;
end
if isempty(M)==1;
break
else
w=-inv(M*M')*M*nabla(F,x(:,k),0.001);
c
[c,v]=size(A1);
u=w(1:c,:);
if u>=0;
break
else
Y=find(u<0);
e3=max(Y);
Y=find(u>(u(e3,:)));
A1=A1(Y,:);
end
end
end
j=j+1;
end
A=x;
[t,o]=size(A);
x=x(:,o);

C. Algoritmi genetici
genetic.m
function [min_gen,timp] = genetic(F,a,b,Pc,Ps,NR,k,gen)
interval = b - a;
pas = interval/(2^k-1);
%generarea populatiei initiale
CR_pos = round((2^k)*rand(1,NR)); %vectorul cromozomilor
CR_real = a + pas*CR_pos; %valoarea lor reala
for i=1:NR,
M(i) = feval(F,CR_real(i));
end
151

minim = min(M);
nr_pasi = 1;

## %etapa de determinare a unei solutii prin evolutie

while (nr_pasi<=gen),
%Pasul.1. Evaluarea populatiei
for i=1:NR, V(i) = f(CR_real(i));
end
%Pasul.2. Probabilitatea cumulativa de selectie
S = 0;
for i=1:NR, S = S + V(i);
end
%Pasul.3. Probabilitatea de selectie a fiecarui cromozom
for i=1:NR, P(i) = V(i)/S;
end
%Pasul.4. Prob. cumulativa de selectie a fiecarui crom.
for i=1:NR,
Q(i) = 0;
for j=1:i, Q(i) = Q(i) + P(j);
end
CRi_cont(i) = 0;
end
%Pasul.5. Crearea unei populatii interm. de cromozomi
CRi_p = rand(1,NR); %probabilitatile uniform aleatoare
m = 1; %indicator - numar de cromozomi intermediari
for i=1:NR,
j = 1; gasit = false;
while (gasit == false && j<=NR),
if (CRi_p(i) <= Q(j)) && (CRi_cont(j) == 0),
CRi_NR(m) = j;
CRi_cont(j) = 1;
gasit = true;
m = m + 1;
end
j = j + 1;
end
end
%Pasul.6. Selectarea cromozomilor supusi imperecherii
m = m - 1; % numarul de cromozomi interm. pt. imperechere
CRim_p = rand(1,m);
j = 1;
for i=1:m,
if CRim_p(i) < Pc,
CRim_NR(j) = CRi_NR(i);
j = j + 1;
end
end
m = j - 1; %numarul de cromozomi supusi imperecherii
%Pasul 7. Incrucisarea cromozomilor supusi imperecherii
j = 1; % contorul cromozomi
if (m>1),
while (j<m), % cat timp am cromozomi de imperechere
t = round(k*rand); % generez un numar intre 0 si k
152

nou1 = dec2bin(CR_pos(CRim_NR(j)),k);
nou2 = dec2bin(CR_pos(CRim_NR(j+1)),k);
for i=t+1:k, nou1(i) = nou2(i);
end
nou = dec2bin(CR_pos(CRim_NR(j)),k);
for i=t+1:k, nou2(i) = nou(i);
end
%depunearea lor in generatia de cromozomi
CR_pos(CRim_NR(j)) = bin2dec(nou1);
CR_pos(CRim_NR(j+1)) = bin2dec(nou2);
j = j + 2;
end
end
%Pasul 8. Aplicarea unor mutatii
for i=1:NR,
for j=1:k,
t = round(k*rand); %proabilitatea fiecarei gene
if t<Ps,
nou = dec2bin(CR_pos(i),k);
if nou(k) == 1,
nou(k) = '0';
CR_pos(i) = bin2dec(nou);
else
nou(k) = '1';
CR_pos(i) = bin2dec(nou);
end
end
end
end
CR_real = a + pas*CR_pos; % evaluarea genereatiei
for i=1:NR,
M(i) = feval(F,CR_real(i));
end
if minim > min(M), minim = min(M);
end
nr_pasi = nr_pasi + 1;
end
min_gen(z) = minim;

153

## Meniu subsol

### Obține aplicațiile noastre gratuite

Drepturi de autor © 2021 Scribd Inc.