Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторная работа №9.doc
Скачиваний:
11
Добавлен:
01.05.2014
Размер:
138.75 Кб
Скачать

6)Перечислить критерии окончания поиска, используемые в методах нулевого порядка.

В методах нулевого порядка используются следующие критерии окончания поиска:

1) Число итераций = n

2)||grad(y)|| <e

3)

Текст программы

#include <iostream.h>

#include <conio.h>

#include <stdio.h>

#include <windows.h>

#include <math.h>

#include "Matrix.h"

int iCount;

int iFunction;

int iIteration=8;

double alfa[3]={0,0,0};

double f(vec l)

{

switch(iFunction)

{

case 1: return (l.x[0]-2)*(l.x[0]-2)*(l.x[0]-2)*(l.x[0]-2)+(l.x[0]-2*l.x[0])*(l.x[0]-2*l.x[0]) ; break;

case 2: return (1.5-l.x[0]*(1-l.x[1]))*(1.5-l.x[0]*(1-l.x[1]))+(2.25-l.x[0]*(1-l.x[1]*l.x[1]))*(2.25-l.x[0]*(1-l.x[1]*l.x[1]))+(2.625-l.x[0]*(1-l.x[1]*l.x[1]*l.x[1]))*(2.625-l.x[0]*(1-l.x[1]*l.x[1]*l.x[1])); break;

}

return 0;

}

///////////////////////////////////////////////////////////////

vec grad(vec x)

{

vec q;

matrix E;

int i;

double h=0.0000001;

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

E.M[i].x[i]=1;

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

q.x[i]=(f(x+E.M[i]*h)-f(x-E.M[i]*h))/(2*h);

return q;

}

/////////////////////////////////////////////////////

double mod(vec x)

{

int i;

double m=0;

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

m+=pow(x.x[i],2);

return sqrt(m);

}

///////////////////////////////////////////////////////////////////////

matrix multipl(vec x,vec y)

{

matrix A(iCount);

for(int i=0;i<iCount;i++)

for(int j=0;j<5;j++)

A.M[i].x[j]=x.x[i]*y.x[j];

return A;

}

vec multiplvec(vec x, vec y)

{

vec res(iCount);

for (int i=0; i<iCount; i++)

res.x[i]=x.x[i]*y.x[i];

return res;

}

////////////////////////////////////////

double diff(vec x, vec p)

{

double h=0.001;

return((f(x+p*h)-f(x))/h);

}

/////////////////////////////////////////////////////////////////

void Svenn4(vec x,vec p,double alpha)

{

vec x1;

if(diff(x,p)>0) alpha=-alpha;

x1=x;

al[1]=0;

al[2]=0;

al[0]=0;

do

{

al[0]+=alpha;

x1=x1+p*alpha;

alpha*=2;

}

while(diff(x,p)*diff(x1,p)>0);

al[2]=alpha/2;

al[0]-=alpha/2;

}

/////////////////////////////////////////////////////////////////////////////

double Gold Section1 (vec x, vec p, double e)

{

double x1,x2;

int k=1;

al[1]=al[0]+al[2];

x1=al[0]+0.618*fabs(-al[0]+al[1]);

x2=al[0]+0.382*fabs(-al[0]+al[1]);

while(fabs(al[1]-al[0])>e)

{

if(f(x+p*x1)>f(x+p*x2))

{

al[1]=x1;

x1=x2;

x2=al[0]+0.382*fabs(al[1]-al[0]);

}

else

{

al[0]=x2;

x2=x1;

x1=al[0]+0.618*fabs(al[1]-al[0]);

}

k++;

}

return ((al[0]+al[1])/2);

}

/////////////////////////////////////////////////////////////////////////////

matrix Zangvill(matrix P, vec A){

vec a(iCount);

vec b(iCount);

matrix Pnew(iCount);

Pnew = P;

int i,j,k;

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

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

a.x[k]=0;

b.x[k]=0;

}

if (A.x[i]==0) a = Pnew.M[i];

else {

for(j=i;j<iCount;j++)

a=a+Pnew.M[j]*A.x[j];

}

if (i==0) b = a;

else{

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

b=b+ Pnew.M[j]*(a*Pnew.M[j]);

b=a-b;

}

b=b/mod(b);

Pnew.M[i]=b;

}

return Pnew;

}

vec Bolcan(vec X, vec p1,double &a){

vec p(iCount);

p=p1;

vec X2(iCount),X3(iCount);

double eps=0.001;

vec B(iCount);

B=X;

double h=0.1;

if (dif(X,p)>0){

p=p*(-1);

}

X2=X+p*h;

while (f(X2)<f(X)){

X=X2;

X2=X+p*h;

};

X3=X-p*h;

while(fabs(dif(X,p))>eps){

X=(X2+X3)*0.5;

if (dif(X,p)>0)

X2=X;

else

X3=X;

}

a = (X.x[1]-B.x[1])/p.x[1];

return X;

}

vec minim (vec x, double e){

matrix P(iCount);

double a;

int i,j;

P.M[i] = grad(x);

vec xk(iCount);

vec A(iCount);

int k=0;

do{

x=xk;

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

Svenn4(xk,P.M[i],0.1);

a=gs1(xk,P.M[i],0.001);

A.x[i] = a;

xk=xk+P.M[i]*a;

}

P = Zangvill(P,A);

k++;

cout<<"( "<<xk.x[0]<<"; "<<xk.x[1]<<"; "<<xk.x[2]<<" )"<<endl;

}while( (mod(xk-x)>e) && */k<iIteration);

cout<<"Number of iteration: "<<k<<endl;

return xk;

}

void Choise()

{

int i=0;

cout<<"Input number of function: ";

cin>>iFunction;

iCount=2 ;

vec X(iCount);

double x;

cout<<"Input Xo :"<<endl;

while (i<iCount)

{

cin>>x;

X.x[i]=x;

i++;

}

double eps = 0.001;

X=minim(X, eps);

cout<<"Minimum: ";

i=0;

while (i<iCount)

{

cout<<X.x[i]<<" ";

i++;

}

}

main(){

system("cls");

char ch=0;

while (ch!=27){

cout<<"Laboratornaja rabota #9"<<endl;

cout<<"1) (x1-2)^4+(x1-2x2)^2"<<endl;

cout<<"2) [1.5-x1*(1-x2)]^2+[2.25-x1*(1-x2^2)]^2+[2.625-x1*(1-x2)^3]^2"<<endl;

Choise();

cout<<"Press <esc> for exit.";

cout<<endl;

ch = getch();

system("cls");

}

return 0;

}

//Matrix.h

class vec

{

public:

int Vn;

double *x;

vec operator +(const vec &x);

vec operator -(const vec &x);

void operator =(const vec &x);

vec operator *(double a);

vec operator /(double a);

vec operator /(vec x);

double operator *(const vec &x);

vec(int i1 = 5);

~vec();

};

class matrix

{

public:

int Mn;

vec *M;

matrix(int i1 = 5);

~matrix();

vec operator *(const vec &x);

matrix operator *(double a);

matrix operator +(const matrix &A);

matrix operator -(const matrix &A);

matrix operator /(double a);

matrix Tr(void);

};

//Matrix.cpp

#include "Matrix.h"

#include <iostream.h>

vec vec::operator +(const vec &x)

{

vec y(Vn);

for(int i=0;i<Vn;i++)

y.x[i]=this->x[i]+x.x[i];

return y;

}

vec vec::operator -(const vec &x)

{

vec y(Vn);

for(int i=0;i<Vn;i++)

y.x[i]=this->x[i]-x.x[i];

return y;

}

void vec::operator =(const vec &x)

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

this->x[i]=x.x[i];

}

vec vec::operator *(double a)

{ vec y(Vn);

for(int i=0;i<Vn;i++)

y.x[i]=this->x[i]*a;

return y;

}

vec vec::operator /(double a)

{ vec y(Vn);

for(int i=0;i<Vn;i++)

y.x[i]=this->x[i]/a;

return y;

}

vec vec::operator /(vec x)

{ ec y(Vn);

for (int i=0;i<Vn; i++)

y.x[i]=this->x[i]/x.x[i];

return y;

}

double vec::operator *(const vec &x)

{ double y=0;

for(int i=0;i<Vn;i++)

y+=this->x[i]*x.x[i];

return y;

}

vec::vec(int i1)

{ Vn = i1;

x = new double[Vn];

for(int i=0;i<Vn;i++)

x[i]=0;

}

vec::~vec()

{ x = 0;

delete [] x;

};

matrix::matrix(int i1)

{ Mn = i1;

M = new vec[Mn];

};

matrix::~matrix()

{ M = NULL;

delete [] M;

};

vec matrix::operator *(const vec &x)

{ vec y(Mn);

for(int i=0;i<Mn;i++)

y.x[i]=this->M[i]*x;

return y;

}

matrix matrix::operator *(double a)

{ matrix A(Mn);

for(int i=0;i<Mn;i++)

A.M[i]=this->M[i]*a;

return A;

}

matrix matrix::operator +(const matrix &A)

{ matrix B(Mn);

for(int i=0;i<Mn;i++)

B.M[i]=this->M[i]+A.M[i];

return B;

}

matrix matrix::operator -(const matrix &A)

{ matrix B(Mn);

for(int i=0;i<Mn;i++)

B.M[i]=this->M[i]-A.M[i];

return B;

}

matrix matrix::operator /(double a)

{ matrix A(Mn);

for(int i=0;i<Mn;i++)

A.M[i]=this->M[i]/a;

return A;

}

matrix matrix::Tr(void)

{ matrix B(Mn);

for(int i=0;i<Mn;i++)

for(int j=i;j<Mn;j++)

{

B.M[i].x[j]=this->M[j].x[i];

B.M[j].x[i]=this->M[i].x[j];

}

return B;

}

Вывод

В ходе выполнения лабораторной работы был изучен метод нулевого порядка Ррозенброка, а также разработана программа минимизации заданных функций. Тестирование показало, что при удачном задании начальной точки данный метод успешно справляется с поставленной задачей. Для первой функции при точности 0,001 и начальной точке (-5, 5) минимум найден за 19 итерации. Для точки

(1,5;4;2) число итераций 7.

18