Читайте также:
|
|
Листинг программы:
#include <iostream.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
#include <fstream.h>
struct znach_x
{ double x1,x2;
};
double f(double x1,double x2)
{double y;
y=25*x1+0.9*x2+exp(0.35*pow(x1,2)+0.35*pow(x2,2));
return y;
}
double dfx1(double x1,double x2)
{double y;
y=25+0.7*x1*exp(0.35*pow(x1,2)+0.35*pow(x2,2));
return y;
}
double dfx2(double x1,double x2)
{double y;
y=0.9+0.7*x2*exp(0.35*pow(x1,2)+0.35*pow(x2,2));
return y;
}
double dfx1x1(double x1,double x2)
{double y,e;
e=exp(0.35*pow(x1,2)+0.35*pow(x2,2));
y=0.7*(e+0.7*x1*x1*e);
return y;
}
double dfx2x2(double x1,double x2)
{double y,e;
e=exp(0.35*pow(x1,2)+0.35*pow(x2,2));
y=0.7*(e+0.7*x2*x2*e);
return y;
}
double dfx1x2(double x1,double x2)
{double y;
y=0.49*x1*x2*exp(0.35*pow(x1,2)+0.35*pow(x2,2));
return y;
}
void newton()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F;
double f2[2][2];
int N1,N2,k,N;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
fout.open("result1.txt");
if(fout.fail()){ cout<<"result1.txt "<<"Mistake!";
getch(); exit(1);}
eps = 0.0004;
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;
m1:
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
x[k+1].x1=x[k].x1-p1;
x[k+1].x2=x[k].x2-p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) goto m1;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void newton_rafs_drobl()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F,delta,a,y,fp;
double f2[2][2];
int N1,N2,k,N,N0;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
eps=0.0004;
cout<<"Vvedite delta':\n";
cout<<"delta = "; cin>>delta;
fout.open("result2.txt");
if(fout.fail()){ cout<<"result2.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;a=1;N0=0;
m2:
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
y=f(x[k].x1,x[k].x2);
N0++;
fp=f1_1*p1+f1_2*p2; //scal.
m1:
if ((f(x[k].x1-a*p1,x[k].x2-a*p2)-y)>(-delta*a*fp)) {a=0.5*a;
N0++;goto m1;}
x[k+1].x1=x[k].x1-a*p1;
x[k+1].x2=x[k].x2-a*p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) goto m2;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2+N0;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void newton_rafs_optim()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F,y1,y2,A,B;
double a1,a2,amin,da,d0,a,f1,f3;
double f2[2][2];
int i,N1,N2,k,N,N0;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
cout<<"Vvedite da:\n";
cout<<"da = "; cin>>da;
eps=0.0004;
fout.open("result3.txt");
if(fout.fail()){ cout<<"result3.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;N0=0;
m2:
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
//opredel A,B
y1=f(x[k].x1,x[k].x2);
i=0;
m5:
y2=f(x[k].x1-((i+1)*da)*p1,x[k].x2-((i+1)*da)*p2);
N0++;
if (y2<y1) {i++;y1=y2;goto m5;}
else {B=(1+i)*da;
if (i>0) A=(i-1)*da;
else A=0;
}
//cout<<"A="<<A<<" B="<<B<<endl;
d0=eps/100;
do
{a=(A+B)/2.0;
a1=a-d0;
a2=a+d0;
f1=f(x[k].x1-a1*p1,x[k].x2-a1*p2);
f3=f(x[k].x1-a2*p1,x[k].x2-a2*p2);
N0=N0+2;
if (f1<f3) B=a2;
else A=a1;
}
while (B-A>2*eps);
amin=(A+B)/2.0;
x[k+1].x1=x[k].x1-amin*p1;
x[k+1].x2=x[k].x2-amin*p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) goto m2;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2+N0;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void f_modif_new()
{const int L=1500;
znach_x x[L];
znach_x x_;
long double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F;
long double f2[2][2];
int N1,N2,k,N;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
eps=0.0004;
fout.open("result4.txt");
if(fout.fail()){ cout<<"result4.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
cout<<"f1_1="<<f1_1<<" "<<"f1_2="<<f1_2<<endl;
k=0;N1=0;N2=0;
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
cout<<"11="<<f2[1][1]<<" "<<"12="<<f2[1][2]<<" "<<"21="<<f2[2][1]<<" "<<"22="<<f2[2][2]<<endl;
N2=N2+3;
m1:
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
cout<<"d="<<d<<" "<<"d1="<<d1<<" "<<"d2="<<d2<<endl;
p1=d1/d;
p2=d2/d;
cout<<"p1="<<p1<<" "<<"p2="<<p2<<endl;
x[k+1].x1=x[k].x1-p1;
x[k+1].x2=x[k].x2-p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
cout<<"f1_1="<<f1_1<<" f1_2="<<f1_2<<endl;
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
cout<<"F="<<F<<endl;
dlina=sqrt(F);
k++;
cout<<"k="<<k<<endl;
cout<<"dlina="<<dlina<<endl;
if (dlina>eps) goto m1;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void f_modif_new_rafs_drobl()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F,delta,a,y,fp;
double f2[2][2];
int N1,N2,k,N,N0;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
eps=0.0004;
cout<<"Vvedite delta':\n";
cout<<"delta = "; cin>>delta;
fout.open("result5.txt");
if(fout.fail()){ cout<<"result5.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;a=1; N0=0;
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
m2:
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
y=f(x[k].x1,x[k].x2);
N0++;
fp=f1_1*p1+f1_2*p2; //scal.
m1:
if ((f(x[k].x1-a*p1,x[k].x2-a*p2)-y)>(-delta*a*fp)) {a=0.5*a;
N0++;goto m1;}
x[k+1].x1=x[k].x1-a*p1;
x[k+1].x2=x[k].x2-a*p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) goto m2;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2+N0;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void f_modif_new_rafs_optim()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F,y1,y2,A,B;
double a1,a2,amin,da,d0,a,f1,f3;
double f2[2][2];
int N1,N2,k,N,N0,i;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
cout<<"Vvedite da:\n";
cout<<"da = "; cin>>da;
eps=0.0004;
fout.open("result6.txt");
if(fout.fail()){ cout<<"result6.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;N0=0;
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
m2:
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
//opredel A,B
y1=f(x[k].x1,x[k].x2);
i=0;
m5:
y2=f(x[k].x1-((i+1)*da)*p1,x[k].x2-((i+1)*da)*p2);
N0++;
if (y2<y1) {i++;y1=y2;goto m5;}
else {B=(1+i)*da;
if (i>0) A=(i-1)*da;
else A=0;
}
//cout<<"A="<<A<<" B="<<B<<endl;
d0=eps/100;
do
{a=(A+B)/2.0;
a1=a-d0;
a2=a+d0;
f1=f(x[k].x1-a1*p1,x[k].x2-a1*p2);
f3=f(x[k].x1-a2*p1,x[k].x2-a2*p2);
N0=N0+2;
if (f1<f3) B=a2;
else A=a1;
}
while (B-A>2*eps);
amin=(A+B)/2.0;
x[k+1].x1=x[k].x1-amin*p1;
x[k+1].x2=x[k].x2-amin*p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) goto m2;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2+N0;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void s_modif_new()
{
const int L=1500;
znach_x x[L];
znach_x x_;
long double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F;
long double f2[2][2];
int N1,N2,k,N,m=9;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
eps = 0.0004;
fout.open("result7.txt");
if(fout.fail()){ cout<<"result7.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
/*cout<<"x[k]:"<<endl<<x[0].x1<<endl<<x[0].x2<<endl;
cout<<"f1_1="<<f1_1<<endl;
cout<<"f1_2="<<f1_2<<endl;
*/
k=0;N1=0;N2=0;
m1:
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
/*cout<<"f211="<<f2[1][1]<<endl;
cout<<"f212="<<f2[1][2]<<endl;
cout<<"f221="<<f2[2][1]<<endl;
cout<<"f222="<<f2[2][2]<<endl;
*/
N2=N2+3;
m2:
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
/*cout<<"x[k]:"<<endl<<x[k].x1<<endl<<x[k].x2<<endl;
cout<<"d="<<d<<endl;
cout<<"d1="<<d1<<endl;
cout<<"d2="<<d2<<endl;
*/
p1=d1/d;
p2=d2/d;
x[k+1].x1=x[k].x1-p1;
x[k+1].x2=x[k].x2-p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<endl<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
//cout<<"F="<<F<<endl;
k++;
if (dlina>eps)
{if (k%m==0)goto m1;
else goto m2; }
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void s_modif_new_rafs_drobl()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F,delta,a,y,fp;
double f2[2][2];
int N1,N2,k,N,N0,m=2;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
eps=0.0004;
cout<<"Vvedite delta':\n";
cout<<"delta = "; cin>>delta;
fout.open("result8.txt");
if(fout.fail()){ cout<<"result8.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;a=1; N0=0;
m2:
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
m6:
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
y=f(x[k].x1,x[k].x2);
N0++;
fp=f1_1*p1+f1_2*p2; //scal.
m1:
if ((f(x[k].x1-a*p1,x[k].x2-a*p2)-y)>(-delta*a*fp)) {a=0.5*a;
N0++;goto m1;}
x[k+1].x1=x[k].x1-a*p1;
x[k+1].x2=x[k].x2-a*p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) { if (k%m==0) goto m2;
else goto m6; }
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2+N0;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void s_modif_new_rafs_optim()
{
const int L=1500;
znach_x x[L];
znach_x x_;
double eps,y_,f1_1,f1_2,d,d1,d2,p1,p2,dlina,F,y1,y2,A,B;
double a1,a2,amin,da,d0,a,f1,f3;
double f2[2][2];
int i,N1,N2,k,N,N0,m=2;
ofstream fout;
cout<<"Vvedite bazovuyu tochku:"<<endl;
cout<<"x0(x1,x2) ";cout<<endl;
cout<<"x1 = ";
cin>>x[0].x1;
cout<<"x2 = ";
cin>>x[0].x2;
cout<<"Vvedite da:\n";
cout<<"da = "; cin>>da;
eps=0.0004;
fout.open("result9.txt");
if(fout.fail()){ cout<<"result9.txt "<<"Mistake!";
getch(); exit(1);}
f1_1=dfx1(x[0].x1,x[0].x2);
f1_2=dfx2(x[0].x1,x[0].x2);
k=0;N1=0;N2=0;N0=0;
m2:
f2[1][1]=dfx1x1(x[k].x1,x[k].x2);
f2[1][2]=dfx1x2(x[k].x1,x[k].x2);
f2[2][1]=f2[1][2];
f2[2][2]=dfx2x2(x[k].x1,x[k].x2);
N2=N2+3;
m6:
d=f2[1][1]*f2[2][2]-pow(f2[1][2],2);
d1=f1_1*f2[2][2]-f1_2*f2[1][2];
d2=f2[1][1]*f1_2-f2[2][1]*f1_1;
p1=d1/d;
p2=d2/d;
//opredel A,B
y1=f(x[k].x1,x[k].x2);
i=0;
m5:
y2=f(x[k].x1-((i+1)*da)*p1,x[k].x2-((i+1)*da)*p2);
N0++;
if (y2<y1) {i++;y1=y2;goto m5;}
else {B=(1+i)*da;
if (i>0) A=(i-1)*da;
else A=0;
}
//cout<<"A="<<A<<" B="<<B<<endl;
d0=eps/100;
do
{a=(A+B)/2.0;
a1=a-d0;
a2=a+d0;
f1=f(x[k].x1-a1*p1,x[k].x2-a1*p2);
f3=f(x[k].x1-a2*p1,x[k].x2-a2*p2);
N0=N0+2;
if (f1<f3) B=a2;
else A=a1;
}
while (B-A>2*eps);
amin=(A+B)/2.0;
x[k+1].x1=x[k].x1-amin*p1;
x[k+1].x2=x[k].x2-amin*p2;
fout<<x[k+1].x1<<"; "<<x[k+1].x2<<endl;
cout<<(k+1)<<") "<<x[k+1].x1<<" "<<x[k+1].x2<<endl;
f1_1=dfx1(x[k+1].x1,x[k+1].x2);
f1_2=dfx2(x[k+1].x1,x[k+1].x2);
N1=N1+2;
F=f1_1*f1_1+f1_2*f1_2;
dlina=sqrt(F);
k++;
if (dlina>eps) if (k%m==0) goto m2;
else goto m6;
x_=x[k];
y_=f(x_.x1,x_.x2);
N=N1+N2+N0;
fout.close();
cout<<"Result:\n";
cout<<"x_(x1,x2) = x_("<<x_.x1<<","<<x_.x2<<")"<<endl;
cout<<"y_ = "<<y_<<endl;
cout<<"Kol-vo icpitanii: N0 = "<<N0<<endl;
cout<<"Kol-vo icpitanii: N1 = "<<N1<<endl;
cout<<"Kol-vo icpitanii: N2 = "<<N2<<endl;
cout<<"Kol-vo icpitanii: N = "<<N<<endl;
cout<<"k="<<k<<endl;
}
void main()
{int j;
while (1)
{clrscr();
cout<<endl;
cout<<"1.Method Newton\n";
cout<<"2.Metod Newton-Rafson s drobleniem\n";
cout<<"3.Metod Newton-Rafson optim\n";
cout<<"4.1 modification Newton\n";
cout<<"5.1 modification Newton-Rafson s drobleniem\n";
cout<<"6.1 modification Newton-Rafson optim\n";
cout<<"7.2 modification Newton\n";
cout<<"8.2 modification Newton-Rafson s drobleniem\n";
cout<<"9.2 modification Newton-Rafson optim\n";
cout<<"0.Exit\n";
cout<<endl;
cout<<"Your choice 0-9: ";
cin>>j;
switch(j)
{
case 1:newton();getch(); break;
case 2:newton_rafs_drobl();getch(); break;
case 3:newton_rafs_optim();getch(); break;
case 4:f_modif_new();getch(); break;
case 5:f_modif_new_rafs_drobl();getch(); break;
case 6:f_modif_new_rafs_optim();getch(); break;
case 7:s_modif_new();getch(); break;
case 8:s_modif_new_rafs_drobl();getch(); break;
case 9:s_modif_new_rafs_optim();getch(); break;
case 0: cout<<"Over"; getch();exit(0);
default:{cout<<"Net takogo punkta!";exit(1);
}
getch();}
}
}
Дата добавления: 2015-07-07; просмотров: 196 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Метод Ньютона | | | Тема № 7: Гистология желез пищеварительной системы у детей. |