Wednesday, 22 April 2015

C++ Program to solve a linear equation using Bisection Method

Write and execute a C++ program to find the three roots of the equation x^3-4x-1=0.



PROGRAM

# include <iostream>
# include <math.h>
#include<iomanip>                    // for setw()
using namespace std;
float f(float);
const int maxit=15;
int main()
{
int count=0;
float a,b,x;
cout<<"\nEnter lower limit:";
cin>>a;
cout<<"\nEnter upper limit:";
cin>>b;
if(f(a)*f(b)>0)
cout<<"\nNo root is in the range.\n";
else
{
cout<<"\n\nLower limt"<<setw(20)<<"Upper limit"<<setw(20)<<"Function"<<setw(20)<<"Root";
while(count<=maxit)
{
x=(a+b)/2;
cout<<setw(20)<<a<<setw(20)<<b<<setw(20)<<f(x)<<setw(20)<<x;
if(f(x)*f(b)>0)
b=x;
else
a=x;
count++;
}
}
return 0;
}
float f(float x)                           
{
float n=(x*x*x)-(4*x)-1;            // the given function
return (n);
}

1 comment :

  1. I think this will be more effective use for computer programming works. This will give some effective learning with the use of this kind of methods in solving problems for linear equations with the use of the bisection methods.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...