Bob103

C#_Method_Chords

May 4th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3.  
  4. namespace ConsoleApplication2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int count;
  11.             double x0 = 1;
  12.             double x1 = 10;
  13.             double e = 0.001;
  14.             Stopwatch sw = new Stopwatch();
  15.             sw.Start();
  16.             double x = method_chord(x0, x1, e,out count);
  17.             sw.Stop();
  18.             Console.WriteLine("Корень = {0}\n{1}",x,count);
  19.             Console.WriteLine(sw.Elapsed);
  20.         }
  21.  
  22.         public static double method_chord(double x_prev, double x_curr, double e,out int count)
  23.         {
  24.             count = 0;
  25.             double x_next = 0;
  26.             double tmp;
  27.             while (Math.Abs(x_next - x_curr) > e)
  28.             {
  29.                     tmp = x_next;
  30.                     x_next = x_curr - f(x_curr) * (x_prev - x_curr) / (f(x_prev) - f(x_curr));
  31.                     x_prev = x_curr;
  32.                     x_curr = tmp;
  33.                     count++;
  34.             }
  35.             return x_next;
  36.         }
  37.  
  38.         public static double f(double x)
  39.         {
  40.             return Math.Pow(x, 2) - 2 * x + 1;
  41.             //return Math.Pow(x, 3) - 18 * x - 83;
  42.            
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment