00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00055
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 };
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();
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 }