using System.Collections.Generic;
public static void Main()
var lista = new List<PointF> { new PointF(500, 100),new PointF(1000, 100),new PointF(1500, 110),new PointF(2000, 140),new PointF(2100, 160) };
FindLinearLeastSquaresFit(lista, out m, out b);
Console.WriteLine("M:" + m + " B:" + b);
public static void FindLinearLeastSquaresFit(
List<PointF> points, out double m, out double b)
double S1 = points.Count;
foreach (PointF pt in points)
m = (Sxy * S1 - Sx * Sy) / (Sxx * S1 - Sx * Sx);
b = (Sxy * Sx - Sy * Sxx) / (Sx * Sx - S1 * Sxx);