Implementation of Chord Method written in C#.

IN
-3
0
0.00001
OUT
X: -1 f(x): -2
X: -2 f(x): -1
X: -2.33333333333333 f(x): -0.222222222222222
X: -2.4 f(x): -0.0400000000000003
X: -2.41176470588235 f(x): -0.0069204152249126
X: -2.41379310344828 f(x): -0.0011890606420929
X: -2.41414141414141 f(x): -0.00020406081012081
X: -2.41420118343195 f(x): -3.50127796636679E-05
using System;

// © 2018 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
namespace Chord_Method_CS
{
    class Program
    {
        static void Main(string[] args)
        {
            double intervalBegin;
            double intervalEnd;
            double x;
            double precision;

            Console.Write("Enter beginning of interval: ");
            intervalBegin = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter end of interval: ");
            intervalEnd = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter precision of method: ");
            precision = Convert.ToDouble(Console.ReadLine());

            if (Function(intervalBegin) * Function(intervalEnd) > 0.0D)
            {
                Console.Write("Function has same signs at ends of interval");
                return;
            }

            x = (intervalBegin * Function(intervalEnd) - intervalEnd * Function(intervalBegin)) / (Function(intervalEnd) - Function(intervalBegin));
            while (Math.Abs(Function(x)) > precision)
            {
                Console.WriteLine("X: " + x + " f(x): " + Function(x));
                if (Function(intervalBegin) * Function(x) < 0)
                {
                    x = (x * Function(intervalBegin) - intervalBegin * Function(x)) / (Function(intervalBegin) - Function(x));
                }
                else
                {
                    x = (x * Function(intervalEnd) - intervalEnd * Function(x)) / (Function(intervalEnd) - Function(x));
                }
            }

            Console.ReadKey(); //Wait
        }

        static double Function(double x) //x^2 + 2x -1
        { 
            return x * (x + 2) - 1;
        }
    }
}

 



C# Chord Method
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close