Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <math.h>
- using namespace std;
- using std::setw;
- ofstream out;
- double H;
- double n;
- double f(double x)
- {
- return -sqrt(pow(x,2)*sqrt(12*pow(x,2)+9)-3*x*x)/sqrt(2);
- }
- double df(double x, double y)
- {
- return (3*pow(y,3)+6*y*x*x)/(2*x*y*y+3*pow(x,3));
- }
- double RungeKuttaStep(double x0, double y0)
- {
- double f1,f2,f3,f4;
- f1=df(x0,y0);
- f2=df(x0+H/2,y0+H/2*f1);
- f3=df(x0+H/2,y0+H/2*f2);
- f4=df(x0+H,y0+H*f3);
- y0+=H/6*(f1+2*f2+2*f3+f4);
- return y0;
- }
- void RungeKutta(double x0, double y0)
- {
- double e=0;
- printf("x0 \t y0 \t f(x0) \t |y0-f(x0)|\n");
- for(int i=0; i<1/H; i++){
- y0 = RungeKuttaStep(x0,y0);
- x0 += H;
- out << x0 << " " << y0 << " " << f(x0) << " " << fabs(y0-f(x0)) << endl;
- cout << setw(4) << x0 << " " << setw(8) << y0 << " " << setw(8) << f(x0) << " " << setw(13) << fabs(y0-f(x0)) << endl;
- if(e<fabs(y0-f(x0)))
- e=fabs(y0-f(x0));
- }
- out << "max|ei| " << e << endl;
- cout << "max|ei| " << e << endl;
- }
- void Adams(double x0, double y0)
- {
- double k[4];
- double e=0;
- k[0]=df(x0,y0);
- printf("x0 \t y0 \t f(x0) \t |y0-f(x0)|\n");
- for(int i=1; i<4; i++){
- y0=RungeKuttaStep(x0,y0);
- x0+=H;
- k[i]=df(x0,y0);
- out << x0 << " " << y0 << " " << f(x0) << " " << fabs(y0-f(x0)) << endl;
- cout << setw(4) << x0 << " " << setw(8) << y0 << " " << setw(8) << f(x0) << " " << setw(13) << fabs(y0-f(x0)) << endl;
- }
- for(int i=3; i<1/H; i++)
- {
- y0+=H/24*(55*k[3]-59*k[2]+37*k[1]-9*k[0]);
- x0+=H;
- for(int j=0; j<3; j++)
- {
- k[j]=k[j+1];
- }
- k[3]=df(x0,y0);
- if(e<fabs(y0-f(x0)))
- e=fabs(y0-f(x0));
- out << x0 << " " << y0 << " " << f(x0) << " " << fabs(y0-f(x0)) << endl;
- cout << setw(4) << x0 << " " << setw(8) << y0 << " " << setw(8) << f(x0) << " " << setw(13) << fabs(y0-f(x0)) << endl;
- }
- out << "max|ei| " << e << endl;
- cout << "max|ei| " << e << endl;
- }
- int main()
- {
- n=1024;
- out.open("Output.txt");
- for(int i=1; i<11; i++){
- H=1/n;
- n/=2;
- out << "H: " << H << endl;
- cout << "R-K:" << endl;
- RungeKutta(6,-18);
- cout << "Adams:" << endl;
- Adams(6,-18);
- cout << "***************************************" << endl;
- }
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement