static int Main(String[] args) { double[] xs, ys; // Make linear spline if (args[0] == "lin") { // f(x) = x if (args[1] == "1") { xs = new double[] { 1, 2, 3, 4, 5 }; ys = new double[] { 1, 2, 3, 4, 5 }; } // f(x) = x**2 else if (args[1] == "2") { xs = new double[] { 1, 2, 3, 4, 5 }; ys = new double[] { 1, 4, 9, 16, 25 }; } // f(x) = sin(x) else { xs = new double[] { 0, PI / 4, PI / 2, 3 * PI / 4, PI, PI *5 / 4, PI *3 / 2, PI *7 / 4, 2 * PI }; ys = new double[] { 0, 1 / Sqrt(2), 1, 1 / Sqrt(2), 0, -1 / Sqrt(2), -1, -1 / Sqrt(2), 0 }; } double eps = 1.0 / 16; for (double z = xs[0]; z <= xs[xs.Length - 1]; z += eps) { WriteLine($"{z} {linterp(xs,ys,z)} {linterpInteg(xs,ys,z)}"); } } else if (args[0] == "quad") { qspline s; if (args[1] == "1") { xs = new double[] { 1, 2, 3, 4, 5 }; ys = new double[] { 1, 2, 3, 4, 5 }; } else if (args[1] == "2") { xs = new double[] { 1, 2, 3, 4, 5 }; ys = new double[] { 2, 3, 2, 4, 2 }; } else { xs = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ys = new double[] { 1, 4, 9, 16, 25, 16, 9, 4, 1 }; } s = new qspline(xs, ys); double eps = 1.0 / 16; for (double z = xs[0]; z <= xs[xs.Length - 1]; z += eps) { // data printed as z spline(z) deriv(z) int(z) WriteLine($"{z} {s.spline(z)} {s.derivative(z)} {s.integral(z)}"); } } return(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 qspline(new vector(xs), new vector(ys)); System.IO.StreamWriter qinterpfile = new System.IO.StreamWriter("out-qinterp.txt", append: false); for (int i = 0; i <= n; i++) { z = 2 * PI * i / (n - 1); qinterpfile.WriteLine("{0} {1} ", z, Q.spline(z)); } qinterpfile.Close(); System.IO.StreamWriter qintegratefile = new System.IO.StreamWriter("out-qintegrate.txt", append: false); for (int i = 0; i <= n; i++) { z = 2 * PI * i / (n - 1); qintegratefile.WriteLine("{0} {1} ", z, Q.integral(z)); } qintegratefile.Close(); System.IO.StreamWriter qderivative = new System.IO.StreamWriter("out-qderivative.txt", append: false); for (int i = 0; i <= n; i++) { z = 2 * PI * i / (n - 1); qderivative.WriteLine("{0} {1} ", z, Q.derivative(z)); } qderivative.Close(); return(0); }
static int Main() { //test of qspline using Sin(x) int n = 9; //number of sample points //generate sample points: vector x = new vector(n); vector y = new vector(n); var points = new System.IO.StreamWriter("points.txt"); for (int i = 0; i < n; i++) { x[i] = 2 * i * (PI / (n - 1)); y[i] = Sin(x[i]); points.WriteLine($"{x[i]} {y[i]}"); } //end for points.Close(); //using qspline qspline qsin = new qspline(x, y); //generating data to plot var data = new System.IO.StreamWriter("data.txt"); int N = 100; for (int i = 0; i < N; i++) { double xn = 2 * i * (PI / (N - 1)); data.WriteLine($"{xn} {Sin(xn)} {qsin.spline(xn)} {Cos(xn)} {qsin.derivative(xn)} {1-Cos(xn)} {qsin.integral(xn)}"); } //end for data.Close(); //end of test using sine WriteLine($"Part B:"); WriteLine($"Quadratic spline of Sin(x) can be seen in quadratic_splineB.svg."); WriteLine($"The derivative of the aforementioned spline of can be seen in quadratic_derivativeB.svg, and the integral in quadratic_integralB.svg"); return(0); }
static void Main() { vector[] data = generatedata.read_data("out_dataB.txt"); vector xs = data[0]; vector ys = data[1]; qspline qsplined = new qspline(xs, ys); double minx = xs[0]; double maxx = xs[xs.size - 1]; double z; for (z = minx; z < maxx; z += 0.1) { double qinterp = qsplined.spline(z); double integral = qsplined.integral(z); double derivative = qsplined.derivative(z); WriteLine($"{z} {qinterp} {integral} {derivative}"); } }
static void Main(string[] args) { vector[] testData = dataMaker.readFileToVector(args[0]); vector xs = testData[0]; vector ys = testData[1]; qspline qspliner = new qspline(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 = qspliner.spline(z); double derivative = qspliner.derivative(z); double integral = qspliner.integral(z); Write("{0:f16} {1:f16} {2:f16} {3:f16}\n", z, interp, derivative, integral); } }
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-qSpline.txt", append: false); System.IO.StreamWriter outputfileIntegral = new System.IO.StreamWriter("out-qIntegral.txt", append: false); System.IO.StreamWriter outputfileDerivative = new System.IO.StreamWriter("out-qDerivative.txt", append: false); qspline spline = new qspline(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); }
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); }
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]; } qspline s = new qspline(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 derivetives 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); }