Exemplo n.º 1
0
    public static int Main(string[] args)
    {
        double[] x = new double[0];
        double[] y = new double[0];
        double[,] ytbl = new double[0, 0];
        double eps = 0;
        double h   = 0;
        int    m   = 0;
        int    i   = 0;

        odesolver.odesolverstate  state = new odesolver.odesolverstate();
        odesolver.odesolverreport rep   = new odesolver.odesolverreport();


        //
        // ODESolver unit is used to solve simple ODE:
        // y'' = -y, y(0) = 0, y'(0)=1.
        //
        // This ODE may be written as first-order system:
        // y' =  z
        // z' = -y
        //
        // Its solution is well known in academic circles :)
        //
        // Three intermediate values are calculated,
        // plus starting and final points.
        //
        y    = new double[2];
        y[0] = 0;
        y[1] = 1;
        x    = new double[5];
        x[0] = Math.PI * 0 / 4;
        x[1] = Math.PI * 1 / 4;
        x[2] = Math.PI * 2 / 4;
        x[3] = Math.PI * 3 / 4;
        x[4] = Math.PI * 4 / 4;
        eps  = 1.0E-8;
        h    = 0.01;
        odesolver.odesolverrkck(ref y, 2, ref x, 5, eps, h, ref state);
        while (odesolver.odesolveriteration(ref state))
        {
            state.dy[0] = state.y[1];
            state.dy[1] = -state.y[0];
        }
        odesolver.odesolverresults(ref state, ref m, ref x, ref ytbl, ref rep);
        System.Console.Write("     X   Y(X)     Error");
        System.Console.WriteLine();
        for (i = 0; i <= m - 1; i++)
        {
            System.Console.Write("{0,6:F3}", x[i]);
            System.Console.Write(" ");
            System.Console.Write("{0,6:F3}", ytbl[i, 0]);
            System.Console.Write("  ");
            System.Console.Write("{0,8:E1}", Math.Abs(ytbl[i, 0] - Math.Sin(x[i])));
            System.Console.WriteLine();
        }
        return(0);
    }
    public static int Main(string[] args)
    {
        double[] x = new double[0];
        double[] y = new double[0];
        double[,] ytbl = new double[0,0];
        double eps = 0;
        double h = 0;
        int m = 0;
        int i = 0;
        odesolver.odesolverstate state = new odesolver.odesolverstate();
        odesolver.odesolverreport rep = new odesolver.odesolverreport();

        
        //
        // ODESolver unit is used to solve simple ODE:
        // y'' = -y, y(0) = 0, y'(0)=1.
        //
        // This ODE may be written as first-order system:
        // y' =  z
        // z' = -y
        //
        // Its solution is well known in academic circles :)
        //
        // Three intermediate values are calculated,
        // plus starting and final points.
        //
        y = new double[2];
        y[0] = 0;
        y[1] = 1;
        x = new double[5];
        x[0] = Math.PI*0/4;
        x[1] = Math.PI*1/4;
        x[2] = Math.PI*2/4;
        x[3] = Math.PI*3/4;
        x[4] = Math.PI*4/4;
        eps = 1.0E-8;
        h = 0.01;
        odesolver.odesolverrkck(ref y, 2, ref x, 5, eps, h, ref state);
        while( odesolver.odesolveriteration(ref state) )
        {
            state.dy[0] = state.y[1];
            state.dy[1] = -state.y[0];
        }
        odesolver.odesolverresults(ref state, ref m, ref x, ref ytbl, ref rep);
        System.Console.Write("     X   Y(X)     Error");
        System.Console.WriteLine();
        for(i=0; i<=m-1; i++)
        {
            System.Console.Write("{0,6:F3}",x[i]);
            System.Console.Write(" ");
            System.Console.Write("{0,6:F3}",ytbl[i,0]);
            System.Console.Write("  ");
            System.Console.Write("{0,8:E1}",Math.Abs(ytbl[i,0]-Math.Sin(x[i])));
            System.Console.WriteLine();
        }
        return 0;
    }
    public static int Main(string[] args)
    {
        double[] x = new double[0];
        double[] y = new double[0];
        double[,] ytbl = new double[0, 0];
        double eps = 0;
        double h   = 0;
        int    m   = 0;
        int    i   = 0;

        odesolver.odesolverstate  state = new odesolver.odesolverstate();
        odesolver.odesolverreport rep   = new odesolver.odesolverreport();


        //
        // ODESolver unit is used to solve simple ODE:
        // y' = y, y(0) = 1.
        //
        // Its solution is well known in academic circles :)
        //
        // No intermediate values are calculated,
        // just starting and final points.
        //
        y    = new double[1];
        y[0] = 1;
        x    = new double[2];
        x[0] = 0;
        x[1] = 1;
        eps  = 1.0E-4;
        h    = 0.01;
        odesolver.odesolverrkck(ref y, 1, ref x, 2, eps, h, ref state);
        while (odesolver.odesolveriteration(ref state))
        {
            state.dy[0] = state.y[0];
        }
        odesolver.odesolverresults(ref state, ref m, ref x, ref ytbl, ref rep);
        System.Console.Write("    X  Y(X)");
        System.Console.WriteLine();
        for (i = 0; i <= m - 1; i++)
        {
            System.Console.Write("{0,5:F3}", x[i]);
            System.Console.Write(" ");
            System.Console.Write("{0,5:F3}", ytbl[i, 0]);
            System.Console.WriteLine();
        }
        return(0);
    }
    public static int Main(string[] args)
    {
        double[] x = new double[0];
        double[] y = new double[0];
        double[,] ytbl = new double[0,0];
        double eps = 0;
        double h = 0;
        int m = 0;
        int i = 0;
        odesolver.odesolverstate state = new odesolver.odesolverstate();
        odesolver.odesolverreport rep = new odesolver.odesolverreport();

        
        //
        // ODESolver unit is used to solve simple ODE:
        // y' = y, y(0) = 1.
        //
        // Its solution is well known in academic circles :)
        //
        // No intermediate values are calculated,
        // just starting and final points.
        //
        y = new double[1];
        y[0] = 1;
        x = new double[2];
        x[0] = 0;
        x[1] = 1;
        eps = 1.0E-4;
        h = 0.01;
        odesolver.odesolverrkck(ref y, 1, ref x, 2, eps, h, ref state);
        while( odesolver.odesolveriteration(ref state) )
        {
            state.dy[0] = state.y[0];
        }
        odesolver.odesolverresults(ref state, ref m, ref x, ref ytbl, ref rep);
        System.Console.Write("    X  Y(X)");
        System.Console.WriteLine();
        for(i=0; i<=m-1; i++)
        {
            System.Console.Write("{0,5:F3}",x[i]);
            System.Console.Write(" ");
            System.Console.Write("{0,5:F3}",ytbl[i,0]);
            System.Console.WriteLine();
        }
        return 0;
    }
Exemplo n.º 5
0
 public odesolverstate(odesolver.odesolverstate obj)
 {
     _innerobj = obj;
 }
Exemplo n.º 6
0
 public odesolverstate()
 {
     _innerobj = new odesolver.odesolverstate();
 }
 public odesolverstate(odesolver.odesolverstate obj)
 {
     _innerobj = obj;
 }
 public odesolverstate()
 {
     _innerobj = new odesolver.odesolverstate();
 }
Exemplo n.º 9
0
        /*************************************************************************
        Test
        *************************************************************************/
        public static bool testodesolver(bool silent)
        {
            bool result = new bool();
            int passcount = 0;
            bool curerrors = new bool();
            bool rkckerrors = new bool();
            bool waserrors = new bool();
            double[] xtbl = new double[0];
            double[,] ytbl = new double[0,0];
            odesolver.odesolverreport rep = new odesolver.odesolverreport();
            double[] xg = new double[0];
            double[] y = new double[0];
            double h = 0;
            double eps = 0;
            int solver = 0;
            int pass = 0;
            int mynfev = 0;
            double v = 0;
            int m = 0;
            int m2 = 0;
            int i = 0;
            double err = 0;
            odesolver.odesolverstate state = new odesolver.odesolverstate();
            int i_ = 0;

            rkckerrors = false;
            waserrors = false;
            passcount = 10;
            
            //
            // simple test: just A*sin(x)+B*cos(x)
            //
            ap.assert(passcount>=2);
            for(pass=0; pass<=passcount-1; pass++)
            {
                for(solver=0; solver<=0; solver++)
                {
                    
                    //
                    // prepare
                    //
                    h = 1.0E-2;
                    eps = 1.0E-5;
                    if( pass%2==0 )
                    {
                        eps = -eps;
                    }
                    y = new double[2];
                    for(i=0; i<=1; i++)
                    {
                        y[i] = 2*math.randomreal()-1;
                    }
                    m = 2+math.randominteger(10);
                    xg = new double[m];
                    xg[0] = (m-1)*math.randomreal();
                    for(i=1; i<=m-1; i++)
                    {
                        xg[i] = xg[i-1]+math.randomreal();
                    }
                    v = 2*Math.PI/(xg[m-1]-xg[0]);
                    for(i_=0; i_<=m-1;i_++)
                    {
                        xg[i_] = v*xg[i_];
                    }
                    if( (double)(math.randomreal())>(double)(0.5) )
                    {
                        for(i_=0; i_<=m-1;i_++)
                        {
                            xg[i_] = -1*xg[i_];
                        }
                    }
                    mynfev = 0;
                    
                    //
                    // choose solver
                    //
                    if( solver==0 )
                    {
                        odesolver.odesolverrkck(y, 2, xg, m, eps, h, state);
                    }
                    
                    //
                    // solve
                    //
                    while( odesolver.odesolveriteration(state) )
                    {
                        state.dy[0] = state.y[1];
                        state.dy[1] = -state.y[0];
                        mynfev = mynfev+1;
                    }
                    odesolver.odesolverresults(state, ref m2, ref xtbl, ref ytbl, rep);
                    
                    //
                    // check results
                    //
                    curerrors = false;
                    if( rep.terminationtype<=0 )
                    {
                        curerrors = true;
                    }
                    else
                    {
                        curerrors = curerrors | m2!=m;
                        err = 0;
                        for(i=0; i<=m-1; i++)
                        {
                            err = Math.Max(err, Math.Abs(ytbl[i,0]-(y[0]*Math.Cos(xtbl[i]-xtbl[0])+y[1]*Math.Sin(xtbl[i]-xtbl[0]))));
                            err = Math.Max(err, Math.Abs(ytbl[i,1]-(-(y[0]*Math.Sin(xtbl[i]-xtbl[0]))+y[1]*Math.Cos(xtbl[i]-xtbl[0]))));
                        }
                        curerrors = curerrors | (double)(err)>(double)(10*Math.Abs(eps));
                        curerrors = curerrors | mynfev!=rep.nfev;
                    }
                    if( solver==0 )
                    {
                        rkckerrors = rkckerrors | curerrors;
                    }
                }
            }
            
            //
            // another test:
            //
            //     y(0)   = 0
            //     dy/dx  = f(x,y)
            //     f(x,y) = 0,   x<1
            //              x-1, x>=1
            //
            // with BOTH absolute and fractional tolerances.
            // Starting from zero will be real challenge for
            // fractional tolerance.
            //
            ap.assert(passcount>=2);
            for(pass=0; pass<=passcount-1; pass++)
            {
                h = 1.0E-4;
                eps = 1.0E-4;
                if( pass%2==0 )
                {
                    eps = -eps;
                }
                y = new double[1];
                y[0] = 0;
                m = 21;
                xg = new double[m];
                for(i=0; i<=m-1; i++)
                {
                    xg[i] = (double)(2*i)/(double)(m-1);
                }
                mynfev = 0;
                odesolver.odesolverrkck(y, 1, xg, m, eps, h, state);
                while( odesolver.odesolveriteration(state) )
                {
                    state.dy[0] = Math.Max(state.x-1, 0);
                    mynfev = mynfev+1;
                }
                odesolver.odesolverresults(state, ref m2, ref xtbl, ref ytbl, rep);
                if( rep.terminationtype<=0 )
                {
                    rkckerrors = true;
                }
                else
                {
                    rkckerrors = rkckerrors | m2!=m;
                    err = 0;
                    for(i=0; i<=m-1; i++)
                    {
                        err = Math.Max(err, Math.Abs(ytbl[i,0]-math.sqr(Math.Max(xg[i]-1, 0))/2));
                    }
                    rkckerrors = rkckerrors | (double)(err)>(double)(Math.Abs(eps));
                    rkckerrors = rkckerrors | mynfev!=rep.nfev;
                }
            }
            
            //
            // end
            //
            waserrors = rkckerrors;
            if( !silent )
            {
                System.Console.Write("TESTING ODE SOLVER");
                System.Console.WriteLine();
                System.Console.Write("* RK CASH-KARP:                           ");
                if( rkckerrors )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                if( waserrors )
                {
                    System.Console.Write("TEST FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("TEST PASSED");
                    System.Console.WriteLine();
                }
            }
            result = !waserrors;
            return result;
        }