Example #1
0
    static int Main()
    {
        int    N = 2000, n = 20;
        double z           = 0;
        double integration = 0;

        double[] xs = new double[N];
        double[] ys = new double[N];
        System.IO.StreamWriter xyfile = new System.IO.StreamWriter("out-xy.txt", append: false);
        for (int i = 0; i <= N - 1; i++)
        {
            xs[i] = 2.5 * PI * i / (N - 1);
            ys[i] = Sin(xs[i]);

            if (i == 0)
            {
                integration = 0;
            }
            else
            {
                integration += -Cos(xs[i]) + Cos(xs[i - 1]);
            }
            xyfile.WriteLine("{0} {1} {2} {3}", xs[i], ys[i], integration, Cos(xs[i]));
        }
        xyfile.Close();
        var Q = new cspline(new vector(xs), new vector(ys));

        System.IO.StreamWriter cinterpfile = new System.IO.StreamWriter("out-cinterp.txt", append: false);
        for (int i = 0; i <= n; i++)
        {
            z = 2 * PI * i / (n - 1);
            cinterpfile.WriteLine("{0} {1} ", z, Q.spline(z));
        }
        cinterpfile.Close();

        System.IO.StreamWriter cintegratefile = new System.IO.StreamWriter("out-cintegrate.txt", append: false);
        for (int i = 0; i <= n; i++)
        {
            z = 2 * PI * i / (n - 1);
            cintegratefile.WriteLine("{0} {1} ", z, Q.integral(z));
        }
        cintegratefile.Close();

        System.IO.StreamWriter cderivative = new System.IO.StreamWriter("out-cderivative.txt", append: false);
        for (int i = 0; i <= n; i++)
        {
            z = 2 * PI * i / (n);
            cderivative.WriteLine("{0} {1} ", z, Q.derivative(z));
        }
        cderivative.Close();


        return(0);
    }
Example #2
0
    static void Main()
    {
        vector[] data = generatedata.read_data("out_dataC.txt");
        vector   xs   = data[0];
        vector   ys   = data[1];

        cspline csplined = new cspline(xs, ys);

        double minx = xs[0];
        double maxx = xs[xs.size - 1];
        double z;

        for (z = minx; z < maxx; z += 0.1)
        {
            double cinterp    = csplined.spline(z);
            double integral   = csplined.integral(z);
            double derivative = csplined.derivative(z);
            WriteLine($"{z} {cinterp} {integral} {derivative}");
        }
    }
Example #3
0
    static void Main(string[] args)
    {
        vector[] testData = dataMaker.readFileToVector(args[0]);
        vector   xs       = testData[0];
        vector   ys       = testData[1];

        cspline cspliner = new cspline(xs, ys);

        double xa      = xs[0];
        double xb      = xs[xs.size - 1];
        double delta_z = 0.01;

        for (double z = xa; z < xb; z += delta_z)
        {
            double interp     = cspliner.spline(z);
            double derivative = cspliner.derivative(z);
            double integral   = cspliner.integral(z);
            Write("{0:f16} {1:f16} {2:f16} {3:f16}\n", z, interp, derivative, integral);
        }
    }
Example #4
0
    public static int Main()
    {
        int    n = 6;
        double x1 = 0, xend = 2 * PI;

        vector xs = new vector(n);
        vector ys = new vector(n);

        System.IO.StreamWriter outputfile = new System.IO.StreamWriter("out-xydata.txt", append: false);
        for (int i = 0; i < n; i++)
        {
            xs[i] = x1 + (xend - x1) / (n - 1) * i;
            ys[i] = Sin(xs[i]);
            outputfile.WriteLine("{0} {1}", xs[i], ys[i]);
        }
        outputfile.Close();

        int    N    = 2000;
        double z    = 0;
        double z1   = x1;
        double zend = xend;

        System.IO.StreamWriter outputfileSpline     = new System.IO.StreamWriter("out-Cspline.txt", append: false);
        System.IO.StreamWriter outputfileIntegral   = new System.IO.StreamWriter("out-CIntegral.txt", append: false);
        System.IO.StreamWriter outputfileDerivative = new System.IO.StreamWriter("out-CDerivative.txt", append: false);

        cspline spline = new cspline(xs, ys);

        for (int i = 0; i < N; i++)
        {
            z = z1 + (zend - z1) / (N - 1) * i;
            outputfileSpline.WriteLine("{0} {1} {2}", z, spline.spline(z), Sin(z));
            outputfileIntegral.WriteLine("{0} {1} {2}", z, spline.integral(z), 1 - Cos(z));
            outputfileDerivative.WriteLine("{0} {1} {2}", z, spline.derivative(z), Cos(z));
        }
        outputfileSpline.Close();
        outputfileIntegral.Close();
        outputfileDerivative.Close();
        return(0);
    }
Example #5
0
    public static int Main()
    {
        // The interpolation routines will be tested on the function f(x) = sin(x)
        double xmin = 0;                                                  // Minimum x value
        double xmax = 3 * PI;                                             // Maximum x value

        misc.generate_data(f1, xmin, xmax, 0.5, "./datafiles/data.txt");  // Generate tabulated function values
        misc.generate_data(f2, xmin, xmax, 0.5, "./datafiles/data2.txt"); // Generate tabulated integration values
        misc.generate_data(f3, xmin, xmax, 0.5, "./datafiles/data3.txt"); // Generate tabulated integration values
        // Load tabulated data values into double arrays
        List <double[]> data = misc.load_data("./datafiles/data.txt");

        double[] x = data[0];
        double[] y = data[1];
        int      n = x.Length;

        // Preparation of output files for plotting
        var lspline_out = new System.IO.StreamWriter("./datafiles/lspline_out.txt", append: false);
        var qspline_out = new System.IO.StreamWriter("./datafiles/qspline_out.txt", append: false);
        var cspline_out = new System.IO.StreamWriter("./datafiles/cspline_out.txt", append: false);
        var outfile     = new System.IO.StreamWriter("./out.txt", append: false);

        double dz = 0.01;
        // Output files for linear interpolation
        var res1 = new lspline(x, y);

        for (double z = x[0]; z <= x[x.Length - 1]; z += dz)
        {
            lspline_out.WriteLine($"{z} {res1.spline(z)} {res1.integral(z)}");
        }
        lspline_out.Close();
        // Output files for quadratic interpolation
        var res2 = new qspline(x, y);

        for (double z = x[0]; z <= x[x.Length - 1]; z += dz)
        {
            qspline_out.WriteLine($"{z} {res2.spline(z)} {res2.integral(z)} {res2.derivative(z)}");
        }
        qspline_out.Close();
        // Output files for cubic interpolation
        var res3 = new cspline(x, y);

        for (double z = x[0]; z <= x[x.Length - 1]; z += dz)
        {
            cspline_out.WriteLine($"{z} {res3.spline(z)} {res3.integral(z)} {res3.derivative(z)}");
        }
        cspline_out.Close();
        // Output files for comparsion of interpolation routines in terms of the integration values
        outfile.WriteLine($"In the following, the interpolation routines are compared with their integration values.");
        outfile.WriteLine($"For comparison, f(x) = sin(x) is interpolated and integrated from 0 to 2*pi (analytical value: 0).\n");
        outfile.WriteLine($"Linear interpolation:");
        outfile.WriteLine($"Integration result:       {res1.integral(2*PI)}");
        outfile.WriteLine($"Error:                    {0-res1.integral(2*PI)}\n");
        outfile.WriteLine($"Quadratic interpolation:");
        outfile.WriteLine($"Integration result:       {res2.integral(2*PI)}");
        outfile.WriteLine($"Error:                    {0-res2.integral(2*PI)}\n");
        outfile.WriteLine($"Cubic interpolation:");
        outfile.WriteLine($"Integration result:       {res3.integral(2*PI)}");
        outfile.WriteLine($"Error:                    {0-res3.integral(2*PI)}\n");
        outfile.Close();
        return(0);
    }
Example #6
0
    static int Main(string[] args)
    {
        if (args.Length < 3)
        {
            Console.Error.WriteLine("too few arguments");
            return(1);
        }
        string       infile     = args[0];
        string       outfile1   = args[1];
        string       outfile2   = args[2];
        string       outfile3   = args[3];
        StreamReader instream   = new StreamReader(infile);
        StreamWriter outstream1 = new StreamWriter(outfile1, append: false);
        StreamWriter outstream2 = new StreamWriter(outfile2, append: false);
        StreamWriter outstream3 = new StreamWriter(outfile3, append: false);

        //Importing the data into vectors
        List <double> xlist = new List <double>();
        List <double> ylist = new List <double>();

        do
        {
            string line = instream.ReadLine();
            if (line == null)
            {
                break;
            }
            string[] values = line.Split(' ', '\t');

            xlist.Add(Double.Parse(values[0]));
            ylist.Add(Double.Parse(values[1]));
        } while (true);

        int    n = xlist.Count;
        vector x = new vector(n);
        vector y = new vector(n);

        for (int i = 0; i <= (n - 1); i++)
        {
            x[i] = xlist[i];
            y[i] = ylist[i];
        }

        cspline s = new cspline(x, y);
        // Evaluating the spline
        int N = 999;

        for (int i = 0; i <= N; i++)
        {
            double z  = (x[n - 1] - x[0]) / N * i + x[0];
            double yz = s.eval(z);
            outstream1.WriteLine($"{z} \t {yz}");
        }

        // Evaluating the derivetives of the spline
        for (int i = 0; i <= N; i++)
        {
            double z       = (x[n - 1] - x[0]) / N * i + x[0];
            double slope_z = s.derivative(z);
            outstream2.WriteLine($"{z} \t {slope_z}");
        }


        // Evaluating the integral of the spline
        for (int i = 0; i <= N; i++)
        {
            double z      = (x[n - 1] - x[0]) / N * i + x[0];
            double area_z = s.integral(z);
            outstream3.WriteLine($"{z} \t {area_z}");
        }

        outstream1.Close();
        outstream2.Close();
        outstream3.Close();
        instream.Close();

        return(0);
    }