Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1000000000000000000000000000000000000000000
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #include <vector>
- class DifferentialEquation{
- double U(double t){
- return (1 - exp(-10*(t+atan(t))));
- }
- double h;
- double t;
- std::ofstream file;
- public:
- DifferentialEquation(double h, double t){
- this->h = h;
- this->t = t;
- file.open("AnaliticData.dat");
- if(!file.is_open()){
- std::cerr<<"Blad otwarcia pliku.\n";
- exit(1);
- }
- for(double i = 0.0; i<t; i+=h){
- file<<i<<" "<<U(i)<<std::endl;
- }
- file.close();
- }
- std::vector<double> directEulerMethod(){
- double y = 0.0;
- file.open("DEM.dat");
- if(!file.is_open()){
- std::cerr<<"Blad otwarcia pliku.\n";
- exit(1);
- }
- std::vector<double> error;
- for(double i = 0.0; i<t; i+=h){
- error.push_back(fabs(y-U(i)));
- y = y + (10.0*i*i+20.0)/(i*i+1.0)*(y-1.0)*h;
- file<<i<<" "<<y<<"\n";
- }
- file.close();
- return error;
- }
- std::vector<double> indirectEulerMethod(){
- double y = 0.0;
- file.open("IEM.dat");
- if(!file.is_open()){
- std::cerr<<"Blad otwarcia pliku.\n";
- exit(1);
- }
- std::vector<double> error;
- for(double i = 0.0; i<t; i+=h){
- double t1 = i+h;
- double ft1 = (10.0*t1*t1+20.0)/(t1*t1+1);
- error.push_back(fabs(y - U(i)));
- y = (y/h + ft1)/(1.0/h+ft1);
- file<<i<<" "<<y<<std::endl;
- }
- file.close();
- return error;
- }
- std::vector<double> indirectTrapeziumMethod(){
- double y = 0.0;
- file.open("ITM.dat");
- if(!file.is_open()){
- std::cerr<<"Blad otwarcia pliku.\n";
- exit(1);
- }
- std::vector<double> error;
- for(double i = 0.0; i<t; i+=h){
- double t1 = i+h;
- double ft = (10.0*i*i+20.0)/(2*i*i+2);
- double ft1 = (10.0*t1*t1+20.0)/(2*t1*t1+2);
- error.push_back(fabs(y-U(i)));
- y = (y/h - ft*(y-1)+ft1)/(1.0/h + ft1);
- file<<i<<" "<<y<<std::endl;
- }
- file.close();
- return error;
- }
- };
- int main(){
- double t = 5;
- double h = 0.00001;
- DifferentialEquation equation(h, t);
- std::vector<double> errorDE, errorIE, errorIT;
- errorDE = equation.directEulerMethod();
- errorIE = equation.indirectEulerMethod();
- errorIT = equation.indirectTrapeziumMethod();
- std::ofstream file;
- file.open("Error.dat");
- if(!file.is_open()){
- std::cerr<<"Blad otwarcia pliku.\n";
- exit(1);
- }
- int j = 0;
- for(double i = 0.0; i<t; i+=h){
- file<<i<<" "<<errorDE.at(j)<<" "<<errorIE.at(j)<<" "<<errorIT.at(j++)<<std::endl;
- }
- file.close();
- return 0;
- }
- 999999999999999999999999999999999999999999999999999999
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- //d^2U(x)/(dx^2) + 4*U(x) + tan(x) = 0
- double U(double x){
- return (2*x*cos(2*x)+2*sin(2*x)-log(2)*sin(2*x)-2*log(cos(x))*sin(2*x))/4.0;
- }
- class Discretization{
- std::vector<double> l, d, u, b, xn;
- double x1, x2;
- double h;
- int numberOfSteps;
- char c;
- double U(double x){
- return 1.0/4.0*(2*x*cos(2*x)+2*sin(2*x)-log(2)*sin(2*x)-2*log(cos(x))*sin(2*x));
- }
- double maxError(){
- double max = 0.0;
- for(int i = 0; i<numberOfSteps; i++){
- double error = fabs(xn.at(i) - U(h*i));
- if(max<error){
- max = error;
- }
- }
- return max;
- }
- void clearVectors(){
- l.clear();
- d.clear();
- u.clear();
- b.clear();
- xn.clear();
- }
- public:
- Discretization(int numberOfSteps){
- this->numberOfSteps = numberOfSteps;
- x1 = 0.0;
- x2 = M_PI/4.0;
- h = fabs(x1-x2)/(numberOfSteps-1);
- }
- void thomasAlgorithm(){
- std::vector<double> n, r;
- n.push_back(d.at(0));
- r.push_back(b.at(0));
- for(int i = 1; i<numberOfSteps; i++){
- n.push_back(d.at(i)-(l.at(i)*u.at(i-1))/n.at(i-1));
- r.push_back(b.at(i)-(l.at(i)*r.at(i-1))/n.at(i-1));
- }
- xn.push_back(1.0/n.at(numberOfSteps-1)*r.at(numberOfSteps-1));
- int j = 0;
- for(int i = numberOfSteps-2; i>=0; i--){
- xn.push_back((r.at(i)-u.at(i)*xn.at(j++))/n.at(i));
- }
- std::reverse(xn.begin(), xn.end());
- }
- double conventionalDiscretization(){
- clearVectors();
- l.push_back(0.0);
- d.push_back(1.0);
- u.push_back(0.0);
- b.push_back(0.0);
- for(int i = 1; i<numberOfSteps-1; i++){
- l.push_back(1.0/(h*h));
- d.push_back(4.0-2.0/(h*h));
- u.push_back(1.0/(h*h));
- b.push_back(-tan(h*i));
- }
- l.push_back(0.0);
- d.push_back(1.0);
- u.push_back(0.0);
- b.push_back(0.5);
- thomasAlgorithm();
- return maxError();
- }
- double numerovDiscretization(){
- clearVectors();
- l.push_back(0.0);
- d.push_back(1.0);
- u.push_back(0.0);
- b.push_back(0.0);
- for(int i = 1; i<numberOfSteps-1; i++){
- l.push_back(4.0/12.0 + 1.0/(h*h));
- d.push_back(40.0/12.0 - 2.0/(h*h));
- u.push_back(4.0/12.0 + 1.0/(h*h));
- b.push_back(-(tan(h*(i-1))/12.0 + (10.0*tan(h*i))/12.0 + tan(h*(i+1))/12.0));
- }
- l.push_back(0.0);
- d.push_back(1.0);
- u.push_back(0.0);
- b.push_back(0.5);
- thomasAlgorithm();
- return maxError();
- }
- double getH(){
- return h;
- }
- void setNumberOfSteps(int numberOfSteps){
- this->numberOfSteps = numberOfSteps;
- h = fabs(x1-x2)/(numberOfSteps-1);
- }
- double getNumberOfSteps(){
- return numberOfSteps;
- }
- };
- int main(){
- int steps = 10;
- Discretization normal(steps);
- std::ofstream file;
- file.open("Data.dat");
- if(!file.is_open()){
- std::cerr<<"File wasn't open!";
- exit(0);
- }
- while(normal.getNumberOfSteps()<10000){
- file<<normal.getH()<<" "<<normal.conventionalDiscretization()<<" "<<normal.numerovDiscretization()<<"\n";
- steps+=100;
- normal.setNumberOfSteps(steps);
- }
- normal.numerovDiscretization();
- file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement