Листинг программы метод покоординатного спуска с постоянным шагом
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <conio.h>
double Ff(double x1, double x2)
{ double f;
f=20*x1+0.4*x2+exp(0.3*x1*x1+0.3*x2*x2);
return(f);
}
double Fx1(double x1, double x2)
{double f1;
f1=20+0.6*x1*exp(0.3*x1*x1+0.3*x2*x2);
return(f1);
}
double Fx2(double x1, double x2)
{double f2;
f2=0.4+0.6*x2*exp(0.3*x1*x1+0.3*x2*x2);
return(f2);
}
void main ()
{int k,i,N,N0,N1,l1,l2,choos,des;
double a,b,d,ymin,xmin1,xmin2,e0,alpha;
double x[3000][2]; double y[10];
x[0][1]=0;
x[0][2]=-1;
e0=0.0001;
//FILE*f=fopen("d:\\lab2noctwar.xls","w");
cout<<"Metod nokoopduHatHoro cnycka c noct.waroM"<<endl;
alpha=0.03;
k=0;N1=0; cout<<"\nk="<<k+1;
m1:
x[2*k+1][1]=x[2*k][1]-alpha*Fx1(x[2*k][1],x[2*k][2]); N1++;
cout<<"\nx[1][1]="<<x[2*k+1][1];
x[2*k+1][2]=x[2*k][2];
cout<<"\nx[1][2]="<<x[2*k+1][2]<<endl;
x[2*k+2][1]=x[2*k+1][1];
x[2*k+2][2]=x[2*k+1][2]-alpha*Fx2(x[2*k+1][1],x[2*k+1][2]);N1++;
//fprintf(f,"%Lf\t%Lf\n",x[k][1],x[k][2]);
d=pow ((pow(x[2*k+2][1]-x[2*k][1],2)+ pow(x[2*k+2][2]-x[2*k][2],2)),0.5);
if (d>e0) {k++; cout<<"\nk="<<k+1; goto m1;}
else {xmin1=x[2*k+2][1];xmin2=x[2*k+2][2];ymin=Ff(xmin1,xmin2);cout<<"\nx1="<<xmin1<<" x2="<<xmin2<<" ymin="<<ymin<<endl<<"N1="<<N1<<" k="<<k+1<<endl;}
getch();
//fclose(f);
}
Листинг программы метод симплекса
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
long int N=0;
long double fx(long double x1,long double x2)
{ N++;
long double f;
f=20*x1+0.4*x2+exp(0.3*x1*x1+0.3*x2*x2);
return(f);}
int i,j;
int main()
{ cout<<"Metod simplex"<<endl;
long double **x = new long double *[2];
for(int p=1;p<3;p++)
x[p]=new long double[200];
x[1][0]=0;x[2][0]=-1;
long double r=2.5,E=0.0001;
cout<<"\nHa4a/lbHoe npu6/lu}l{eHue=(0;-1) E=0.0001 r=2.5"<<endl;
x[1][1]=x[1][0]+r; x[2][1]=x[2][0];
x[1][2]=x[1][0]; x[2][2]=x[2][0]+r;
cout<<"\nx11="<<x[1][1]<<" x21="<<x[2][1]<<endl;
cout<<"x12="<<x[1][2]<<" x22="<<x[2][2]<<endl;
long double *y=new long double[4];
y[0]=fx(x[1][0],x[2][0]);
y[1]=fx(x[1][1],x[2][1]);
y[2]=fx(x[1][2],x[2][2]);
long double c1,c2,u1,u2;
int l1,l2;
metka: l1=0,l2=0;
for(int i=0;i<3;i++)
{if(y[i]<y[l1]) l1=i; }
for(int j=0;j<3;j++)
{if(y[j]>y[l2]) l2=j; }
if(r>E) { c1=0,c2=0;
for(int i=0;i<3;i++)
if(i!=l2)
{c1+=x[1][i]; c2+=x[2][i];}
c1/=2; c2/=2;
u1=2*c1-x[1][l2];
u2=2*c2-x[2][l2];
y[3]=fx(u1,u2);
if(y[3]<y[l2])
{ x[1][l2]=u1;
x[2][l2]=u2;
y[l2]=y[3];
cout<<""<<x[1][l2]<<"; "<<x[2][l2]<<endl;
}
else { for(int i=0;i<3;i++)
if(i!=l1)
{x[1][i]=(x[1][i]+x[1][l1])/2;
x[2][i]=(x[2][i]+x[2][l1])/2;
y[i]=fx(x[1][i],x[2][i]);
cout<<""<<x[1][i]<<"; "<<x[2][i]<<endl;
}
r=r/2;
} goto metka;
}
else{ long double minx1,minx2,minf;
minx1=x[1][l1];
minx2=x[2][l1];
minf=y[l1];
cout<<"X1min="<<minx1<<" X2min="<<minx2<<"\nfmin="<<minf<<endl;
cout<<"N="<<N<<endl;
getch();
return(0);}
}
Листинг программы метод поиска по образцу
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
long int N=0;
long double fx(long double x1,long double x2)
{ N=N++;
long double f;
f=20*x1+0.4*x2+exp(0.3*x1*x1+0.3*x2*x2);
return(f);
}
int main()
{ cout<<"----MeToD noucka no o6pazcy----"<<endl;
long double **x = new long double *[2];
for(int p=0;p<5;p++)
x[p]=new long double[6];
x[1][0]=-1;
x[2][0]=0;
long double h=0.8,e=0.0001;
long double *y=new long double[6];
y[0]=fx(x[1][0],x[2][0]);
cout<<"\nHa4a/lbHoe npu6/lu}l{eHue=(0;-1) E=0.0001 h=0.8"<<endl;
int t=0; int iter=1; cout<<"\nIteracia HoMep="<<iter<<endl;
metka:
if (t!=1)
{ x[1][1]=x[1][0]-h;
x[2][1]=x[2][0]-h;
y[1]=fx(x[1][1],x[2][1]);
cout<<"x11="<<x[1][1]<<" x21="<<x[2][1]<<endl;
}
if (t!=2)
{ x[1][2]=x[1][0]-h;
x[2][2]=x[2][0]+h;
y[2]=fx(x[1][2],x[2][2]);
cout<<"x12="<<x[1][2]<<" x22="<<x[2][2]<<endl;
}
if (t!=3)
{ x[1][3]=x[1][0]+h;
x[2][3]=x[2][0]+h;
y[3]=fx(x[1][3],x[2][3]);
cout<<"x13="<<x[1][3]<<" x23="<<x[2][3]<<endl;
}
if (t!=4)
{ x[1][4]=x[1][0]+h;
x[2][4]=x[2][0]-h;
y[4]=fx(x[1][4],x[2][4]);
cout<<"x14="<<x[1][4]<<" x24="<<x[2][4]<<endl;
}
t=0;
int l=1;
for(int i=1;i<5;i++)
{ if(y[i]<y[l]) l=i; }
if(y[l]<y[0])
{ if(l>2) {x[1][l-2]=x[1][0];
x[2][l-2]=x[2][0];y[l-2]=y[0];t=l-2;
}
else
{x[1][l+2]=x[1][0];
x[2][l+2]=x[2][0];
y[l+2]=y[0];t=l+2;
}
x[1][0]=x[1][l];x[2][0]=x[2][l];y[0]=y[l];
iter++;
cout<<"\nIteracia HoMep="<<iter<<endl; goto metka;
}
else
{h=h/2;
if(h>e)
{iter++; cout<<"\nIteracia HoMep="<<iter<<endl; goto metka;}
else
{ long double minx1,minx2,minf;
minx1=x[1][0]; minx2=x[2][0]; minf=y[0];
cout<<"X1min="<<minx1<<"X2min="<<minx2<<"fmin="<<minf<<"\nIteracii="<<iter<<endl;
cout<<"N="<<N<<endl;
getch();
return(0);}
}
}