Advertisement
V4L304

Untitled

Dec 17th, 2021
1,351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using MathConsoleApplication;
  2. using org.mariuszgromada.math.mxparser;
  3.  
  4. public class Bisection
  5. {
  6.     private static float EPSILON = (float)0.01;
  7.  
  8.     // An example function whose
  9.     // solution is determined using
  10.     // Bisection Method. The function
  11.     // is x^3 - x^2 + 2
  12.     private static double func(string expression, double x)
  13.     {
  14.         expression = SobstitutionClass.replaceX(expression, x);
  15.         Expression e = new Expression(expression);
  16.         return e.calculate();
  17.     }
  18.  
  19.     public static void bisection(string expression,
  20.                           double a,
  21.                           double b)
  22.     {
  23.         if (func(expression, a) * func(expression, b) >= 0)
  24.         {
  25.             Console.WriteLine("a e b non sono corretti");
  26.             return;
  27.         }
  28.  
  29.         double c = a;
  30.         while ((b - a) >= EPSILON)
  31.         {
  32.             // Find middle point
  33.             c = (a + b) / 2;
  34.  
  35.             // Check if middle
  36.             // point is root
  37.             if (func(expression, c) == 0.0)
  38.                 break;
  39.  
  40.             // Decide the side
  41.             // to repeat the steps
  42.             else if (func(expression, c) * func(expression, a) < 0)
  43.                 b = c;
  44.             else
  45.                 a = c;
  46.         }
  47.         Console.WriteLine($"la radice equivale a {Math.Round(c, 15)}");
  48.     }
  49. }
  50.  
  51.  
  52. namespace MathConsoleApplication
  53. {
  54.     public class SobstitutionClass
  55.     {
  56.         public static string replaceX(string expression, double x)
  57.         {
  58.             for (int i = 0; i < expression.Length; i++)
  59.             {
  60.                 if (expression[i] == 'x')
  61.                 {
  62.                     if (i - 1 >= 0)
  63.                     {
  64.                         if (expression[i - 1] == '(' || expression[i - 1] == '+' || expression[i - 1] == '-' || expression[i - 1] == '*' || expression[i - 1] == '/')
  65.                         {
  66.                             expression = expression.Remove(i, "x".Length - 1);
  67.                             expression = expression.Insert(i, x.ToString());
  68.                         }
  69.                         else
  70.                         {
  71.                             string tmpRepace = $"*{x.ToString()}";
  72.                             expression = expression.Remove(i, "x".Length);
  73.                             expression = expression.Insert(i, tmpRepace);
  74.                         }
  75.                     }
  76.                     else
  77.                     {
  78.                         expression = expression.Remove(i, "x".Length - 1).Insert(i, x.ToString());
  79.                     }
  80.                     {
  81.                     }
  82.                 }
  83.             }
  84.             return expression;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement