Какие существуют способы решения простых уравнений? - C#
Формулировка задачи:
уравнения вида y = x^2 * x и т.п
можно ли решить не используя стандартных функций как pow т.п
ну и естественно без переумножений
только решение не пишите прошу) нужно только пнуть в нужное направление, ссылки на методы может быть)
Решение задачи: «Какие существуют способы решения простых уравнений?»
textual
Листинг программы
namespace nonlinear { class Program { public delegate double Function(double x); #region Linear Search public static double LinearIncrementalSearch(Function f, double xstart, double deltaX, int nMaxInc) { double x = xstart; double fstart = f(x); double fx = fstart; double fProd = 0; for (int i = 0; i < nMaxInc; i++) { x = xstart + i * deltaX; fx = f(x); fProd = fstart * fx; if (fProd < 0) break; } if (fProd > 0) throw new Exception("Solution not found!"); else { return x = x - deltaX * fx / (fx - f(x - deltaX)); } } #endregion #region Bisection Method public static double BisectionMethod(Function f, double a, double b, double epsilon) { double x1 = a; double x2 = b; double fb = f(b); while (Math.Abs(x2 - x1) > epsilon) { double midpt = 0.5 * (x1 + x2); if (fb * f(midpt) > 0) x2 = midpt; else x1 = midpt; } return x2 - (x2 - x1) * f(x2) / (f(x2) - f(x1)); } #endregion #region Secant Method public static double SecantMethod(Function f, double a, double b, double epsilon) { double x1 = a; double x2 = b; double fb = f(b); while (Math.Abs(f(x2)) > epsilon) { double mpoint = x2 - (x2 - x1) * fb / (fb - f(x1)); x1 = x2; x2 = mpoint; fb = f(x2); } return x2; } #endregion #region False Position Method public static double FalsePositionMethod(Function f, double a, double b, double epsilon) { double x1 = a; double x2 = b; double fb = f(b); while (Math.Abs(x2 - x1) > epsilon) { double xpoint = x2 - (x2 - x1) * f(x2) / (f(x2) - f(x1)); if (fb * f(xpoint) > 0) x2 = xpoint; else x1 = xpoint; if (Math.Abs(f(xpoint)) < epsilon) break; } return x2 - (x2 - x1) * f(x2) / (f(x2) - f(x1)); } #endregion #region Fixed Point Method public static double FixedPointMethod(Function f, double x0, double epsilon, int nMaxIter) { double x1 = x0; double x2 = x0; double currEpsilon = 0.0; for (int i = 0; i < nMaxIter; i++) { x2 = f(x1); currEpsilon = Math.Abs(x1 - x2); x1 = x2; if (currEpsilon < epsilon) break; } if (currEpsilon > epsilon) { throw new Exception("Solution not found!"); } return x1; } #endregion #region Newton-Raphson Method public static double NewtonRaphsonMethod(Function f, Function fprime, double x0, double epsilon) { double f0 = f(x0); double x = x0; while (Math.Abs(f(x)) > epsilon) { x -= f0 / fprime(x); f0 = f(x); } return x; } #endregion #region Test Functions static double F(double x) { //Setup for test case: x^3 - 5x + 3 = 0 return x * x * x - 5.0 * x + 3.0; } static double Ffixpt(double x) { //Setup for test case: x^2 + 2x - 35 = 0 //Test function = sqrt(35 - 2x) return Math.Sqrt(35.0 - 2.0 * x); } static double F1(double x) { return Math.Sin(x) - x * x * x * x; } static double F1prime(double x) { return Math.Cos(x) - 4.0 * x * x * x; } #endregion static void Main(string[] args) { try { Console.WriteLine("\n\nTesting Linear Incremental Search Methodn"); double deltaX = 0.01; int n = 500; double x = -4.0; for (int i = 1; i <= 3; i++) { x = LinearIncrementalSearch(F, x, deltaX, n); Console.WriteLine("\nSolution " + i.ToString() + " = " + x.ToString()); Console.WriteLine("Solution confirmation: f(x) = " + F(x).ToString()); } Console.ReadLine(); Console.WriteLine("\n\nTesting Bisection Method\n"); x = BisectionMethod(F, 1.0, 2.0, 0.0001); Console.WriteLine("Solution from the bisection method: " + x.ToString()); Console.WriteLine("Solution confirmation: f(x) = " + F(x).ToString()); Console.ReadLine(); Console.WriteLine("\n\nTesting Secant Method\n"); x = SecantMethod(F, 1.0, 1.5, 0.0001); Console.WriteLine("Solution from the secant method: " + x.ToString()); Console.WriteLine("Solution confirmation: f(x) = " + F(x).ToString()); Console.ReadLine(); Console.WriteLine("\n\nTesting False Position Method\n"); x = FalsePositionMethod(F, 1.0, 2.0, 0.0001); Console.WriteLine("Solution from the false position method: " + x.ToString()); Console.WriteLine("Solution confirmation: f(x) = " + F(x).ToString()); Console.ReadLine(); Console.WriteLine("\n\nTesting Fixed Point Method\n"); double tol = 0.0001; n = 10000; double x0 = 1.6; x = FixedPointMethod(Ffixpt, x0, tol, n); Console.WriteLine("solution from the fixed point method: " + x.ToString()); Console.WriteLine("Expected solution = 5.00"); Console.ReadLine(); Console.WriteLine("\n\nTesting Testing Newton-Raphson Method\n"); x = NewtonRaphsonMethod(F1, F1prime, 1.0, 0.0001); Console.WriteLine("Solution from the Newton-Raphson method: " + x.ToString()); Console.WriteLine("Solution confirmation: f(x) = " + F1(x).ToString()); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("Fatal error: " + ex.Message); Console.ReadLine(); } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д