public override float findRoot(Segment ab, Interpolation F, float X, float e) { float a = ab.a; float b = ab.b; float fa, fb, fc; //N = 0; while (true) { fa = F.Calc(a, new LagrangeMethod()) - X; fb = F.Calc(b, new LagrangeMethod()) - X; fc = F.Calc((a + b) / 2.0f, new LagrangeMethod()) - X; if ((b - a) < 2 * e) break; if (fc == 0) break; // System.Console.WriteLine("x = {0} {1}", b-a, 2 * e); // N++; if (fa * fc < 0) b = (a + b) / 2.0f; else a = (a + b) / 2.0f; } return (a + b) / 2.0f; }
public void Div() { float h = mh; int lCount = -1; // prev segments count int count = 0; //curent count float a = ma; float b = 0; bool calcH = false; if (h < 0) // if we need to calculate h automatic { h = (mb - ma) / 5.0f; calcH = true; } while (true) // for auto calc { count = 0; a = ma; mSegments.Clear(); while (a < mb) { b = Math.Min(mb, a + h); if ((mFunc.Calc(a, new LagrangeMethod()) - mX) * (mFunc.Calc(b, new LagrangeMethod()) - mX) < 0) { count++; mSegments.Add(new Segment(a, b)); } a = b; } if (!calcH) { break; } if ((count == lCount) || (h < MINH)) { break; } lCount = count; h /= 11.3f; } mh = h; }
static void Main(string[] args) { System.Console.WriteLine("The problem of the algebraic interpolation."); Interpolation i1 = new Interpolation(); Interpolation i2 = new Interpolation(); Function f = new EFunction(); float a = 0.0f; float b = 1.0f; int m = 10; int n = 10; System.Console.Write("Function: "); f.Print(); System.Console.WriteLine("Segment: [{0}, {1}]", a, b); System.Console.WriteLine("Params: m = {0}, n = {1}", m, n); System.Console.WriteLine("Inverse:"); i1.Func = f; i1.Init(m, n, a, b, true); System.Console.WriteLine("Normal:"); i2.Func = f; i2.Init(m, n, a, b, false); RootSolution rs = new RootSolution(); rs.Init(0.000001f, 0.05f, a, b, false); rs.Func = i2; Differential.print(f, i2); float fx; while (true) { System.Console.WriteLine("Input F"); float x = Convert.ToSingle(System.Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture); System.Console.WriteLine("First Solution"); fx = i1.Calc(x, new LagrangeMethod()); System.Console.WriteLine("Pn(x) = {0}", (float)fx); System.Console.WriteLine("efn(x) = {0}", Math.Abs((float)f.f(fx) - x)); System.Console.WriteLine("Second Solution"); rs.X = x; List <float> roots = rs.getRoots(new BisectionMethod()); for (int i = 0; i < roots.Count(); i++) { fx = roots[i]; System.Console.WriteLine("Pn(x) = {0}", (float)fx); System.Console.WriteLine("efn(x) = {0}", Math.Abs((float)f.f(fx) - x)); } } System.Console.ReadKey(); }
static void Main(string[] args) { System.Console.WriteLine("The problem of the algebraic interpolation."); Interpolation i1 = new Interpolation(); Interpolation i2 = new Interpolation(); Function f = new EFunction(); float a = 0.0f; float b = 1.0f; int m = 10; int n = 10; System.Console.Write("Function: "); f.Print(); System.Console.WriteLine("Segment: [{0}, {1}]", a, b); System.Console.WriteLine("Params: m = {0}, n = {1}", m, n); System.Console.WriteLine("Inverse:"); i1.Func = f; i1.Init(m, n, a, b, true); System.Console.WriteLine("Normal:"); i2.Func = f; i2.Init(m, n, a, b, false); RootSolution rs = new RootSolution(); rs.Init(0.000001f, 0.05f, a, b, false); rs.Func = i2; Differential.print(f, i2); float fx; while (true) { System.Console.WriteLine("Input F"); float x = Convert.ToSingle(System.Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture); System.Console.WriteLine("First Solution"); fx = i1.Calc(x, new LagrangeMethod()); System.Console.WriteLine("Pn(x) = {0}", (float)fx); System.Console.WriteLine("efn(x) = {0}", Math.Abs((float)f.f(fx) - x)); System.Console.WriteLine("Second Solution"); rs.X = x; List<float> roots = rs.getRoots(new BisectionMethod()); for (int i = 0; i < roots.Count(); i++) { fx = roots[i]; System.Console.WriteLine("Pn(x) = {0}", (float)fx); System.Console.WriteLine("efn(x) = {0}", Math.Abs((float)f.f(fx) - x)); } } System.Console.ReadKey(); }
override public float findRoot(Segment ab, Interpolation F, float X, float e) { float a = ab.a; float b = ab.b; float fa, fb, fc; //N = 0; while (true) { fa = F.Calc(a, new LagrangeMethod()) - X; fb = F.Calc(b, new LagrangeMethod()) - X; fc = F.Calc((a + b) / 2.0f, new LagrangeMethod()) - X; if ((b - a) < 2 * e) { break; } if (fc == 0) { break; } // System.Console.WriteLine("x = {0} {1}", b-a, 2 * e); // N++; if (fa * fc < 0) { b = (a + b) / 2.0f; } else { a = (a + b) / 2.0f; } } return((a + b) / 2.0f); }