Posts Tagged ‘bisection method’

This file contains one solution from

Bisection

Secant

Regular False

Newton

Fixed Point Iteration

Synthetic Division

QD Method

Link for online file: https://docs.google.com/spreadsheet/ccc?key=0Arryl0I1PQbVdEFuM1k0YVBTZF9SS21SUUpvZjc3aXc

x1 = 1
x2 = 2

fx1 = (x1^3) + (x1^2) - (3*x1) - 3;
fx2 = (x2^3) + (x2^2) - (3*x2) - 3;

while(abs(x1 -x2)> 0.00001)
    x3 = (x1 +x2)/2;
    fx3 = (x3^3) + (x3^2) - (3*x3) - 3;

    if (fx1 * fx3 < 0)
        x2 = x3;
        fx2 = fx3;
    else
        x1 = x3;
        fx1 = fx3;
    end

end

x3

Bisection Method // Matlab code

Posted: January 31, 2012 by muhammadakif in Algorithms
Tags: ,
format long;
x = sym('x');
expr = input('Please enter the equation : ');
fx = sym(expr);

x1 = input('Please enter value of X1 : ');
x2 = input('Please enter value of X2 : ');
iter = input('Please enter number of Iterations : ');

fx1 = subs(fx,x,x1);
fx2 = subs(fx,x,x2);

x3 = 0;
counter = 0;

fid=fopen('c:\bisection.csv','w');
fprintf(fid,'%s\n','Iteration,x1,x2,x3,F(x1),F(x2),F(x3),Max Errors');

if fx1*fx2 &gt;= 0
    message = 'x1 and x2 values are not valid for desired root';
    disp(message)
else 

   while ((counter == 0 || abs(x1-x2) &gt; 0.000001 || subs(fx,x,x3) == 0) &amp;&amp; counter &lt;= iter)
       counter = counter + 1;

       x3 = (x1 + x2) / 2;   

       fprintf(fid,'%d,',counter);
       fprintf(fid,'%5.8f,',x1);
       fprintf(fid,'%5.8f,',x2);
       fprintf(fid,'%5.8f,',x3);
       fprintf(fid,'%5.8f,',subs(fx,x,x1));
       fprintf(fid,'%5.8f,',subs(fx,x,x2));
       fprintf(fid,'%5.8f,',subs(fx,x,x3));
       fprintf(fid,'%5.8f\n',abs((x2-x1)/(2^counter)));

       x3 = (x1 + x2) / 2;
       if subs(fx,x,x3) * subs(fx,x,x1)&lt; 0
           x2 = x3;
       else
           x1 = x3;
       end    
   end    
end    

sprintf('The root of the equation using Bisection method is :: %d', x3)

fclose(fid);

using System;

class Bisection
{
    public static void Main()
    {
        float fx1,fx2,fx3;
        float x1, x2;

        Console.Write("Enter Value for X1 :");
        x1 = int.Parse(Console.ReadLine());
        fx1=fx(x1);

        Console.Write("Enter Value for X2 :");
        x2 = int.Parse(Console.ReadLine());
        fx2 = fx(x2);

        Console.Write("Number Of Itrations =");
        int itre = int.Parse(Console.ReadLine());

        float m = (x1 + x2) / 2;
        fx3=fx(m);
        int counter = 0;
        while (Math.Abs(x1 - x2) > 0.000001 || m != 0)
        {
            if (counter == itre)
            {
                break;
            }

            Console.Write("x1 = " + x1);
            Console.Write(" :x2 = " + x2);
            Console.Write(" :m = " + m);
            Console.Write(" :Fx1 = " + fx1);
            Console.Write(" :Fx2 = " + fx2);
            Console.WriteLine(" :Fx3 = " + fx3);
            counter=counter + 1;
            if(m==0)
            {
                break;
            }
            else if (fx1 * fx3 < 0)
            {
                x2 = m;
            }
            else
            {
                x1 = m;
            }
            m = (x1 + x2) / 2;
            fx1 = fx(x1);
            fx2 = fx(x2);
            fx3 = fx(m);
        }
        Console.WriteLine(m);
        Console.WriteLine(counter);
        Console.ReadLine();

    }

    private static float fx(float x)
    {
        x = ((float)Math.Pow(x, 3)) + ((float)Math.Pow(x, 2)) - 3 * x - 3;
        return x;
    }
}

#include<iostream.h>
#include<conio.h>
#include<math.h>

float f (float x)
{
	float fx1;
	fx1 = pow(x,3) + pow(x,2) - (3*x) - 3;
	return (fx1);
}

void main()
{
	float x1,x2,x3;
	int count = 0;
	int iter;

	cout <<"Enter x1 = ";
	cin >> x1;
	cout <<"Enter x2 = ";
	cin >> x2;
	cout <<"Enter number of iterations = ";
	cin >> iter;

//	int fx1 = (x1^3) + (x1^2) - (3x1) - 3;
//	int fx2 = (x2^3) + (x2^2) - (3x2) - 3;
	do
	{
		if(count == iter)
		{
			break;
		}

		x3 = (x1 + x2)/2;

		cout <<"x1=" << x1 <<" | x2="<< x2 <<" | x3=" << x3 <<" | " << "  f(x1)=" << f(x1) << " |  f(x2)=" << f(x2) << " |  f(x3)=" << f(x3) << endl << endl;

		//float temp1 = f(x1);
		//float temp2 = f(x3);
		if( f(x1) * f(x3) < 0 )
		{
			x2 = x3;
		}
		else
		{
			x1 = x3;
		}
		count++;
	}
	while ( abs(x1 - x2) < 0.0000001 || f(x3) == 0 );

}