Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int count;
- double x0 = 1;
- double x1 = 10;
- double e = 0.001;
- Stopwatch sw = new Stopwatch();
- sw.Start();
- double x = method_chord(x0, x1, e,out count);
- sw.Stop();
- Console.WriteLine("Корень = {0}\n{1}",x,count);
- Console.WriteLine(sw.Elapsed);
- }
- public static double method_chord(double x_prev, double x_curr, double e,out int count)
- {
- count = 0;
- double x_next = 0;
- double tmp;
- while (Math.Abs(x_next - x_curr) > e)
- {
- tmp = x_next;
- x_next = x_curr - f(x_curr) * (x_prev - x_curr) / (f(x_prev) - f(x_curr));
- x_prev = x_curr;
- x_curr = tmp;
- count++;
- }
- return x_next;
- }
- public static double f(double x)
- {
- return Math.Pow(x, 2) - 2 * x + 1;
- //return Math.Pow(x, 3) - 18 * x - 83;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment