Exemplo n.º 1
0
    public static void Main(string[] args)
    {
        vector x    = new vector(0, 1, 2, 3);
        vector y    = new vector(0, 2, 4, 6);
        vector siny = new vector(x.size);

        for (int i = 0; i < x.size; i++)
        {
            siny[i] = Sin(x[i]);
        }
        WriteLine(string.Format("# {0, -8} {1, -10} {2, -10} {3, -10} {4, -10} {5, -10} {6, -10} {7, -10} {8, -10}", "x", "y", "integral", "different", "sin(x)", "integral", "different", "exactint", "exactdiff"));
        double z;
        double steps = 4;         // interpolates 4 steps between points

        interpolate.quadratic quad = new interpolate.quadratic(x, y);
        interpolate.quadratic sin  = new interpolate.quadratic(x, siny);
        for (int i = 0; i < x.size - 1; i++)
        {
            for (int j = 0; j < steps; j++)
            {
                z = x[i] + (x[i + 1] - x[i]) * j / steps;
                WriteLine($"{z, -10:F3} {quad.spline(z), -10:F3} {quad.integrate(z), -10:F3} {quad.differentiate(z), -10:F3} {sin.spline(z), -10:F3} {sin.integrate(z), -10:F3} {sin.differentiate(z), -10:F3} {-Cos(z) + 1, -10:F3} {Cos(z), -10:F3}");
            }
        }
    }
Exemplo n.º 2
0
    public static void Main(string[] args)
    {
        double b    = 2 * PI;    // the range is from 0 to this
        int    len  = 10;
        double step = b / len;
        vector x    = new vector(len);
        vector y    = new vector(len);

        for (int i = 0; i < len; i++)
        {
            x[i] = i * step;
            y[i] = Sin(x[i]);
        }

        double steps = 4;         // interpolates 4 steps between points

        WriteLine("# DATA");
        WriteLine($"{"# x", -8} {"y", -8}");
        for (int i = 0; i < y.size; i++)
        {
            WriteLine($"{x[i], -8:F3} {y[i], -8:F3}");
        }

        WriteLine("\n\n# LINEAR");
        WriteLine($"{"# x", -8} {"sin(x)", -8} {"int", -8} {"expint", -8}");
        for (int i = 0; i < x.size - 1; i++)
        {
            for (double z = x[i]; z < x[i + 1]; z += (x[i + 1] - x[i]) / steps)
            {
                WriteLine($"{z, -8:F3} {interpolate.linear.spline(x, y, z), -8:F3} {interpolate.linear.integrate(x, y, z) - 1, -8:F3} {-Cos(z), -8:F3}");
            }
        }

        interpolate.quadratic qsin = new interpolate.quadratic(x, y);
        WriteLine("\n\n# QUADRATIC");
        WriteLine($"{"# x", -8} {"sin(x)", -8} {"int", -8} {"expint", -8} {"diff", -8} {"expdiff", -8}");
        for (int i = 0; i < x.size - 1; i++)
        {
            for (double z = x[i]; z < x[i + 1]; z += (x[i + 1] - x[i]) / steps)
            {
                WriteLine($"{z, -8:F3} {qsin.spline(z), -8:F3} {qsin.integrate(z) - 1, -8:F3} {-Cos(z), -8:F3} {qsin.differentiate(z), -8:F3} {Cos(z), -8:F3}");
            }
        }


        interpolate.cubic csin = new interpolate.cubic(x, y);
        WriteLine("\n\n# CUBIC");
        WriteLine($"{"# x", -8} {"sin(x)", -8} {"int", -8} {"expint", -8} {"diff", -8} {"expdiff", -8}");
        for (int i = 0; i < x.size - 1; i++)
        {
            for (double z = x[i]; z < x[i + 1]; z += (x[i + 1] - x[i]) / steps)
            {
                WriteLine($"{z, -8:F3} {csin.spline(z), -8:F3} {csin.integrate(z) - 1, -8:F3} {-Cos(z), -8:F3} {csin.differentiate(z), -8:F3} {Cos(z), -8:F3}");
            }
        }
    }
Exemplo n.º 3
0
    public static void Main(string[] args)
    {
        double[] x, y;
        x = new double[] { 0, PI *0.25, PI *0.5, PI *0.75, PI };
        y = new double[x.Length];
        for (int i = 0; i < x.Length; i++)
        {
            y[i] = Cos(x[i]);                           // y is cos(x)
        }
        double z;
        int    step = 5;      //number of steps between two points

        interpolate.quadratic q = new interpolate.quadratic(x, y);
        for (int i = 0; i < x.Length - 1; i++)
        {
            for (int j = 0; j < step; j++)
            {
                z = x[i] + j * (x[i + 1] - x[i]) / step;
                WriteLine($"{z} {q.spline(z)}	{Cos(z)}	{q.derivative(z)}	{-Sin(z)}	{q.integral(z)}	{Sin(z)}");
            }
        }
    }