Ashieboy
Member level 1
Code:
#include<iostream>
#include<cmath>
using namespace std;
float f (float x)
{
float fx1;
fx1 = pow(x,3) - pow(x,2) + (1.4*x) + 7.8;
return (fx1);
}
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) + (1.4x1) + 7.8;
// int fx2 = (x2^3) - (x2^2) + (1.4x1) + 7.8;
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 );
}
was trying to do it in OOP style... can anyone link me to a tutorial on it??
Last edited by a moderator: