using System; using System.Collections.Generic; using System.Linq; using System.Drawing; public class Program { /// /// Gets the value at a given X using the line of best fit (Least Square Method) to determine the equation /// /// Points to calculate the value from /// Function input /// Value at X in the given points public static float LeastSquaresValueAtX(List points, float x) { float slope = SlopeOfPoints(points); float yIntercept = YInterceptOfPoints(points, slope); return (slope * x) + yIntercept; } /// /// Gets the slope for a set of points using the formula: /// m = ? (x-AVG(x)(y-AVG(y)) / ? (x-AVG(x))² /// /// Points to calculate the Slope from /// SlopeOfPoints private static float SlopeOfPoints(List points) { float xBar = points.Average(p => p.X); float yBar = points.Average(p => p.Y); float dividend = points.Sum(p => (p.X - xBar) * (p.Y - yBar)); float divisor = (float)points.Sum(p => Math.Pow(p.X - xBar, 2)); return dividend / divisor; } /// /// Gets the Y-Intercept for a set of points using the formula: /// b = AVG(y) - m( AVG(x) ) /// /// Points to calculate the intercept from /// Y-Intercept private static float YInterceptOfPoints(List points, float slope) { float xBar = points.Average(p => p.X); float yBar = points.Average(p => p.Y); return yBar - (slope * xBar); } public static void Main() { Console.WriteLine("Hello C#"); } }