Hi i have an assignment on finding the roots of a polynomial equation. i was abl
ID: 3630385 • Letter: H
Question
Hi i have an assignment on finding the roots of a polynomial equation. i was able to find the first root of the equation using newton raphson method.
now i was asked to implement a function to find all other roots of the polynomial equations.
for example : if f(x) = x^2 + 3x + 2
using newton method, i was able to find the first root of the above equation , which is -1
now i have to write another function to find other root of the equation which is -2. but i got stuck on how to find it.
another example: if f(x) = (x + 1)^2 . (x+ 2) ^2. then the output should return -1 -1 -2 -2.
the algorithm that the teacher gave us is:
1. Use root-polynomial function to find a root,
2. Divide the polynomial by this first order factor (x – root)
3. Repeat Step 1-2 until all n real roots are found.
#include
using namespace std;
double polynomial(double c, double a[], int n);
double root_polynomial (double x, double a[], int n);
const int N = 10;
int main()
{
double a[20] = {2,3,1};
double root, n;
root = root_polynomial(1, a, 2);
system("pause");
return 0;
}
double polynomial(double c, double a[], int n)
{
double power;
power = a[n];
for(int i = n -1 ; i >= 0; --i)
{
power = power * c + a[i];
}
return power;
}
double root_polynomial (double x, double a[], int n)
{
double b[20];
for(int i = 1; i <= n; i++) b[i - 1] = i * a[i];
for(int i = 1; i < N; i++)
{
x = x - polynomial(x, a , n) / polynomial(x, b, n - 1);
/*cout << " the root is : " << x << endl;*/
}
return x;
}
Explanation / Answer
Dear User, #include <iostream>using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::showpos;
using std::noshowpos;
class Polynomial
{
public:
Polynomial();
Polynomial operator+(const Polynomial& ) const;
Polynomial operator-(const Polynomial& ) const;
Polynomial operator*(const Polynomial& );
const Polynomial operator=(const Polynomial& );
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
Polynomial& operator*=( const Polynomial& );
void enterTerms();
void printPolynomial() const;
int getNumberOfTerms();
int getTermExponent( int );
int getTermCoefficient( int );
void setCoefficient( int, int );
~Polynomial();
private:
int numberOfTerms;
int exponents[ 100 ];
int coefficients[ 100 ];
void polynomialCombine( Polynomial& );
};
Polynomial::Polynomial()
{
for ( int t = 0; t < 100; t++ )
{
coefficients[ t ] = 0;
exponents[ t ] = 0;
}
numberOfTerms = 0;
}
void Polynomial::printPolynomial() const
{
int start;
bool zero = false;
if ( coefficients[ 0 ] )
{
cout << coefficients[ 0 ];
start = 1;
zero = true;
}
else
{
if ( coefficients[ 1 ] )
{
cout << coefficients[ 1 ] << 'x';
if ( ( exponents[ 1 ] != 0 ) && ( exponents[ 1 ] != 1 ) )
cout << '^' << exponents[ 1 ];
zero = true;
}
start = 2;
}
for ( int x = start; x < 100; x++ )
{
if ( coefficients[ x ] != 0 )
{
cout << showpos << coefficients[ x ] << noshowpos << 'x';
if ( ( exponents[ x ] != 0 ) && ( exponents[ x ] != 1 ) )
cout << '^' << exponents[ x ];
zero = true;
} }
if ( !zero )
cout << '0';
cout << endl;
}
const Polynomial Polynomial::operator=( const Polynomial& r )
{
exponents[ 0 ] = r.exponents[ 0 ];
coefficients[ 0 ] = r.coefficients[ 0 ];
for ( int s = 1; ( s < 100 ); s++ )
{
if ( r.exponents[ s ] != 0 )
{
exponents[ s ] = r.exponents[ s ];
coefficients[ s ] = r.coefficients[ s ];
}
else
{
if ( exponents[ s ] == 0 )
break;
exponents[ s ] = 0;
coefficients[ s ] = 0;
}
}
return *this;
}
Polynomial Polynomial::operator+( const Polynomial& r ) const
{
Polynomial temp;
bool exponentExists;
int s;
temp.coefficients[ 0 ] = coefficients[ 0 ] + r.coefficients[ 0 ];
for ( s = 1; ( s < 100 ) && ( r.exponents[ s ] != 0 ); s++ )
{
temp.coefficients[ s ] = r.coefficients[ s ];
temp.exponents[ s ] = r.exponents[ s ];
}
for ( int x = 1; x < 100; x++ )
{
exponentExists = false;
for ( int t = 1; ( t < 100 ) && ( !exponentExists ); t++ )
if ( exponents[ x ] == temp.exponents[ t ] )
{
temp.coefficients[ t ] += coefficients[ x ];
exponentExists = true; // exponent found
}
if ( !exponentExists )
{
temp.exponents[ s ] = exponents[ x ];
temp.coefficients[ s ] += coefficients[ x ];
s++;
} }
return temp;
}
Polynomial &Polynomial::operator+=( const Polynomial &r )
{
*this = *this + r;
return *this;
}
Polynomial Polynomial::operator-( const Polynomial& r ) const
{
Polynomial temp;
bool exponentExists;
int s;
temp.coefficients[ 0 ] = coefficients[ 0 ] - r.coefficients[ 0 ];
for ( s = 1; ( s < 100 ) && ( exponents[ s ] != 0 ); s++ )
{
temp.coefficients[ s ] = coefficients[ s ];
temp.exponents[ s ] = exponents[ s ];
}
for ( int x = 1; x < 100; x++ )
{
exponentExists = false;
for ( int t = 1; ( t < 100 ) && ( !exponentExists ); t++ )
if ( r.exponents[ x ] == temp.exponents[ t ] )
{
temp.coefficients[ t ] -= r.coefficients[ x ];
exponentExists = true;
}
if ( !exponentExists )
{
temp.exponents[ s ] = r.exponents[ x ];
temp.coefficients[ s ] -= r.coefficients[ x ];
s++;
}
}
return temp;
}
Polynomial &Polynomial::operator-=( const Polynomial& r )
{
*this = *this - r;
return *this;
}
Polynomial Polynomial::operator*( const Polynomial& r )
{
Polynomial temp;
int s = 1;
for ( int x = 0; ( x < 100 ) &&
( x == 0 || coefficients[ x ] != 0 ); x++ )
for ( int y = 0; ( y < 100 ) &&
( y == 0 || r.coefficients[ y ] != 0 ); y++ )
if ( coefficients[ x ] * r.coefficients[ y ] )
if ( ( exponents[ x ] == 0 ) &&
( r.exponents[ y ] == 0 ) )
temp.coefficients[ 0 ] +=
coefficients[ x ] * r.coefficients[ y ];
else
{
temp.coefficients[ s ] =
coefficients[ x ] * r.coefficients[ y ];
temp.exponents[ s ] =
exponents[ x ] + r.exponents[ y ];
s++;
}
polynomialCombine( temp );
return temp;
}
void Polynomial::polynomialCombine( Polynomial& w )
{
Polynomial temp = w;
int exp;
for ( int x = 0; x < 100; x++ )
{
w.coefficients[ x ] = 0;
w.exponents[ x ] = 0;
}
for ( int x = 1; x < 100; x++ )
{
exp = temp.exponents[ x ];
for ( int y = x + 1; y < 100; y++ )
if ( exp == temp.exponents[ y ] )
{
temp.coefficients[ x ] += temp.coefficients[ y ];
temp.exponents[ y ] = 0;
temp.coefficients[ y ] = 0;
}
}
w = temp;}
Polynomial &Polynomial::operator*=( const Polynomial& r )
{
*this = *this * r;
return *this;
}
void Polynomial::enterTerms()
{
bool found = false;
int c, e, term;
cout << " Enter number of polynomial terms: ";
cin >> numberOfTerms;
for ( int n = 1; n <= numberOfTerms; n++ )
{ cout << " Enter coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;
if ( c != 0 )
{
if ( e == 0 )
{
coefficients[ 0 ] += c;
continue;
}
for ( term = 1; ( term < 100 ) &&
( coefficients[ term ] != 0 ); term++ )
if ( e == exponents[ term ] )
{
coefficients[ term ] += c;
exponents[ term ] = e;
found = true;
}
if ( !found )
{
coefficients[ term ] += c;
exponents[ term ] = e;
} } } }
int Polynomial::getNumberOfTerms()
{
return numberOfTerms;
}
int Polynomial::getTermExponent( int term )
{
return exponents[ term ];
}
int Polynomial::getTermCoefficient( int term )
{
return coefficients[ term ];
}
void Polynomial::setCoefficient( int term, int coefficient )
{
if ( coefficients[ term ] == 0 )
cout << "No term at this location, cannot set term."<< endl;
else
coefficients[ term ] = coefficient;
}
Polynomial::~Polynomial()
{
}
int main()
{
Polynomial a, b, c, t;
a.enterTerms();
b.enterTerms();
t = a;
cout << "First polynomial is: ";
a.printPolynomial();
cout << "Second polynomial is: ";
b.printPolynomial();
cout << " Adding the polynomials yields: ";
c = a + b;
c.printPolynomial();
cout << " += the polynomials yields: ";
a += b;
a.printPolynomial();
cout << " Subtracting the polynomials yields: ";
a = t;
c = a - b;
c.printPolynomial();
cout << " -= the polynomials yields: ";
a -= b;
a.printPolynomial();
cout << " Multiplying the polynomials yields: ";
a = t; // reset a to original value
c = a * b;
c.printPolynomial();
cout << " *= the polynomials yields: ";
a *= b;
a.printPolynomial();
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.