public void Q2Test() { var a = new[] { 1d, 2, 3 }; Func <double, double> q = x => { var sum = 0d; var r = a.Length; for (int k = 0; k < a.Length; k++) { sum += a[k] * Pow(x, r - k - 1) / fact(r - k - 1); } return(sum); }; var solver = new OdeSolver(x => x, a, a); var qCoeffs = Range(0, 3).Select(solver.QCoeffs).ToArray(); var Laguerre = Range(0, 3).Select(mathlib.Polynomials.Laguerre.Get).ToArray(); Func <double, double> qLaguerreExpansion = x => { var sum = 0d; for (int k = 0; k < qCoeffs.Length; k++) { sum += qCoeffs[k] * Laguerre[k](x); } return(sum); }; var t = 0d; while (t < 100) { Assert.That(q(t), Is.EqualTo(qLaguerreExpansion(t)).Within(0.0001)); t += 0.1; } }
public void QTest() { var solver = new OdeSolver(x => x, new[] { 1d, 2, 3 }, new[] { 1d, 2, 3 }); // Q[0] should be equal to sum of coeffs a_i, i=0, 1,..., r-1 Assert.AreEqual(1 + 2 + 3, solver.QCoeffs(0)); }
public static void Main() { Console.WriteLine("A) Refer to plot.svg, here u=-u'' is solved and the expected result is sin and cos"); double a = 0; vector ya = new vector(0, 1); double b = 2.25 * Math.PI; double h = 0.1, acc = 1e-3, eps = 1e-3; var data = OdeSolver.Driver(F, a, ya, b, h, acc, eps, 1000); File.WriteAllLines("Plot.txt", data.Xs .Select((x, i) => $"{x} {data.Ys[i][0]} {data.Ys[i][1]}")); a = 0; double r0 = 0; double i0 = 700; double population = 5500000; double s0 = population - r0 - i0; ya = new vector(s0, i0, r0); b = 120; h = 0.1; acc = 1e-3; eps = 1e-3; Console.WriteLine("B) SIR modelled with parameters fitting for denmark, refer to plot2.svg"); data = OdeSolver.Driver(sir(population, 8, 3), a, ya, b, h, acc, eps, 1000); data.Ys[1].print(); File.WriteAllLines("Plot2.txt", data.Xs .Select((x, i) => $"{x} {data.Ys[i][0]} {data.Ys[i][1]} {data.Ys[i][2]}")); }
static void RunDLSODE(int numberOfEquations, SolutionMethod solutionMethod, CorrectorIteratorMethod iteratorMethod) { Console.WriteLine("Calling DLSODE..."); SolverParams getChordWithDiagonalJacobianSolver() => new(new ChordWithDiagonalJacobianSolver(numberOfEquations, solutionMethod), GetInitialValues(numberOfEquations)) { StartTime = StartTime, EndTime = EndTime, }; SolverParams getFunctionalSolver() => new(new FunctionalSolver(numberOfEquations, solutionMethod), GetInitialValues(numberOfEquations)) { StartTime = StartTime, EndTime = EndTime, }; SolverParams throwNotSupported() => throw new NotSupportedException($"Iterator method: {iteratorMethod} is not supported."); var solverParam = iteratorMethod.Switch( onFunctional: getFunctionalSolver, onChordWithUserJacobian: throwNotSupported, onChordWithGeneratedJacobian: throwNotSupported, onChordWithDiagonalJacobian: getChordWithDiagonalJacobianSolver, onChordWithBandedUserJacobian: throwNotSupported, onChordWithBandedGeneratedJacobian: throwNotSupported); SolverResult solverResult; var sw = new Stopwatch(); sw.Start(); unsafe { solverResult = OdeSolver.Run(solverParam, FImpl2); } var elapsed = sw.Elapsed; OutputResults(solverResult, elapsed); }
private void Button3_Click(object sender, EventArgs e) { functions.Clear(); startValue.Clear(); span = new double[2]; foreach (DataGridViewRow row in dataGridView1.Rows) { row.Selected = false; if (row.Cells[0].Value != null) { functions.Add(row.Cells[0].Value.ToString()); } else { row.DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 0, 0); } } foreach (DataGridViewRow row in dataGridView2.Rows) { row.Selected = false; if (row.Cells[0].Value != null) { startValue.Add(Convert.ToDouble(row.Cells[0].Value)); } else { row.DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 0, 0); } } dataGridView3.Rows[0].Cells[0].Selected = false; dataGridView3.Rows[0].Cells[1].Selected = false; if (dataGridView3.Rows[0].Cells[0].Value != null && dataGridView3.Rows[0].Cells[1].Value != null) { if (Convert.ToDouble(dataGridView3.Rows[0].Cells[0].Value) < Convert.ToDouble(dataGridView3.Rows[0].Cells[1].Value)) { span[0] = Convert.ToDouble(dataGridView3.Rows[0].Cells[0].Value); span[1] = Convert.ToDouble(dataGridView3.Rows[0].Cells[1].Value); var solver = new OdeSolver(); var solution = solver.Ode45(functions.ToArray(), span[0], span[1], startValue.ToArray()); List <Function> result = new List <Function>(); foreach (Function f in solver.Ode45(functions.ToArray(), span[0], span[1], startValue.ToArray()).Functions) { result.Add(f); } for (int i = 0; i < result.Count; i++) { cartesianChart1.Series.Add(new LineSeries { Title = "f " + i.ToString() + " =", Values = new ChartValues <ObservablePoint>(), LineSmoothness = 0, PointGeometry = null }); for (int j = 0; j < result[i].Points.Length; j++) { cartesianChart1.Series.Last().Values.Add(new ObservablePoint(result[i].Points[j].X, result[i].Points[j].Y)); } } } else { MessageBox.Show("Enter a < b", "Uncorrect span data"); } } else { dataGridView3.Rows[0].DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 0, 0); } }
public void HCoeffsTest() { var solver = new OdeSolver(t => Pow(t, 3), new [] { 1d }, new [] { 1d }); var h = Range(0, 5).Select(solver.HCoeffs).ToArray(); }
public static int BinomsProductSum(int s, int k) { return(OdeSolver.BinomsProductSum(s, k)); }