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 static void print(Function mFunc, Interpolation mIn) { Console.WriteLine(" x | f(x_i) | f'(x_i) | f'(x_i)Т - f'(x_i) | f''(x_i) | f''(x_i)Т - f''(x_i)"); // Console.Write(mFunc.f(x) + " "); for (int i = 0; i < mIn.mPoints.Count; i++) { float x = mIn.mPoints[i].x; float h = (mIn.b - mIn.a) / (mIn.mPoints.Count - 1); float fx = mFunc.f(x); float d1 = 0; float d2 = 0; // Console.WriteLine(h); if (i == 0) d1 = (-3 * mIn.mPoints[i].fx + 4 * mIn.mPoints[i + 1].fx - mIn.mPoints[i + 2].fx) / (2 * h); else if (i == mIn.mPoints.Count - 1) d1 = (3 * mIn.mPoints[i].fx - 4 * mIn.mPoints[i - 1].fx + mIn.mPoints[i - 2].fx) / (2 * h); else d1 = (mIn.mPoints[i + 1].fx - mIn.mPoints[i - 1].fx) / (2.0f * h); if (0 < i && i < mIn.mPoints.Count - 1) d2 = (mIn.mPoints[i + 1].fx - 2 * mIn.mPoints[i].fx + mIn.mPoints[i - 1].fx) / (h * h); Console.WriteLine("{0,5} {1,10} {2,10} {3,15} {4,15} {5,10}", x, fx, d1, Math.Abs(mFunc.divf(x) - d1), d2, Math.Abs(mFunc.div2f(x) - d2)); } }
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(); }
public abstract float findRoot(Segment ab, Interpolation F, float X, float e);
abstract public float findRoot(Segment ab, Interpolation F, float X, float e);