Soft Computing Lab, RCH-554: Q1. Search of Bracketed Root
Soft Computing Lab, RCH-554: Q1. Search of Bracketed Root
Algorithm for Search Interval Using the Tabulation Method: This algorithm is
transformed to a computer program written in C++ language . Let xi and xf be the
initial and final values of the interval over which y = f (x) is to be tabulated. Let Dx
be the increment in the value of x.
#include <iostream.h>
#include<conio.h>
#define eps 0.00001
inline float f(float x)
{ return x*x*x-9*x+1; }
inline float derf(float x)
{ return (3*x*x-9); }
void ntrp() { float a,c,h1;
int count; char option;
cout<<"For default input values enter 'y' else 'n'\n";
cin>>option;
if (option=='y')
{ a=2; }
else
{
cout<<"input initial root value a\n";
cin>>a; }
count=0;
do
{ if (derf(a)>0)
h1=f(a)/derf(a);
c=a-h1; count=count+1;
if ((fabs((c-a)/c)<eps) && (f(c)==0))
break;
else a=c; }
while ((count<=20) && fabs(f(c))>eps);
cout<<"root= "<<c<<endl; cout<<"f(c)= "<<f(c)<<endl;
cout<<"iterations= "<<count<<endl;
}
main()
{ ntrp();
}