Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class GoldenSectionMethod
- {
- public static readonly double Phi = (1 + Math.Sqrt(5)) / 2;
- public readonly double Accuracy;
- public readonly Func<double, double> Func;
- public GoldenSectionMethod(Func<double, double> func, double accuracy = 0.01)
- {
- if (func is null)
- throw new Exception("Function is null");
- if (accuracy <= 0)
- throw new Exception("Wrong accuracy");
- Func = func;
- Accuracy = accuracy;
- }
- public double FindMin(double start, double end)
- {
- int counter = 0;
- while (Math.Abs(end - start) >= Accuracy)
- {
- var x1 = end - (end - start) / Phi;
- var x2 = start + (end - start) / Phi;
- if (Func(x1) >= Func(x2))
- start = x1;
- else
- end = x2;
- counter++;
- }
- Console.WriteLine($"Number of iterations for the First Part of GoldenSection method {counter} ");
- return (start + end) / 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement