You are on page 1of 13

Raport laborator 3

Student: Iordache Bogdan Andrei


Grupa: 433Aa
Descrierile metodelor din laborator:
Metoda cautarii directe:
Metoda de cautare pe coordonate:
Metoda Gradient Descent:
Graficele aferente:
Pentru paraboloid:

Cautare directa:

Chart Title
4

3.5

2.5

1.5

0.5

0
0 0.5 1 1.5 2

Cautare pe coordonate:

Chart Title
2.5

1.5

0.5

0
-1 -0.5 0 0.5 1 1.5 2 2.5
-0.5

-1

-1.5

Series1 Series2
Gradient Descent:

Chart Title
1.2

0.8

0.6

0.4

0.2

0
0 0.2 0.4 0.6 0.8 1 1.2

Pentru Rosenblock:

Cautare directa:

Chart Title
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
Cautare pe coordonate:

Chart Title
18
16
14
12
10
8
6
4
2
0
-0.4 -0.2 -2 0 0.2 0.4 0.6 0.8

Series1 Series2

Gradient Descent:

Chart Title
1.2

0.8

0.6

0.4

0.2

0
0 0.2 0.4 0.6 0.8 1 1.2
Codul Sursa:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

FILE *logpatternSearch;

FILE *logCoordonate;

FILE *logGradient;

void patternSearch(double f(double[]), double coef, double tol, double x[]);

void coordonate(double f(double,double), double pas, double alfa, double tol, double x[2]);

void gradient(double x0_init, double x1_init, double(*f1_deriv_x)(double,double),


double(*f1_deriv_y)(double,double), double tol, double alfa);

double func1(double x[]);

double func1(double x[]) {

return x[0] * x[0] + x[1] * x[1] - x[0] * x[1] - x[0]- x[1];

double f1_paraboloid(double x, double y);

double f1_paraboloid(double x, double y) {

return x*x + y*y - x*y - x - y;

double f1_deriv_x(double x,double y);

double f1_deriv_x(double x,double y){

return 2*x - y - 1;

double f1_deriv_y(double x,double y);

double f1_deriv_y(double x,double y){

return -x + 2*y - 1;
}

double func2(double x[]);

double func2(double x[]){

return (1 - x[0])*(1 - x[0]) + 100* (x[1] - x[0]*x[0])*(x[1] - x[0]*x[0]);

double func22(double x, double y);

double func22(double x, double y) {

return pow((1-x), 2) + 100*pow((y-x*x), 2);}

void aScale(int n, double in[], double coef, double out[]){

int i;

for (i = 0; i < n; i++)

out[i] = coef * in[i];

void aAdd(int n, double xnow[], double step[], double xnew[]) {

int i;

for (i = 0; i < n; i++)

xnew[i] = xnow[i] + step[i] ;

int main() {

logpatternSearch = fopen("logpatternSearch.csv", "w");

logCoordonate = fopen("logCoordonate.csv", "w");

logGradient = fopen("logGradient.csv","w");
if(logpatternSearch==NULL || logCoordonate==NULL || logGradient==NULL) {

printf("Could not open one of files\n");

return 1;

double init[2] = {0,0}; //rosenbrock

patternSearch(func2, 1, 0.00001, init);

//double init[2] = {2,2}; //paraboloid

//patternSearch(func2, 1, 0.00001, init);

printf("%f ", init[0]);

printf("%f\n", init[1]);

//double init1[2] = {2,0};

//coordonate(f1_paraboloid, 0.8, 0.3, 0.00001, init1);

double init1[2] = {0,0}; //rosenbrock

coordonate(func22, 0.3, 0.5, 1e-8, init1);

gradient(2,0, f1_deriv_x, f1_deriv_y, 0.00001, 0.5);

void patternSearch(double f(double[]), double coef, double tol, double x[]) {

double dir[3][2] = { {1, 0} , {-0.5, sqrt(3) / 2} , {-0.5, -sqrt(3) / 2} };

double x_next[2];

double step[2] = {1, 1};

double aux[2];

//while (coef > tol) {

int k;

for( k = 0; k<=10000; k++){


int verif = 1;

int i;

for ( i = 0; i < 3; i++) {

int j;

for ( j = 0; j < 2; j++)

aux[j] = dir[i][j];

aScale(2, aux, coef, step);

aAdd(2, x, step, x_next);

if ( f(x_next) < f(x) ) {

x[0] = x_next[0]; x[1] = x_next[1];

else

verif = 0;

if(verif == 0)

//coef/=2; //impart la 2 pentru paraboloid

coef/=1.001;

fprintf(logpatternSearch, "%f, %f\n", x[0], x[1]);

void coordonate(double f(double,double), double pas, double alfa, double tol, double x[2]){

fprintf(logCoordonate, "%f, %f, %f\n", x[0], x[1], f(x[0], x[1]));

int distanta;

double f0;

int j;

for( j=0; j<50; j++){

int i;
for( i=0; i<2; i++){

printf("%f, %f, %f\n", x[0], x[1], f(x[0],x[1]));

fprintf(logCoordonate, "%f, %f, %f\n", x[0], x[1], f(x[0],x[1]));

f0 = f(x[0],x[1]);

distanta =1;

x[i] = x[i] +pas*distanta;

if(f(x[0],x[1])>f0){

distanta = -1;

x[i] = x[i] +2*pas*distanta;

while(f0>= f(x[0],x[1])){

f0 = f(x[0],x[1]);

x[i] = x[i] + pas*distanta;

pas = pas*alfa;

if(pas < tol){

break;

void gradient(double x0_init, double x1_init, double(*f1_deriv_x)(double,double),


double(*f1_deriv_y)(double,double), double tol, double alfa) {

int nr_pasi = 0;

double x0_ant = x0_init;

double x1_ant = x1_init;

double distanta;
do {

x0_init = x0_ant - f1_deriv_x(x0_ant,x1_init)*alfa;

x1_init = x1_ant - f1_deriv_y(x0_init,x1_ant)*alfa;

distanta = sqrt((x0_init - x0_ant) * (x0_init - x0_ant) + (x1_init - x1_ant)*(x1_init -


x1_ant));

x0_ant = x0_init;

x1_ant = x1_init;

nr_pasi++;

printf("Punctele de optim gasite sunt X0 = %f, si X1 = %f\n", x0_init, x1_init);

fprintf(logGradient, "%f, %f\n", x0_init, x1_init);

} while (distanta > tol);

You might also like