Alright guys im honestly at the novice to begginer status of programing and im in the progress of a project, if any one can help me out with it.
The goal of this exercise is to create a class for polynomial manipulation. For
simplicity, you may assume that we are concerned with polynomials whose
coefficients are real. Associated with an nth order polynomial there are n+1
coefficients. So, in your code the space for coefficients must be allocated
dynamically.
Let us assume that the name of the class is “poly” and it should have the
following features:
1. It should have variety of constructors. For example, the declaration:
poly p(3) ;
says that p is a third order polynomial whose coefficients are real.
On the other hand, the declaration
double c[ ] ={ 1 6 11 6};
poly p(3,c) ;
says that p(x) = x^3 + 6* x^2+ 11*x + 6. You need to come up with a
couple of more constructors on your own.
2. You must provide two different member functions in order to set the
coefficients of your polynomial.
3. You must provide two different member functions in order to read the
vales of the coefficients of the polynomial. For example, the call
p.getcoeff(2) should return 6 as the result.
4. You must provide a member function in order to print the polynomial in a
readable format.
5. You must provide a member function in order to evaluate a given
polynomial at a given value of x. For example, the call p.eval(1) should
return 24 because p(1) = 24. Your function should accomplish the
mission without calculating the higher power of x.
6. Include a function to numerically integrate the polynomial between the
limits a and b. You can accomplish this task by using the trapezoidal rule
or Simpson’s 1/3 rule with n- panels (Consult web for these formulas).
7. Include a function to numerically estimate the first derivative of the
polynomial at a given point x. You can accomplish this task by using the
second order central difference formula:
h
p x h p x h
p x
2
( ) ( )
'( ) » + - -
where p’(x) is the first derivative of the polynomial at x and h is the step
size which is typically 0.001.
8. Include a function to numerically estimate the second derivative of the
polynomial at a given point x. You can accomplish this task by using the
second order central difference formula:
2
( ) 2 ( ) ( )
''( )
h
p x h p x p x h
p x
» + - + -
where p”(x) is the second derivative of the polynomial at x and h is the
step size and it is typically set to 0.001.
9. Include a function to symbolically integrate the given polynomial.
10. Include a function to symbolically determine the first derivative of a
polynomial.
11. Include a function that will determine the real root of the given polynomial.
This can accomplished by using either Newton-Raphson or Secant
method (Check web for more details).
And this is what i have so far..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
using namespace std;
class poly{
public:
poly(int);
void setCo(double []);
double polyVal(double);
double polyCoef(double);
double eval(double);
private:
int order;
double *cf;
};
poly::poly(int n){
order=n;
cf= new double[n+1];
for(int i=n; i> -1; i--)
cf[i]=0;
}
void poly::setCo(double c[]){
for(int i=order; i> -1; i--)
cf[i]=c[i];
}
double poly::polyVal(double x){
int i;
double v;
for(i=v=0; i<= order; i++)
v= cf[i] + x*v;
return v;
}
double poly::polyCoef(double x){
int n;
double v;
v = cf[n-1];
return v;
}
double poly::eval(double x){
void main(){
poly p(3);
double x[]={1, 6, 11, 6};
p.setCo(x);
cout<<p.polyVal(2)<<endl;
cout<<p.polyVal(-2)<<endl;
}
|