Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using MathNet.Numerics;
- using MathNet.Numerics.RootFinding;
- using MathNet.Symbolics;
- using static System.Collections.Specialized.BitVector32;
- using Expr = MathNet.Symbolics.SymbolicExpression;
- namespace RootFindingMethods
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- string functionString = ReadFunctionFromUser();
- double a = ReadDoubleFromUser("Enter the left endpoint a: ");
- double b = ReadDoubleFromUser("Enter the right endpoint b: ");
- double tolerance = ReadDoubleFromUser("Enter the tolerance TOL: ");
- int maxIterations = ReadIntFromUser("Enter the maximum iterations NMAX: ");
- var expression = Expr.Parse(functionString);
- var function = expression.Compile("x");
- Func<double, double> f = x => function(x);
- // Compute the derivative of the function
- var derivativeExpression = Expr.Differentiate(expression, "x");
- var derivativeFunction = derivativeExpression.Compile("x");
- Func<double, double> df = x => derivativeFunction(x);
- Console.WriteLine("Choose a method:");
- Console.WriteLine("1. Bisection");
- Console.WriteLine("2. Newton-Raphson");
- int methodChoice = ReadIntFromUser("Enter your choice (1 or 2): ");
- double root;
- int iterations;
- switch (methodChoice)
- {
- case 1: // Bisection method
- (root, iterations) = BisectionMethod(f, a, b, tolerance, maxIterations);
- break;
- case 2: // Newton-Raphson method
- double initialGuess = ReadDoubleFromUser("Enter the initial guess: ");
- (root, iterations) = NewtonRaphsonMethod(f, df, initialGuess, tolerance, maxIterations);
- break;
- default:
- throw new ArgumentException("Invalid method choice.");
- }
- if (double.IsNaN(root))
- {
- Console.WriteLine("Method failed.");
- }
- else
- {
- Console.WriteLine("The root is approximately " + root);
- Console.WriteLine("Number of iterations: " + iterations);
- }
- }
- catch (MathNet.Numerics.NonConvergenceException ex)
- {
- Console.WriteLine(ex.Message);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error: " + ex.Message);
- }
- }
- private static (double, int) BisectionMethod(Func<double, double> f, double a, double b, double tolerance, int maxIterations)
- {
- int iterations = 0; // Initialize the iteration count
- double root = Bisection.FindRoot(f, a, b, tolerance);
- while (iterations < maxIterations)
- {
- // Calculate midpoint
- double c = (a + b) / 2;
- // Check if root is found within tolerance
- if (Math.Abs(f(c)) < tolerance)
- {
- root = c;
- break;
- }
- // Update interval
- if (f(a) * f(c) < 0)
- b = c;
- else
- a = c;
- iterations++;
- }
- return (root, iterations);
- }
- private static (double, int) NewtonRaphsonMethod(Func<double, double> f, Func<double, double> df, double initialGuess, double tolerance, int maxIterations)
- {
- int iterations = 0; // Initialize the iteration count
- double root = initialGuess;
- while (iterations < maxIterations)
- {
- double fValue = f(root);
- double dfValue = df(root);
- // Update root using Newton-Raphson formula
- root = root - fValue / dfValue;
- // Check if root is found within tolerance
- if (Math.Abs(f(root)) < tolerance)
- {
- break;
- }
- iterations++;
- }
- return (root, iterations);
- }
- private static string ReadFunctionFromUser()
- {
- Console.Write("Enter the function f(x): ");
- string functionString = Console.ReadLine();
- if (string.IsNullOrEmpty(functionString))
- {
- throw new ArgumentException("Function string cannot be null or empty.");
- }
- return functionString;
- }
- private static double ReadDoubleFromUser(string prompt)
- {
- Console.Write(prompt);
- if (!double.TryParse(Console.ReadLine(), out double value))
- {
- throw new FormatException("Invalid input. Please enter a valid number.");
- }
- return value;
- }
- private static int ReadIntFromUser(string prompt)
- {
- Console.Write(prompt);
- if (!int.TryParse(Console.ReadLine(), out int value))
- {
- throw new FormatException("Invalid input. Please enter a valid integer.");
- }
- return value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement