Hi everyone. This is my first post here at DW.
I was just experimenting with programs and thus I am trying to implement Bisection Method & False Position / Regula Falsi Method into a C++ program. Once I am done doing these two, ill start coding the Newton-Raphson Method.
All of the above mentioned methods are used in finding the root of a given polynomial, by the use of multiple iterations.
Now, ill first talk about the Bisection Method program. This program which i made may not be efficient / good, but it does its job in a crippled manner.
PROBLEMS in BISECTION METHOD :
1. Among the successful roots found out by the program of a polynomial are not accurate. (I did a counter check using a scientific calculator)
2. The program is not capable of finding the root of a polynomial even though a root exists.
3. For most of the polynomials, it hangs in the last do-while loop which is to find the root.
/* x0 - number at which, f(x0) yields the smallest possible negative value.
x1 - number at which, f(x0) yields the smallest possible positive value.
x2 - the mid-point or bisection of x0,x1
x - variable used in the computation of f(x)
a - the coefficient of x^2, in the polynomial of the form : ax^2+bx+c=0
b - the coefficient of x, in the polynomial of the form : ax^2+bx+c=0
c - the value of c, in the polynomial of the form : ax^2+bx+c=0
fx - the computed value of f(x), using different values of x during different iterations.
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void bisection()
{
float x=0,x0=0,x1=0,x2=0;
int i=0,a=0,b=0,c=0,fx=0;
cout<<"\n Find root of a given polynomial by BISECTION METHOD";
cout<<"\n\nEnter the polynomial which is in the form ax^2+bx+c=0 :";
cout<<"\n\na=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"c=";
cin>>c;
/* First do-while loop to find : x0 */
do
{
x=i;
fx=((a*pow(x,2))+(b*x)+c);
if (fx<0)
{
x0=i;
}
i++;
}while(fx>0);
i=0;
/* Second do-while loop to find : x1 */
do
{
x=i;
fx=((a*pow(x,2))+(b*x)+c);
if (fx>0)
{
x1=i;
}
i++;
}while(fx<0);
cout<<"\nRoot of the given polynomial lies between "<<x0<<" and "<<x1;
/* Third do-while loop to find : x2 */
do
{
x2=((x0+x1)/2);
x=x2;
fx=((a*pow(x,2))+(b*x)+c);
if (fx<0)
{
x0=x2;
}
if (fx>0)
{
x1=x2;
}}while(fx!=0);
cout<<"\n\nThe root of the given polynomial is : "<< x2;
}
/* Main Function */
void main()
{
clrscr();
bisection();
getch();
}
Now coming on to the Regula Falsi method.
PROBLEMS IN FALSE POSITION / REGULA FALSI :
1. same as above.
/* x0 - number at which, f(x0) yields the smallest possible negative value.
x1 - number at which, f(x0) yields the smallest possible positive value.
x2 - the value which is computed using the False Position / Regula Falsi Method.
x - variable used in the computation of f(x)
a - the coefficient of x^2, in the polynomial of the form : ax^2+bx+c=0
b - the coefficient of x, in the polynomial of the form : ax^2+bx+c=0
c - the value of c, in the polynomial of the form : ax^2+bx+c=0
fx - the computed value of f(x), using different values of x during different iterations.
*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void falsepos()
{
float temp=0,x=0,x0=0,x1=0,x2=0,fx0=0,fx1=0;
int i=0,a=0,b=0,c=0,fx=0;
cout<<"\n Find root of a given polynomial by REGULA FALSI METHOD";
cout<<"\n\nEnter the polynomial which is in the form ax^2+bx+c=0 :";
cout<<"\n\na=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"c=";
cin>>c;
/* First do-while loop to find : x0 */
do
{
x=i;
fx=((a*pow(x,2))+(b*x)+c);
if (fx<0)
{
x0=i;
}
i++;
}while(fx>0);
i=0;
/* Second do-while loop to find : x1 */
do
{
x=i;
fx=((a*pow(x,2))+(b*x)+c);
if (fx>0)
{
x1=i;
}
i++;
}while(fx<0);
cout<<"\nRoot of the given polynomial lies between "<<x0<<" and "<<x1;
/* Third do-while loop to find : x2 */
do
{
if(x1<x0)
{
temp=x0;
x0=x1;
x1=temp;
}
fx0=((a*pow(x0,2))+(b*x0)+c);
fx1=((a*pow(x1,2))+(b*x1)+c);
x2=(x0-(((fx0)*(x1-x0))/((fx1)-(fx0))));
x=x2;
fx=((a*pow(x,2))+(b*x)+c);
if (fx<0)
{
x0=x2;
}
if (fx>0)
{
x1=x2;
}}while(fx!=0);
cout<<"\n\nThe root of the given polynomial is : "<< x2;
}
/* Main Function */
void main()
{
clrscr();
falsepos();
getch();
}
Please help me out. Thanks in advance. :)