| | |
| | | this.data = data; |
| | | |
| | | double[] xlist = data.Select(p => (double)p.X).ToArray(); |
| | | coefficients = Fit.Polynomial(xlist, data.Select(p => (double)p.Y).ToArray(), 2); |
| | | isFitted = true; |
| | | if (data.Count>2) |
| | | { |
| | | coefficients = Fit.Polynomial(xlist, data.Select(p => (double)p.Y).ToArray(), 2); |
| | | isFitted = true; |
| | | } |
| | | else if(data.Count==2) |
| | | { |
| | | //使用两个点的线性拟合 |
| | | var tuple = Fit.Line(xlist, data.Select(p => (double)p.Y).ToArray()); |
| | | coefficients= new DenseVector(new double[] { tuple.Item1, tuple.Item2 }); |
| | | isFitted = true; |
| | | } |
| | | else if (data.Count==1) |
| | | { |
| | | coefficients = new DenseVector(new double[] { data[0].Y }); |
| | | isFitted = true; |
| | | } |
| | | else |
| | | { |
| | | coefficients = new DenseVector(new double[] { 0}); |
| | | isFitted = true; |
| | | } |
| | | |
| | | var list = xlist.ToList(); |
| | | xMax = list.Max(); |
| | | xMin = list.Min(); |
| | |
| | | |
| | | public double Evaluate(double x) |
| | | { |
| | | while (!isFitted) ; |
| | | if (!isFitted) return double.NaN; |
| | | double y = 0; |
| | | for (int i = 0; i < coefficients.Count; i++) |
| | | { |
| | |
| | | public List<PointF> GetFitCurve(int count) |
| | | { |
| | | List<PointF> curve = new List<PointF>(); |
| | | for (double x = xMin; x < xMax; x += (xMax - xMin) / count) |
| | | |
| | | for (double x = xMin; x <= xMax && curve.Count<count; x += (xMax - xMin) / count) |
| | | { |
| | | double y = Evaluate(x); |
| | | curve.Add(new PointF((float)x, (float)y)); |