private void ShowError() { double error = Math.Sqrt( CurveFunctions.ErrorSquared(Points, BestCoeffs)); txtError.Text = error.ToString(); }
private void btnFit_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; txtCoeffs.Clear(); txtError.Clear(); Application.DoEvents(); DateTime start_time = DateTime.Now; int degree = (int)nudDegree.Value; BestCoeffs = CurveFunctions.FindPolynomialLeastSquaresFit(Points, degree); HasSolution = true; DateTime stop_time = DateTime.Now; TimeSpan elapsed = stop_time - start_time; Console.WriteLine("Time: " + elapsed.TotalSeconds.ToString("0.00") + " seconds"); string txt = ""; foreach (double coeff in BestCoeffs) { txt += " " + coeff.ToString(); } txtCoeffs.Text = txt.Substring(1); ShowError(); HasSolution = true; picGraph.Refresh(); this.Cursor = Cursors.Default; }
private void picGraph_Paint(object sender, PaintEventArgs e) { e.Graphics.Transform = DrawingTransform; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; DrawAxes(e.Graphics); if (HasSolution) { using (Pen thin_pen = new Pen(Color.Blue, 0)) { const double x_step = 0.1; double y0 = CurveFunctions.F(BestCoeffs, Xmin); for (double x = Xmin + x_step; x <= Xmax; x += x_step) { double y1 = CurveFunctions.F(BestCoeffs, x); e.Graphics.DrawLine(thin_pen, (float)(x - x_step), (float)y0, (float)x, (float)y1); y0 = y1; } } } const float dx = (Xmax - Xmin) / 100; const float dy = (Ymax - Ymin) / 100; using (Pen thin_pen = new Pen(Color.Black, 0)) { foreach (PointF pt in Points) { e.Graphics.FillRectangle(Brushes.White, pt.X - dx, pt.Y - dy, 2 * dx, 2 * dy); e.Graphics.DrawRectangle(thin_pen, pt.X - dx, pt.Y - dy, 2 * dx, 2 * dy); } } }