0% found this document useful (0 votes)
36 views

Lucrare de Laborator 5: Universitatea Tehnică A Moldovei

The document is a laboratory work in Romanian on numerical optimization methods. It contains: - The purpose is to find the minimum of two functions f(x1,x2) and f(y1,y2) using gradient descent and Newton's method. - The code implements gradient descent to minimize f(x1,x2), stores the result in x[], and counts iterations in Rez[]. It then minimizes f(y1,y2) in y[] and counts in Rez[]. - Finally, it applies Newton's method to find the root of the gradient of f(x1,x2), storing the result in xh[] and counting iterations in Re

Uploaded by

Adrian Bodorin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lucrare de Laborator 5: Universitatea Tehnică A Moldovei

The document is a laboratory work in Romanian on numerical optimization methods. It contains: - The purpose is to find the minimum of two functions f(x1,x2) and f(y1,y2) using gradient descent and Newton's method. - The code implements gradient descent to minimize f(x1,x2), stores the result in x[], and counts iterations in Rez[]. It then minimizes f(y1,y2) in y[] and counts in Rez[]. - Finally, it applies Newton's method to find the root of the gradient of f(x1,x2), storing the result in xh[] and counting iterations in Re

Uploaded by

Adrian Bodorin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Universitatea Tehnică a Moldovei

Facultatea Calculatoare, Informatică si Microelectronică


Departamentul Informatică și Ingineria Sistemelor

Lucrare de laborator №5

Efectuat: st.gr. IA-171 Bodorin Adrian


Verificat: conf. Univ. dr. Moraru Vasile

Chișinău 2018
Scopul lucrării:

f(x1,x2)=2x12+2x1x2+x22-2x1-3x2

4 x 1+2 x 2−2
(
F(x1,x2)=
2 x 1 +2 x 2−3 )
x 2=2
4 x 1 +2 x 2−2=0
{
2 x1 +2 x 2−3=0
<=>
x 2=1−2 x 1
{
2 x 1 +2−4 x 1−3=0 {
<=>
x2 =1−2 x1
−2 x 1=1
<=>
{x1 =
−1
2

−1
( )
x ¿= 2
2
p. min local

Textul programului:
#include <stdlib.h>
#include <math.h>
#include <conio.h>
float
a=1,b=0.5,c=0.1,ak=1,eps1=0.0005,eps2=0.00001,x[2]={1,1},y[2]={
0.1,0.2},z[2],fd[2],v1[2]={-0.5,2},v2[2]={0,0},
H[2][2]={{4,2},{2,2}},xh[2]={0,0};
int Rez[3]={0,0,0};

float f1(float m[2])


{float t;
t=2*m[0]*m[0]+2*m[0]*m[1]+m[1]*m[1]-2*m[0]-3*m[1];
return t;}
float f2(float m[2])
{float t;
t=exp(-
(pow(m[0],2)+pow(m[1],2)))*(8*pow(m[0],2)+3*pow(m[1],2));
return t;}
void Nabla1(float m[2])
{
float Nabla11(float x,float y)
{float t;
t=4*x+2*y-2;
return t;}
float Nabla12(float x,float y)
{float t;
t=2*x+2*y-3;
return t;}
fd[0]=Nabla11(m[0],m[1]);
fd[1]=Nabla12(m[0],m[1]);}
void Nabla2(float m[2])
{
float Nabla21(float x,float y)
{float t;
t=exp(-(pow(x,2)+pow(y,2)))*((-16)*pow(x,3)-6*x*pow(y,2)+16*x);
return t;}

float Nabla22(float x,float y)


{float t;
t=exp(-(pow(x,2)+pow(y,2)))*((-6)*pow(y,3)-16*pow(x,2)*y+6*y);
return t;}
fd[0]=Nabla21(m[0],m[1]);
fd[1]=Nabla22(m[0],m[1]);}
float Norm2(float m[2])
{int i;
float t=0;
for(i=0;i<2;i++)
t+=m[i]*m[i];
t=sqrt(t);
return t;}
void Calc(int q)
{float aux[2];
int i;
if(q==1)
{Nabla1(x);
for(i=0;i<2;i++)
aux[i]=-ak*fd[i];
for(i=0;i<2;i++)
z[i]=x[i]+aux[i];}
if(q==2)
{Nabla2(y);
for(i=0;i<2;i++)
aux[i]=-ak*fd[i];
for(i=0;i<2;i++)
z[i]=y[i]+aux[i];}}

void MG(int w)
{int i;

if(w==1)
{Nabla1(x);
while(Norm2(fd)>eps1)
{while(1)
{Calc(w);
Rez[w-1]+=1;
if(f1(z)-f1(x)<=-ak*c*pow(Norm2(fd),2))
{ak=a;
for(i=0;i<2;i++)
x[i]=z[i];
break;}
else
ak=ak*b;}}}
if(w==2)
{Nabla2(y);
while(Norm2(fd)>eps2)
{while(1)
{Calc(w);
Rez[w-1]+=1;
if(f2(z)-f2(y)<=-ak*c*pow(Norm2(fd),2))
{ak=a;
for(i=0;i<2;i++)
y[i]=z[i];
break;}
else
ak=ak*b;}}}}
void Hess()
{float d[2],alfa,beta,daux[2]={0,0},aux1,aux2;
int i,j,t=0;
Nabla1(xh);
for(i=0;i<2;i++)
d[i]=-fd[i];
for(i=0;i<2;i++)
if(fd[i]==0 || fabs(0-fd[i])<0.0001) t++;
while(t!=2)
{aux1=0; aux2=0;
for(i=0;i<2;i++)
daux[i]=0;
for(i=0;i<2;i++)
aux1+=fd[i]*d[i];
for(j=0;j<2;j++)
for(i=0;i<2;i++)
daux[j]+=H[j][i]*d[i];
for(i=0;i<2;i++)
aux2+=d[i]*daux[i];
alfa=-aux1/aux2;
for(i=0;i<2;i++)
daux[i]=alfa*d[i];
for(i=0;i<2;i++)
z[i]=xh[i]+daux[i];
Rez[2]+=1;
Nabla1(z); t=0;
for(i=0;i<2;i++)
if(fd[i]==0 || fabs(0-fd[i])<0.0001) t++;
if(t==2)
{for(i=0;i<2;i++)
xh[i]=z[i];
break;}
else
{Nabla1(z);
aux1=Norm2(fd);
Nabla1(xh);
aux2=Norm2(fd);
beta=pow(aux1,2)/pow(aux2,2);
for(i=0;i<2;i++)
daux[i]=beta*d[i];
Nabla1(z);
for(i=0;i<2;i++)
d[i]=(-fd[i])+daux[i];
for(i=0;i<2;i++)
xh[i]=z[i];
t=0;
Nabla1(xh);
for(i=0;i<2;i++)
if(fd[i]==0 || fabs(0-fd[i])<0.0001) t++;}}}
void main()
{int i;
MG(1);
printf("Rezultatul:\n");
for(i=0;i<2;i++)
printf("%.5f ",x[i]);
printf("\nIteratii=%i\n\n",Rez[0]);
printf("Verificare:\n");
printf("f(x)=%.5f\n",f1(x));
printf("f(v1)=%.5f\n\n",f1(v1));
MG(2);
printf("Rezultatul:\n");
for(i=0;i<2;i++)
printf("%.5f ",y[i]);
printf("\nIteratii=%i\n\n",Rez[1]);
printf("Verificare:\n");
printf("f(y)=%.5f\n",f2(y));
printf("f(v2)=%.5f\n\n",f2(v2));
Hess();
printf("Rezultatul Hess:\n");
for(i=0;i<2;i++)
printf("%.5f ",xh[i]);
printf("\nIteratii=%i\n\n",Rez[2]);}

You might also like