256
#include
<iomanip>
#include
<fstream>
usingnamespace
std;
double
fy(
double
t,
double
p,
double
z){
return
z;}
double
fz(
double
t,
double
p,
double
z){
return
15.0-2.0*z-5.0*p; }
void
main(){
double
t,p,z;
double
k0,k1,k2,k3,l0,l1,l2,l3;
double
a=0,b=3,n=100,h=(b-a)/n;
int
i;
double
t0=a,p0=4,z0=1;
fstream f;
f.open(
"RK.xls"
,ios::out);
cout<<setw(10)<<
"t"
<<setw(10)<<
"p"
<<setw(10)<<
"z"
<<endl;
cout<<setw(10)<<t0<<setw(10)<<p0<<setw(10)<<z0<<endl;
f<<
"t"
<<
"\t"
<<
"p"
<<
"\t"
<<
"z"
<<endl;
f<<t0<<
"\t"
<<p0<<
"\t"
<<z0<<endl;
for
(i=0; i<n; i++){
k0=fy(t0,p0,z0);
l0=fz(t0,p0,z0);
k1=fy(t0+h/2.0,p0+h*k0/2.0,z0+h*l0/2.0);
l1=fz(t0+h/2.0,p0+h*k0/2.0,z0+h*l0/2.0);
k2=fy(t0+h/2.0,p0+h*k1/2.0,z0+h*l1/2.0);
l2=fz(t0+h/2.0,p0+h*k1/2.0,z0+h*l1/2.0);
k3=fy(t0+h/2.0,p0+h*k2,z0+h*l2);
l3=fz(t0+h/2.0,p0+h*k2,z0+h*l2);
t=t0+h;
p=p0+h/6.0*(k0+2.0*k1+2.0*k2+k3);
z=z0+h/6.0*(l0+2.0*l1+2.0*l2+l3);
cout<<setw(10)<<t<<setw(10)<<p<<setw(10)<<z<<endl;
f<<t<<
"\t"
<<p<<
"\t"
<<z<<endl;
t0=t; p0=p; z0=z;
}
f.close();
}
Результаты выполнения программы представлены на рис. 8.18, рис.
8.19.
Рис. 8.18. Результат выполнения программы