public double Solve(Equasion eq, double a, double b) { MathFunction func = eq.Left - eq.Right; if (func.Calculate(a) * func.Calculate(b) > 0) { throw new Exception("Method cannot be applied!"); } if (Math.Abs(func.Calculate(a)) <= epsilanIter) { return(a); } if (Math.Abs(func.Calculate(b)) <= epsilanIter) { return(b); } double xn = (a + b) / 2; while (Math.Abs(func.Calculate(xn)) > epsilanIter) { if (func.Calculate(xn) * func.Calculate(a) < 0) { b = xn; } else if (func.Calculate(xn) * func.Calculate(b) < 0) { a = xn; } xn = (a + b) / 2; } return(xn); }
private void Sub_Click(object sender, EventArgs e) { Equal.Focus(); if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } if (Equasion.Text.Equals("0")) { Equasion.ResetText(); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "-"); }
public double Solve(Equasion eq, double a, double b) { MathFunction func = eq.Left - eq.Right; MathFunction der1 = func.Derivative(1); if (!IsSuitable(der1, a, b)) { throw new Exception("Method can not be applied!"); } MathFunction ksi = IterationFunc(der1, a, b); double x0 = InitialX(a, b); return(GenericIterationMethod(func, ksi, a, b, x0)); }
public double Solve(Equasion eq, double a, double b) { MathFunction func = eq.Left - eq.Right; MathFunction der1 = func.Derivative(1), der2 = func.Derivative(2); if (!IsSuitable(der1, a, b) || !IsSuitable(der2, a, b)) { throw new Exception("Method can not be applied!"); } double c = SuitableRandomArgumnent((double x) => { return(func.Calculate(x) * der2.Calculate(x) > 0); }, a, b); MathFunction ksi = IterationFunc(func, a, b, c); double x0 = InitialX(func, a, b, c); return(GenericIterationMethod(func, ksi, a, b, x0)); }
public void Test() { Dictionary <string, Variable> variables = Equasion.CreateVariables(new string[] { "a", "b", "c" }, new decimal[] { 1, 1, 0 }); Constant pi = Constant.PI; Variable x = new Variable("x"); //Equasion quad = new Equasion(variables["a"] * x ^ 2 + variables["b"] * x + variables["c"], 0); //System.Console.WriteLine(quad); //quad.SolveFor(x); //System.Console.WriteLine(x); Variable test = variables["a"]; test.sin(); test.ToString(); //System.Console.WriteLine(test); }
private void Form1_KeyDown(object sender, KeyEventArgs e) { Equal.Focus(); if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Text = Convert.ToString(e.KeyValue); if (e.Modifiers == Keys.Enter) { Answer.Text = Calculate(Equasion.Text); if (Answer.Text == "ERROR") { return; } Equasion.Text = Answer.Text; } if (e.Shift) { switch (e.KeyValue) { case 27: Close(); break; case 187: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "*"); return; case 55: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "/"); return; } } switch (e.KeyValue) { case 48: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "0"); break; case 49: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "1"); break; case 50: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "2"); break; case 51: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "3"); break; case 52: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "4"); break; case 53: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "5"); break; case 54: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "6"); break; case 55: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "7"); break; case 56: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "8"); break; case 57: if (Equasion.Text == "0") { Equasion.Text = ""; } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "9"); break; case 187: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "+"); break; case 107: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "+"); break; case 189: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "-"); break; case 109: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } if (Equasion.Text.Equals("0")) { Equasion.ResetText(); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "-"); break; case 106: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "*"); break; case 111: if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/") || Equasion.Text.EndsWith(",")) { Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "/"); break; case 188: if (Equasion.Text.Split(new char[] { '+', '-', '*', '/' })[(Equasion.Text.Split(new char[] { '+', '-', '*', '/' }).Length - 1)].Contains(",")) { return; } if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/")) { Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "0"); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, ","); break; case 190: if (Equasion.Text.Split(new char[] { '+', '-', '*', '/' })[(Equasion.Text.Split(new char[] { '+', '-', '*', '/' }).Length - 1)].Contains(",")) { return; } if (Equasion.Text.EndsWith("+") || Equasion.Text.EndsWith("-") || Equasion.Text.EndsWith("*") || Equasion.Text.EndsWith("/")) { Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, "0"); } Equasion.Text = Equasion.Text.Insert(Equasion.Text.Length, ","); break; case 27: WindowState = FormWindowState.Minimized; break; case 46: Equasion.Text = "0"; break; case 8: if (Equasion.Text.Length == 1) { Equasion.Text = "0"; return; } Equasion.Text = Equasion.Text.Remove(Equasion.Text.Length - 1); break; } }