private void AutoFindRoots() { float x1, x2; double eps; errorProvider.Clear(); try { eps = float.Parse(textE.Text); } catch (FormatException) { errorProvider.SetError(textE, "Wrong float number"); return; } if (eps < 0) { errorProvider.SetError(textE, "Has to be > 0"); return; } try { x1 = float.Parse(textX1.Text); } catch (FormatException) { errorProvider.SetError(textX1, "Wrong float number"); return; } try { x2 = float.Parse(textX2.Text); } catch (FormatException) { errorProvider.SetError(textX2, "Wrong float number"); return; } float step = (x2 - x1) / 100f; DekartForm dForm; dForm = new DekartForm(100, 100, 175, 300); dForm.Use_IsVisible = false; dForm.Size = new Size(350, 600); dForm.CenterView(); listRoots.Items.Clear(); listY.Items.Clear(); roots = new List <double>(); for (float x = x1; x < x2; x += step) { double res = double.NaN; List <PointF[]> lines = null; switch (selectedSolMeth) { #region Method Still Point case SolutionMethod.StillPoint: if (dForm.CountGraphics == 0) { dForm.AddGraphic(g, -5f, 5f, DrawModes.DrawLines, Color.Green); dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Gray); dForm.AddGraphic(bisectress, -5f, 5f, DrawModes.DrawLines, Color.Blue); } res = FindRoot.StillPointMethod(h, x, eps, out lines, true); break; #endregion #region Method Bisection case SolutionMethod.Bisection: if (dForm.CountGraphics == 0) { dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); } res = FindRoot.Bisection(h, x, x + step, eps, out lines, true); break; #endregion //#region Method Newtone-Rafson //case SolutionMethod.NewtoneRafson: // MessageBox.Show("How to evaluate dh/dx?"); // return; // if ( dForm.CountGraphics == 0 ) // dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); // res = FindRoot.NewtoneRafson(f, df, x, eps, out lines); // break; //#endregion #region Method Cuttings case SolutionMethod.Cuttings: if (dForm.CountGraphics == 0) { dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); } res = FindRoot.Cuttings(h, x, x + step, eps, out lines, true); break; #endregion //#region Method Hord //case SolutionMethod.Hord: // if ( dForm.CountGraphics == 0 ) // dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); // res = FindRoot.Hord(f, d2f, x, x + step, eps, out lines); // break; //#endregion #region Method Muller case SolutionMethod.Muller: if (dForm.CountGraphics == 0) { dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); } res = FindRoot.Muller(h, x - step, x, x + step, eps, ref dForm, true); lines = null; break; #endregion default: return; } if (lines != null) { foreach (PointF[] pts in lines) { dForm.AddPolygon(Color.Red, DrawModes.DrawLines, pts); } } if (double.IsNaN(res)) { continue; } if ((res > x2) || (res < x1)) { continue; } roots.Add(res); } roots.Sort(); foreach (double p in roots) { listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = " + p.ToString("F16")); listY.Items.Add("y" + (listY.Items.Count + 1) + " = " + f(p).ToString("F16")); } dForm.Show(); dForm.Update2(); }
private void buttonSolve_Click(object sender, EventArgs e) { if (checkAutoSearch.Checked) { AutoFindRoots(); return; } double p0, p1, p2; double eps; errorProvider.Clear(); try { eps = float.Parse(textE.Text); } catch (FormatException) { errorProvider.SetError(textE, "Wrong float number"); return; } if (eps < 0) { errorProvider.SetError(textE, "Has to be > 0"); return; } double res = double.NaN; DekartForm dForm = null; List <PointF[]> lines = null; switch (selectedSolMeth) { #region Method Still Point case SolutionMethod.StillPoint: try { p0 = float.Parse(textP0.Text); } catch (FormatException) { errorProvider.SetError(textP0, "Wrong float number"); return; } dForm = new DekartForm(100, 100, 320, 300); dForm.Size = new Size(700, 500); dForm.AddGraphic(g, -5f, 5f, DrawModes.DrawLines, Color.Green); dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Gray); dForm.AddGraphic(bisectress, -5f, 5f, DrawModes.DrawLines, Color.Blue); res = FindRoot.StillPointMethod(f, p0, eps, out lines, false); break; #endregion #region Method Bisection case SolutionMethod.Bisection: try { p0 = float.Parse(textP0.Text); } catch (FormatException) { errorProvider.SetError(textP0, "Wrong float number"); return; } try { p1 = float.Parse(textP1.Text); } catch (FormatException) { errorProvider.SetError(textP1, "Wrong float number"); return; } dForm = new DekartForm(100, 100, 175, 300); dForm.Use_IsVisible = false; dForm.Size = new Size(350, 600); dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); res = FindRoot.Bisection(f, p0, p1, eps, out lines, false); break; #endregion #region Method Newtone-Rafson case SolutionMethod.NewtoneRafson: try { p0 = float.Parse(textP0.Text); } catch (FormatException) { errorProvider.SetError(textP0, "Wrong float number"); return; } dForm = new DekartForm(100, 100, 300, 300); dForm.Size = new Size(750, 600); dForm.Use_IsVisible = false; dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); res = FindRoot.NewtoneRafson(f, df, p0, eps, out lines, false); break; #endregion #region Method Cuttings case SolutionMethod.Cuttings: try { p0 = float.Parse(textP0.Text); } catch (FormatException) { errorProvider.SetError(textP0, "Wrong float number"); return; } try { p1 = float.Parse(textP1.Text); } catch (FormatException) { errorProvider.SetError(textP1, "Wrong float number"); return; } dForm = new DekartForm(100, 100, 300, 300); dForm.Size = new Size(750, 600); dForm.Use_IsVisible = false; dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); res = FindRoot.Cuttings(f, p0, p1, eps, out lines, false); break; #endregion #region Method Hord case SolutionMethod.Hord: try { p0 = float.Parse(textP0.Text); } catch (FormatException) { errorProvider.SetError(textP0, "Wrong float number"); return; } try { p1 = float.Parse(textP1.Text); } catch (FormatException) { errorProvider.SetError(textP1, "Wrong float number"); return; } dForm = new DekartForm(100, 100, 300, 300); dForm.Size = new Size(750, 600); dForm.Use_IsVisible = false; dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); res = FindRoot.Hord(f, d2f, p0, p1, eps, out lines, false); break; #endregion #region Method Muller case SolutionMethod.Muller: try { p0 = float.Parse(textP0.Text); } catch (FormatException) { errorProvider.SetError(textP0, "Wrong float number"); return; } try { p1 = float.Parse(textP1.Text); } catch (FormatException) { errorProvider.SetError(textP1, "Wrong float number"); return; } try { p2 = float.Parse(textP2.Text); } catch (FormatException) { errorProvider.SetError(textP2, "Wrong float number"); return; } dForm = new DekartForm(100, 100, 300, 300); dForm.Size = new Size(750, 600); dForm.Use_IsVisible = false; dForm.AddGraphic(f, -5f, 5f, DrawModes.DrawLines, Color.Green); res = FindRoot.Muller(f, p0, p1, p2, eps, ref dForm, false); lines = null; break; #endregion default: return; } #region Print results if (lines != null) { foreach (PointF[] pts in lines) { dForm.AddPolygon(Color.Red, DrawModes.DrawLines, pts); } } dForm.Show(); dForm.Update2(); listRoots.Items.Clear(); listY.Items.Clear(); if (double.IsNaN(res)) { MessageBox.Show("Корни не найдены."); return; } listRoots.Items.Add("x" + (listRoots.Items.Count + 1) + " = " + res.ToString("F16")); listY.Items.Add("y" + (listY.Items.Count + 1) + " = " + f(res).ToString("F16")); #endregion }