tode.cpp

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #include <iostream>
00023 #include <iomanip>
00024 #include <cmath>
00025 
00026 #include "numerical/integschemesctes.h"
00027 #include "numerical/autostepme.h"
00028 
00029 class ODE
00030 {
00031 public:
00032     ODE()
00033     {
00034         x  = 0.0;
00035         y  = 0.5;
00036         Dx = 2.0;
00037         z  = 0;
00038     }
00039     int Solve()
00040     {
00041         IntegSchemesCtes isc; isc.ME_maxSS(4000).ME_STOL(1.0e-7);
00042         AutoStepME<double,double,double,ODE> TI(isc);
00043         return TI.Solve(this, &ODE::tangent_dy_dz, &ODE::calc_error,
00044                         x,y,z,Dx);
00045     }
00046     double x;
00047     double y;
00048     double Dx;
00049     double z;
00050 private:
00051     int tangent_dy_dz(double const &  x, double const &  y, double const & z, 
00052                        double const & dx, double       & dy, double       & dz) const
00053     {
00054         // dy/dt=y-t^2+1 {0<=t<=2,y(0)=0.5} ==> x=t, z=0
00055         // solution: y(t)=(t+1)^2-0.5exp(t)
00056         dy = ( y-x*x+1 )*dx;
00057         dz = 0.0;
00058         return 0;
00059     }
00060     double calc_error(double const & Ey, double const & y_high,
00061                       double const & Ez, double const & z_high) const
00062     {
00063         return fabs(Ey)/fabs(y_high);
00064     }
00065 }; // class ODE
00066 
00067 
00068 
00069 using namespace std;
00070 
00071 int main(int argc, char **argv)
00072 {
00073     ODE ode;
00074 
00075     int k = ode.Solve(); // x will be x_1
00076     cout << "num of its (k) = " << k << endl;
00077 
00078     if (k==-1)
00079     {
00080         cout << "ERROR: TI did not converge!\n";
00081         return 1;
00082     }
00083 
00084     cout << "y_1 = " << setw(20) << setprecision(15) << ode.y << endl;
00085     cout << "solution:\n";
00086     cout << "y_1 = " << setw(20) << setprecision(15) << (ode.x+1.0)*(ode.x+1.0)-0.5*exp(ode.x) << endl;
00087 
00088     return 0;
00089 }

Generated on Wed Jan 24 15:56:27 2007 for MechSys by  doxygen 1.4.7