Click here to Skip to main content
15,898,036 members
Articles / Programming Languages / C#

Flight of a projectile

Rate me:
Please Sign up or sign in to vote.
4.81/5 (31 votes)
8 Jun 200712 min read 84.1K   3.2K   56  
Numerical solution of a set of second order differential equations
// --------------------------------------------------------
// Program.cs
//
// Author: Vit Buchta
// Date  : June 06, 2007
// --------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace NystromNDim
{
    /// <summary>
    /// Testing the integration of a set of differential equations
    /// of second order with initial conditions
    /// </summary>
    class Program
    {
        /// <summary>
        /// Flight of the projectile. This test calculates and print the flight
        /// of two projectiles. One of them neglects the air draw. The other projectile
        /// consideres the drag.
        /// </summary>
        static void Test1()
        {
            FlyingBall2 projectileA = new FlyingBall2(0.1, 600, 0.0, 9.81, 1.29);
            SecondDerivativeN accelFunctionA = (SecondDerivativeN)projectileA;
            FlyingBall2 projectileB = new FlyingBall2(0.1, 600, 0.5, 9.81, 1.29);
            SecondDerivativeN accelFunctionB = (SecondDerivativeN)projectileB;

            double t0 = 0;
            double tmax = 12;
            double h = 0.5;
            
            PointN startPosition = new PointN(2);
            startPosition[0] = 0;
            startPosition[1] = 0;
            
            PointN startVelocity = new PointN(2);
            startVelocity[0] = 50;
            startVelocity[1] = 50;

            PointN startAccelerationA = accelFunctionA.GetValue(t0, startPosition, startVelocity);
            NystromN integratorA = new NystromN(accelFunctionA, t0, startPosition, startVelocity,
                h, startAccelerationA);
            PointN startAccelerationB = accelFunctionB.GetValue(t0, startPosition, startVelocity);
            NystromN integratorB = new NystromN(accelFunctionA, t0, startPosition, startVelocity,
                h, startAccelerationB);

            Console.WriteLine("TEST1 - Flight of the projectile");
            Console.WriteLine(" t,s    xA,m    yA,m    xB,m    yB,m");
            Console.WriteLine("------------------------------------");
            Console.WriteLine("{0,4:F1}{1,8:F2}{2,8:F2}{3,8:F2}{4,8:F2}", t0,
                startPosition[0], startPosition[1],
                startPosition[0], startPosition[1]);

            double t;
            PointN positionA, velocityA, accelerationA;
            PointN positionB, velocityB, accelerationB;
            do
            {
                integratorA.Step(out t, out positionA, out velocityA, out accelerationA);
                integratorB.Step(out t, out positionB, out velocityB, out accelerationB);
                Console.WriteLine("{0,4:F1}{1,8:F2}{2,8:F2}{3,8:F2}{4,8:F2}", t,
                    positionA[0], positionA[1],
                    positionB[0], positionB[1]);
            } while (t < tmax);
            Console.WriteLine();
        }

        /// <summary>
        /// Mechanical system with two degrees of freedom. This test calculates
        /// the dynamical response of a system consisting from two masses
        /// and three springs.
        /// </summary>
        static void Test2()
        {
            CoupledOscillators couple = new CoupledOscillators(
                1.0, 1.0,
                40.0, 40.0, 10.0);
            SecondDerivativeN accelFunction = (SecondDerivativeN)couple;

            double t0 = 0, tmax = 6, h = 0.05;
            PointN startPosition = new PointN(2);
            startPosition[0] = 0.1;
            PointN startVelocity = new PointN(2);
            PointN startAcceleration = accelFunction.GetValue(t0, startPosition, startVelocity);
            NystromN integrator = new NystromN(accelFunction, t0, startPosition, startVelocity,
                h, startAcceleration);

            Console.WriteLine("TEST2 - Coupled oscillations");
            Console.WriteLine(" t,s      x0,m      x1,m   v0,m/s    v1,m/s");
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("{0,4:F2}{1,10:F3}{2,10:F3}{3,10:F3}{4,10:F3}", t0,
                startPosition[0], startPosition[1],
                startVelocity[0], startVelocity[1]);

            double t;
            PointN position, velocity, acceleration;
            do
            {
                integrator.Step(out t, out position, out velocity, out acceleration);
                Console.WriteLine("{0,4:F2}{1,10:F3}{2,10:F3}{3,10:F3}{4,10:F3}", t,
                    position[0], position[1],
                    velocity[0], velocity[1]);
            } while (t < tmax);
            Console.WriteLine();
        }
        
        static void Main(string[] args)
        {
            //Test1();
            Test2();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Czech Republic Czech Republic
Software developer, mainly in Visual C++

Comments and Discussions