Mathematical Recipes: Bisection Method in C#

sumitmehta

New Member
Messages
215
Reaction score
1
Points
0
Hye

Bisection method is the most basic method in root computations. Here is the full implementation in C#.

Credits or reps are welcome.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Bisection_Method
{    
    public partial class FrmBisection : Form
    {
        struct Polynomial
        {
            public int Exponent;
            public int Coefficient;
            public bool isVariable;
        };

        private Polynomial[] Poly;
        private int polyCount = 0;

        public FrmBisection()
        {
            //This constructor initializes the polynomial
            //It can also be initialized at runtime

            InitializeComponent();
            polyCount = 0;

            initPoly(2);

            //This polynomial is equal to
            //3x^2 - x^3 + 27
            addTerm(1, 2, true);
            addTerm(-10,1, false);
        }

        public void initPoly(int termCount) {

            Poly = new Polynomial[termCount];
        }
        public void addTerm(int Coefficient, int Exponent, bool isVariable) {

            Poly[polyCount].Coefficient = Coefficient;
            Poly[polyCount].Exponent = Exponent;
            Poly[polyCount].isVariable = isVariable;

            polyCount++;
        }
        public float CalculateValue(float value) {

            float val=0;
            float e=1;

            for (int i = 0; i < polyCount; i++) {

                if (Poly[i].isVariable==true)
                {
                    for (int j = 0; j < Poly[i].Exponent; j++)
                        e *= value;
                    
                    e *= Poly[i].Coefficient;
                    val += e;
                    
                }
                else {
                    val += (float) Poly[i].Coefficient;
                }
                e = 1;
            }
            return val;
        }
        public int[] FindApproxRoots() {

            int[] roots = new int[2];
            roots[0]=0;

            float c = CalculateValue(0);

            for (int i = 0; ; i++) {

                float v = CalculateValue(i);

                if ((v < 0 && c < 0) | (v > 0 && c > 0))
                {
                    c = v;
                    roots[0] = i;
                    continue;
                }
                else if ((c < 0 && v > 0) | (c > 0 && v < 0))
                {
                    roots[1] = i;
                    break;
                }
            }
            return roots;
        }
        public float Bisection() {

            float r=0;
            int[] roots = FindApproxRoots();
            int i=0;

            float x1 = (float)roots[0];
            float x2 = (float)roots[1];
            float v1=CalculateValue(x1);
            float v2=CalculateValue(x2);
            float v;
            float mid;

            mid = (x1 + x2) / 2;
            v = CalculateValue(mid);

            while (Math.Abs(v-v1)>0.001f)
            {
                if ((v > 0 && v1 < 0)|(v<0 && v1>0))
                {
                    x2 = mid;
                    v2 = v;
                }
                else
                {
                    x1 = mid;
                    v1 = v;
                }
                r = mid;

                mid = (x1 + x2) / 2;
                v = CalculateValue(mid);
                i++;
            }
            return r;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Main Program
            float v = Bisection();
            float c = CalculateValue(v);
            MessageBox.Show("Root is: " + v.ToString() + "\nVerification of f(" + v.ToString() + ") = " + c.ToString());
        }
    }
}
 
Top