Advertisement
JohnathanMayhem

Prog

Dec 9th, 2022
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. internal class Program
  2.     {
  3.         public delegate double Function(double x);
  4.  
  5.         public delegate double Method(Function d, double eps);
  6.  
  7.         static public double func1(double x)
  8.         {
  9.             return Math.Exp(x) - 4 * (1 - Math.Pow(x, 2));
  10.         }
  11.  
  12.         static public double func2(double x)
  13.         {
  14.             return 1 - (1 / Math.Log(x));
  15.         }
  16.  
  17.         static public double dihotomy(Function f, double eps)
  18.         {
  19.             double left = 100;
  20.             double right = -100;
  21.             double c = left;
  22.             int iteration = 0;
  23.             while (right - left > eps)
  24.             {
  25.                 ++iteration;
  26.                 c = (left + right) / 2;
  27.                 if (f(left) * f(c) < 0)
  28.                 {
  29.                     right = c;
  30.                 }
  31.                 else
  32.                 {
  33.                     if (f(left) * f(c) > 0)
  34.                         left = c;
  35.                     else
  36.                         return c;
  37.                 }
  38.                
  39.                
  40.             }
  41.  
  42.             return c;
  43.         }
  44.  
  45.         static public double chords(Function f, double eps)
  46.         {
  47.             {
  48.                 int iteration = 0;
  49.                 double x_next = 0;
  50.                 double tmp;
  51.                 double x_prev = -100;
  52.                 double x_curr = 100;
  53.  
  54.                 do
  55.                 {
  56.                     ++iteration;
  57.                     tmp = x_next;
  58.                     x_next = x_curr - f(x_curr) * (x_prev - x_curr) / (f(x_prev) - f(x_curr));
  59.                     x_prev = x_curr;
  60.                     x_curr = tmp;
  61.                 } while (Math.Abs(x_next - x_curr) > eps);
  62.  
  63.                 return x_next;
  64.             }
  65.         }
  66.  
  67.         static public double newton(Function f, double eps)
  68.         {
  69.             int iteration = 1;
  70.             double x1 = 0;
  71.  
  72.             double delta = 1e-9;
  73.  
  74.             double der = (f(x1 + delta) - f(x1)) / delta;
  75.  
  76.             double x2 = x1 - (f(x1) / der);
  77.    
  78.             while (Math.Abs(x2-x1) > eps)
  79.             {
  80.                 iteration += 1;
  81.                 x1 = x2;
  82.                 der = (f(x1 + delta) - f(x1)) / delta;
  83.                 x2 = x1 - (f(x1) / der);
  84.             }
  85.  
  86.             return x2;
  87.         }
  88.  
  89.         public static void Main(string[] args)
  90.         {
  91.             Function f = func1;
  92.             Method m = newton;
  93.             double k = m(f, 1e-10);
  94.             Console.WriteLine(k);
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement