Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using YuKu.MathExpression;
- public class BisectionClass
- {
- private static double Func(string expression, double x)
- {
- Func<double, double> func = expression.AddModule(typeof(Math)).AddParameter("x").Compile<Func<double, double>>();
- double res = func(x);
- return res;
- }
- public static void Bisection(string expression,
- double a,
- double b)
- {
- double c = a;
- Stopwatch stopWatch = new();
- stopWatch.Start();
- double count = 0;
- if (b < a)
- {
- double tmp = a;
- a = b;
- b = tmp;
- }
- while ((b - a) >= 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001)
- {
- c = (a + b) / 2;
- if (Func(expression, c) == 0.0)
- break;
- else if (Func(expression, c) * Func(expression, a) < 0)
- b = c;
- else
- a = c;
- count++;
- }
- Console.WriteLine(count);
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine($"la radice equivale a {Math.Round(c, 15)} in {elapsedTime}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement