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); } }
static int Main() { int n = 40; int N = 100; double[] x = new double[n]; double[] y = new double[n]; int i; //change for (i = 0; i < n; i++) { x[i] = 2 * PI * i / (n - 1); y[i] = Sin(x[i]); //WriteLine("equidistant points of sine {0:g6} {1:g6}", x[i],y[i]); } Write("\n"); var cs = new cspline(x, y); var qua = new quadspline(x, y); var cub = new cubespline(x, y); double z = x[0]; double step = (x[n - 1] - x[0]) / (N - 1); for (i = 0; i < N; i++) { z = x[0] + i * step; WriteLine($"{z} {cs.eval(z)} {cs.integ(z)} {qua.eval(z)} {qua.integ(z)} {qua.derive(z)} {cub.eval(z)} {cub.integ(z)} {cub.derive(z)}"); } // Write($"test integration of sine from 0 to 2pi: {cs.integ(2*PI)}\n"); // Write($"evaluate interpol sin at pi/2, pi: {cs.eval(PI/2)} {cs.eval(PI)}\n"); return(0); }
static void Main(){ int n=5, N=200; double[] x = new double[n]; double[] y = new double[n]; int i; for (i=0; i<n; i++){ x[i]=2*PI*i/(n-1); y[i]=Sin(x[i]); WriteLine("{0:g6} {1:g6}",x[i],y[i]); } Write("\n\n"); var cs = new cspline(x,y); double z, step=(x[n-1]-x[0])/(N-1); for (z=x[0], i=0; i<N; z=x[0]+(++i)*step){ WriteLine($"{z} {Sin(z)} {cs.eval(z)}"); } Write("\n\n"); for (z=x[0], i=0; i<N; z=x[0]+(++i)*step){ WriteLine($"{z} {Cos(z)} {cs.deriv(z)}"); } Write("\n\n"); for (z=x[0], i=0; i<N; z=x[0]+(++i)*step){ WriteLine($"{z} {1-Cos(z)} {cs.integ(z)}"); } }//Main
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); }
//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); }
static int Main() { //test of cubicspline using Sin(x) int n = 11; //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 cspline csin = new cspline(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)} {csin.c_spline(xn)} {Cos(xn)} {csin.cderivative(xn)} {1-Cos(xn)} {csin.cintegral(xn)}"); } //end for data.Close(); //end of test using sine WriteLine($"Part C:"); WriteLine($"Cubic spline of Sin(x) can be seen in cubic_splineC.svg."); WriteLine($"The derivative of the aforementioned spline of can be seen in cubic_derivativeC.svg, and the integral in cubic_integralC.svg"); WriteLine($"In comparisonC.svg my the calculated cubic spline is compared with spline from plotutils"); return(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}"); } }
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); } }
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); }
static int Main(string[] args) { // input to vector... var input = new System.IO.StreamReader("sin_data.txt"); int input_lenght = 0; while (input.ReadLine() != null) { input_lenght++; } input.Close(); input = new System.IO.StreamReader("sin_data.txt"); vector x1 = new vector(input_lenght); vector y1 = new vector(input_lenght); string line; for (int n = 0; n < input_lenght; n++) { line = input.ReadLine(); string[] words = line.Split(' '); x1[n] = double.Parse(words[0]); y1[n] = double.Parse(words[1]); } /// end input to vector cspline a = new cspline(x1, y1); for (double n = 0; n <= 10; n += 0.05) { WriteLine($"{n} {a.cintegral(n)}"); } 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]; } 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); }
static int Main() { //test with sin(x) int n = 7; vector x = new vector(n); vector y = new vector(n); for (int i = 0; i < n; i++) { x[i] = 2 * i * (PI / (n - 1)); y[i] = Sin(x[i]); } //end for Akima AA = new Akima(x, y); //outfile for the points given to Akima var sinp = new System.IO.StreamWriter("sinp.txt"); for (int k = 0; k < n; k++) { sinp.WriteLine($"{x[k]} {y[k]}"); } //end for sinp.Close(); //outfile for data for sin(x) and it's derivative and anti-derivativ var sind = new System.IO.StreamWriter("sindata.txt"); int N = 100; for (int i = 0; i < N; i++) { double xnew = 2 * i * (PI / (N - 1)); sind.WriteLine($"{xnew} {Sin(xnew)} {AA.spline(xnew)} {Cos(xnew)} {AA.derivative(xnew)} {1-Cos(xnew)} {AA.integral(xnew)} "); } //end for sind.Close(); //test using f2=O(x-1)-O(x-2) where O is the unity step function //step function Func <double, double> O = (z) => { if (z < 0) { return(0); } else { return(1); } }; int n2 = 11; //generate generation-points: vector x2 = new vector(n2); vector y2 = new vector(n2); var stepp = new System.IO.StreamWriter("stepp.txt"); for (int i = 0; i < n2; i++) { x2[i] = (3.0 / (n2 - 1)) * i; y2[i] = O(x2[i] - 1) - O(x2[i] - 2); stepp.WriteLine($"{x2[i]} {y2[i]}"); } //end for stepp.Close(); //akima on f2 Akima Af2 = new Akima(x2, y2, new vector(0.0)); //cubic spline on f2 cspline Cf2 = new cspline(x2, y2); //data to plot of step function var step = new System.IO.StreamWriter("stepdata.txt"); int N2 = 100; for (int i = 0; i < N2; i++) { double xnew2 = (3.0 / (N2 - 1)) * i; step.WriteLine($"{xnew2} {Af2.spline(xnew2)} {Cf2.c_spline(xnew2)}"); } //end for step.Close(); return(0); }