This document contains C++ code to find roots of equations using the bisection method. It includes 4 questions:
1) Finds the root of x3 – 4x – 8.95 = 0 between 2 and 3 to 3 decimal places.
2) Finds the root of ex – 3x = 0 using bisection up to a specified number of iterations.
3) Defines a bisection function to find the root of f(x) = x6 – x – 1 = 0 within 0.001 accuracy.
4) Defines a bisection function to find the smallest positive root of a cubic equation ax3+bx2+cx+d=0 within an error tolerance of 10-6.
3. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
QUESTION 2
Write a C++ program to find a root of the equation ex – 3x = 0 using the Bisection method.
#include <iostream>
#include<cmath>
using namespace std;
void main()
{
int n, count=1;
double x0,x1,x2,fx0,fx1,fx2;
cout <<"nSolution by BISECTION method n";
cout <<"nThe equation is: exp(x) - 3*x= 0nn";
cout << "Enter the number of iterations: ";
cin >> n;
/*Finding an Approximate ROOT of Given Equation, Having +ve Value*/
for(x2=1;;x2++)
{
fx2= exp(x2) - 3*x2;
if (fx2 > 0)
{
break;
}
}
/*Finding an Approximate ROOT of Given Equation, Having -ve Value*/
for(x1=x2-1; ; x2--)
{
fx1= exp(x1) - 3*x1;
11. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
Root = 1.13473
Iterations = 19
----------------------------------
ITERATION 20: 1.13472
----------------------------------
Root = 1.13472
Iterations = 20
----------------------------------
----------------------------------------
Root = 1.13472
Iterations = 20
----------------------------------------
QUESTION 4
Write a C++ Function program to find the smallest positive root of the cubic
equation ax3+bx2+cx+d=0 using Bisection method. You can take error tolerance
of 10-6
#include <iostream>
#include <cmath>
using namespace std;
#define f(x) (a*pow(x,3)+b*pow(x,2)+c*x+d)
double Bisection(double a, double b, double c, double d)
{
double x1, x2;
double mid_x;
x1 = 0;
x2 = 1; // We need this (in other case x2 is random value)
mid_x = 0.5; // initialize all variables before use
while( f(x1) * f(x2) > 0)
{
x1 = x2;
x2 = x1 + 1;
if (x2 > 1000)
return -1.0; // to prevent overflow
12. }
for(int i = 1; i < 100; i++)
{
mid_x = (x1 + x2) / 2.0;
if(f(x1) * f(mid_x) < 0)
{
x2 = mid_x;
}
else
{
x1 = mid_x;
}
if(fabs(x1 - x2) < 0.0000001)
{
return (x1 + x2) / 2; // will return immediately
}
}
return mid_x; // return anything. Code can to bounce between two numbers
}
int main()
{
double a, b, c, d, h;
ULANG:
cout << "Enter the value of constants for the cubic equation: " << endl;
cin >> a>> b>> c>> d;
if(a==0)
13. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
{
cout << "The value for constant a cannot equal to 0.Try again:n" << endl;
goto ULANG;
}
h = Bisection(a, b, c, d);
if (h >= 0)
cout << "The smallest +ve root of the cubic equation using Bisection method is: "
<< h << endl;
else
cout << "The root is not found in range from 0 to 1000" << endl;
return 0;
}
//Output:
Enter the value of constants for the cubic equation:
0 1 6 -1
The value for constant a cannot equal to 0.Try again:
Enter the value of constants for the cubic equation:
6 2 0 -1
The smallest +ve root of the cubic equation using Bisection method is: 0.45872
/******ALTERNATIVE METHOD FOR QUESTION 4******/
Done by FAIZ MATHS STUDENT, UM (trust me, he is so genius)
#include <cmath>
#include <iostream>
#include <iomanip>
14. using namespace std;
void bisec(double a, double b);
void choosepositive(int& x, int& y);
void choosenegative(int& x, int& y);
void zero(int z);
double f(double x);
double mid(double x,double y);
double p,q,r,s;
int main()
{
int z,l=0,n=1,m=0,o=-1,e,g,t=0,u=0,h;
cout << "BISECTION METHOD [CUBIC EQUATION]nnEnter ax^3+bx^2+c+dn" << endl;
cin >> p >> q >> r >> s;
do
{
do
{
zero(m);
cout << "nChoose:n" << "1. +ve rootn" << "2. -ve rootn" << endl;
cin >> z;
if (z==1)
{
choosepositive(l,n);e=n;g=n+1;
if (f(l)*f(n) == 0 && f(n)!=0) {cout << "NO MORE REAL +VE ROOT" <<
endl;break;}
if (n==100){cout<<"NO MORE REAL +VE ROOTn";break;}
15. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
bisec(l,n);break;
}
else if (z==2)
{
choosenegative(m,o);t=o;u=o-1;
if (f(m)*f(o) == 0 && f(o)!=0) {cout << "NO MORE REAL -VE ROOT" <<
endl;break;}
if (o==-100){cout<<"NO MORE REAL -VE ROOTn";break;}
bisec(m,o);break;
}
}
while (z!=1 || z!=2);
cout << "Want Another root?n1. YESn2. NO (EXIT)n" << endl;
cin >> h;l=e;n=g;m=t;o=u;
}
while (h==1);
return 0;
}
void bisec(double a, double b)
{
double n=1,r;
if (f(b)==0) {cout << "nThe root is " << b <<endl;}
else
{
r=mid(a,b);
cout << "n attbttxttb-rttf(x)" <<endl;
16. do
{
cout << setprecision(0) << n << " "<< setprecision(6) << fixed << a << "t"
<< b << "t" << r << "t" << b-r << "t" << f(r) << endl;
if (f(a)*f(r) < 0) b=r; else if (f(r)*f(b) < 0) a=r; else if (f(r)==0) break;
r=mid(a,b);n++;
}
while (abs(b-r) > 0.000001 );
cout << "nThe root is " << r <<endl;
}
}
void choosepositive(int& x, int& y) { while (f(x)*f(y) > 0 && y<100) {x=y;y++;} }
void choosenegative(int& x, int& y) { while (f(x)*f(y) > 0 && y>-100) {x=y;y--;} }
void zero(int z) {if (f(0)==0) {cout << "nThe root is 0"<< endl;}}
double f(double x) { return (double) p*pow(x,3)+q*pow(x,2)+r*x+s;}
double mid(double x,double y) {return (double)(x+y)/2;}
//Output:
BISECTION METHOD [CUBIC EQUATION]
Enter ax^3+bx^2+c+d
1 2 3 4
Choose:
1. +ve root
2. -ve root
17. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
1
NO MORE REAL +VE ROOT
Want Another root?
1. YES
2. NO (EXIT)
1
Choose:
1. +ve root
2. -ve root
2
n a b x b-r f(x)
1 -1.000000 -2.000000 -1.500000 -0.500000 0.625000
The root is -1.750000
Want Another root?
1. YES
2. NO (EXIT)
2
QUESTION 5
Write a C++ program to find a root of the function f (x) = ex – 3x2 to an accuracy
of 5 digits in the interval 0.5 and 1.0 using the false position method
#include<iostream>
#include<cmath>
using namespace std;
19. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
//Output:
x1= |x2= |x3= |fx1= |fx2= |fx3=
0.5 1 0.880672 0.898721 -0.281718 0.0857699
0.880672 1 0.908523 0.0857699 -0.281718 0.0044143
0.908523 1 0.909934 0.0044143 -0.281718 0.000218671
0.909934 1 0.910004 0.000218671 -0.281718 1.08115e-005
0.910004 1 0.910007 1.08115e-005 -0.281718 5.34494e-007
0.910007 1 0.910008 5.34494e-007 -0.281718 2.64238e-008
0.910008 1 0.910008 2.64238e-008 -0.281718 1.30632e-009
0.910008 1 0.910008 1.30632e-009 -0.281718 6.45808e-011
0.910008 1 0.910008 6.45808e-011 -0.281718 3.19211e-012
0.910008 1 0.910008 3.19211e-012 -0.281718 1.58096e-013
0.910008 1 0.910008 1.58096e-013 -0.281718 7.10543e-015
0.910008 1 0.910008 7.10543e-015 -0.281718 1.33227e-015
0.910008 1 0.910008 1.33227e-015 -0.281718 -4.44089e-016
The root is= 0.910008
QUESTION 6
Write a C++ function program to find a root of cos x – 3x + 5 = 0 using the false
Position method.
#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
double fx;
20. fx= cos(x) - 3*x +5;;
return fx;
}
void main()
{
double x1, x2,x3;
cout << "Enter x1: ";
cin >> x1;
cout << "Enter x2: ";
cin >> x2;
cout << "x1=t" << "|x2=tt"<<"|x3=tt" << "|fx1=tt" << "|fx2=tt" << "|fx3=t" << endl;
do
{
x3 = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));
cout << x1 << "t" <<x2 << "t" <<x3 <<"t" << f(x1) <<"tt" << f(x2) << "tt" <<f(x3) <<endl;
if(f(x1)*f(x3) <0)
{
x2=x3;
}
else
{
x1=x3;
}
} while ( fabs(x1 - x2) > 0.000001); //the epsilon value can be changed
/***The function is like a straight line that regular falsi can extrapolate to a root even from far
away, such as from (10, 100). But it is not a straight line, so it cannot converge with that much
precision. To avoid this, we can try dropping our precision requirements by order of magnitude
and it will work ****/
22. QUESTION 7
Find a real root of the equation x4 – 11x + 8 = 0 accurate to four decimal places
using regular falsi method.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#define f(x) (pow(x,4)- 11*x+8)
double FALSI(double a, double b, double c, double d,double e)
{
double x1, x2;
double mid_x;
x1 = 0;
x2 = 1; // need this!! in other case x2 is random value!
mid_x = 0.5; // initialize all variables before use
while( f(x1) * f(x2) > 0)
{
x1 = x2;
x2 = x1 + 1;
if (x2 > 1000)
return -1.0; // to prevent overflow
}
for(int i = 1; i < 100; i++)
{
mid_x = (x1*f(x2) -x2*f(x1))/(f(x2)-f(x1));
23. SJEM2231: STRUCTURED PROGRAMMING (C++) TUTORIAL 6
if(f(x1) * f(mid_x) < 0)
{
x2 = mid_x;
}
else
{
x1 = mid_x;
}
if(fabs(x1 - x2) < 0.0000001)
{
return (x1 + x2) / 2; // will return immediately
}
}
return mid_x; // return anything. Code can to bounce between two numbers
}
int main()
{
double h;
h = FALSI(1, 0, 0,-11,8);
cout << "The real root of the equation by using Falsi method is: " << setprecision(4)
<<fixed<< h << endl;
return 0;
}
//Output:
The real root of the equation by using Falsi method is: 0.7571 (4 decimal places)