public static Result Evaluate(FittingFunction function, float a, float b, float accuracy) { float width = b - a; if (!Converge(function, a, b)) { return(Evaluate(function, a + (function(a) > 0 ? -width : width) * precentage, b + (function(a) > 0 ? -width : width) * precentage, accuracy)); } float center = (a + b) / 2.0f; int iterationCount = 0; do { iterationCount++; center = (a + b) / 2.0f; var result = function(center); if (function(a) * result <= 0) { b = center; } else { a = center; } } while (Math.Abs(b - a) > accuracy); return(new Result(iterationCount, center)); }
public static float[] Approximate(float a, float b, int degree, FittingFunction fittingFunction) { int n = degree + 1; float[] result = new float[n]; var u = ChebyshevSpace(n); float[] xs = new float[n]; float[] ys = new float[n]; for (int j = 0; j < n; j++) { xs[j] = Expand(u[j], a, b); ys[j] = Convert.ToSingle(fittingFunction(xs[j])); } for (int j = 0; j < n; j++) { float c = 0; for (int i = 0; i < n; i++) { var f = ys[i]; var t = CalculatePolynom(j, u[i]); c += f * t; } result[j] = (2.0f / n) * c; } result[0] /= 2.0f; return(result); }
private static bool Converge(FittingFunction function, double x) { var value = function(x); var derivative = (function(x + h) - 2 * function(x) + function(x - h)) / (h * h); return(value * derivative > 0); }
/// <summary> /// Check if convergention condition is achieved /// </summary> /// <param name="function">Function to evaluate</param> /// <param name="a">Left border</param> /// <param name="b">Right border</param> /// <returns></returns> public static bool Converge(FittingFunction function, float a, float b) { var aValue = function(a); var bValue = function(b); return((aValue * bValue) <= 0); }
private void DrawBasePoints(FittingFunction fittingFunction) { var points = new List <Point>(); var xValues = ChebyshevApproximation.GenerateX(a, b, step); for (int i = 0; i < xValues.Length; i++) { points.Add(new Point(xValues[i], fittingFunction(xValues[i]))); } RealDataPlotFor1.ItemsSource = RealDataPlotFor2.ItemsSource = RealDataPlotFor3.ItemsSource = RealDataPlotFor4.ItemsSource = RealDataPlotFor5.ItemsSource = RealDataPlotFor6.ItemsSource = RealDataPlotFor7.ItemsSource = RealDataPlotFor8.ItemsSource = RealDataPlotFor9.ItemsSource = points; }
public static Result Evaluate(FittingFunction function, float a, float b, float accuracy) { double x0 = SetStartPoint(function, a, b); int iterationCount = 0; double x = x0 - (function(x0) / FirstDerivative(function, x0, h)); while (IsAccuracyReached(function, x0, x, accuracy)) { x0 = x; iterationCount++; x = x0 - (function(x0) / FirstDerivative(function, x0, h)); } return(new Result(iterationCount, x)); }
private static double SetStartPoint(FittingFunction function, float a, float b) { if (Converge(function, a)) { return(a); } if (Converge(function, b)) { return(b); } float width = b - a; return(SetStartPoint(function, a + (function(a) > 0 ? -width : width) * percentage, b + (function(a) > 0 ? -width : width) * percentage)); }
private static double FirstDerivative(FittingFunction function, double x, double epsilon) { return((function(x + epsilon) - function(x)) / epsilon); }
private static bool IsAccuracyReached(FittingFunction function, double oldValue, double newValue, float accuracy) { return(Math.Abs(function(oldValue) - function(newValue)) > accuracy); }