コード例 #1
0
    static void Main()
    {
        int    NPoints = 10;
        vector xs      = new vector(NPoints);
        vector ys      = new vector(NPoints);
        int    i       = 0;
        double xa      = -4.5;
        double xb      = 4.5;
        double delta_x = (xb - xa) / (NPoints - 1);

        for (double x = xa; x <= xb; x += delta_x)
        {
            xs[i] = x;
            ys[i] = 1.0 / (1.0 + x * x);
            Error.Write("{0} {1}\n", x, ys[i]);
            i++;
        }

        qspline qspliner = new qspline(xs, ys);
        cspline cspliner = new cspline(xs, ys);

        for (double z = xa; z < xb; z += 0.05)
        {
            double linterp = linInterp.linterp(xs, ys, z);
            double qinterp = qspliner.spline(z);
            double cinterp = cspliner.spline(z);
            Write("{0} {1} {2} {3}\n", z, linterp, qinterp, cinterp);
        }
    }
コード例 #2
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);
    }
コード例 #3
0
ファイル: ode.integrator.cs プロジェクト: LukasWick/ppnm
    //Provide vector of times at wich you want the value.
    //Runs the ODE from first to last point while saving the points interpolates to give the values in y
    public static matrix driver(
        Func <double, vector, vector> f, /* right-hand-side of dydt=f(t,y) */
        vector ts,                       /* points return y at these points */
        vector y,                        /* starting y */
        double h   = 1e-1,               /* initial step-size */
        double acc = 1e-2,               /* absolute accuracy goal */
        double eps = 1e-2                /* relative accuracy goal */
        )                                /* return y(b) */
    {
        List <double> ts_found = new List <double>();
        List <vector> ys_found = new List <vector>();

        driver(f, ts[0], y, ts[-1], ts_found, ys_found, h, acc, eps); // solve using above driver to give points (ts[-1] is last element in vector)
        vector[] ys_vectors = new vector[y.size];

        vector ts_vector = new vector(ts_found.Count);

        // convert list to vector of right format
        for (int i = 0; i < y.size; i++)
        {
            ys_vectors[i] = new vector(ts_found.Count);
        }
        for (int i = 0; i < ts_found.Count; i++)
        {
            ts_vector[i] = ts_found[i];

            for (int j = 0; j < y.size; j++)
            {
                ys_vectors[j][i] = ys_found[i][j];
            }
        }

        cspline cs;
        matrix  ys_res = new matrix(y.size, ts.size);

        for (int i = 0; i < y.size; i++)
        {
            cs = new cspline(ts_vector, ys_vectors[i]);
            for (int j = 0; j < ts.size; j++)
            {
                ys_res[j][i] = cs.spline(ts[j]); //interpolates between points
            }
        }

        return(ys_res);
    }
コード例 #4
0
ファイル: main.cs プロジェクト: anders6400/Prakprog
    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}");
        }
    }
コード例 #5
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);
        }
    }
コード例 #6
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);
    }
コード例 #7
0
ファイル: main.cs プロジェクト: marcomajland/pracprog2020exam
    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);
    }