project for dynamic allocation

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;
    
}
Whats your question?
im having a problem with step 5 im not sure how to call p.eval(1) and have it return 24 from the equation form step 1
You want poly::eval to execute p(x) = x^3 + 6* x^2+ 11*x + 6.

When written as C++ that'd be double ans = pow(x,3) + 6*(x*x)+ 11*x + 6;. As long as you #include <math.h> you should have no problems.
alright im now on step 7 and im getting errors but i thinkits cuss im not doing this right to find the first derivative..
and have it print out..

any help?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <math.h>
using namespace std;
class poly{
public:
	poly(int);
	void setCo(double []);
	double polyVal(double);
	double polyCoef(double);
	void printPoly (void);
	double trap(double);
	double derivative(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;
	
}
void poly::printPoly(){
	int n=order;
	int i=0;
	for(;n>=0 ;i++, n--)
	if(n==0)
	cout<<cf[i]<<endl;
		else
		cout<<cf[i]<<"x^"<<n<<"+";
}
double poly::trap(double x){
	double a=1, b=4, t;
		t = 1 / 2*(b - a)*(polyVal(a) + polyVal(b));
	return t;
}
/*double poly::derivative(double x){
	double p, x, h;
	 P = ((p*(x+h) - p*(x-h)) / (2*h));

}*/

void main(){
poly p(3);
double x[]={1, 6, 11, 6};
	p.setCo(x);
	cout<<p.polyVal(1)<<endl;
	cout<<p.polyVal(-1)<<endl;
	p.printPoly();
	cout<<p.trap();<<endl;
	

}
anyone know how to do this? im stuck and tryin to get it done..
Are you able to do step 7 by hand?
yeah its P'(x) = p(x+h)-p(x-h)/2h

but to get it im told you have to call the polyVal function and then have it print..

i changed a little of it but i donno if im doin it correctly..

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <math.h>

using namespace std;
class poly{
public:
	poly(int);
	void setCo(double []);
	double polyVal(double);
	double polyCoef(double);
	void printPoly (void);
	double trapzd(double (*func)(double), double a, double b, int n);
	//double derivative(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;
	
}
void poly::printPoly(){
	int n=order;
	int i=0;
	for(;n>=0 ;i++, n--)
	if(n==0)
	cout<<cf[i]<<endl;
		else
		cout<<cf[i]<<"x^"<<n<<"+";
}

#define polyVal(x) ((*func)(x))
double trapzd(double (*func)(double), double a, double b, int n)
{
    double x,tnm,sum,del;
    double s;
    int it,j;

    if (n == 1) {
            return (s=0.5*(b-a)*(polyVal(a)+polyVal(b)));
    } else {
            for (it=1,j=1;j<n-1;j++) it <<= 1;
				tnm=it;
				del=(b-a)/tnm;             
				x=a+0.5*del;
            for (sum=0.0,j=1;j<=it;j++,x+=del) sum += polyVal(x);
				s=0.5*(s+(b-a)*sum/tnm);  
            return s;
			

    }
}
#undef polyVal


/*double poly::trap(double x){
	double polyVal();
	double a=1, b=4, t;
		t = 1 / 2*(b - a)*(polyVal() + polyVal());
	return t;
}
/*double poly::derivative(double x){
	double p, x, h;
	 P = ((p*(x+h) - p*(x-h)) / (2*h));

}*/

void main(){
poly p(3);
double x[]={1, 6, 11, 6};
	p.setCo(x);
	cout<<p.polyVal(1)<<endl;
	cout<<p.polyVal(-1)<<endl;
	p.printPoly();
	
	

}
I'm not clear on what data the class stores. Is cf storing the coefficients and order storing the order?

A couple of observations,
1. The double[] constructor doesn't conrrectly initialise the class.
2. Why not use a container to manage the collection of coefficients? As you can see, handling raw memory directly is error-prone a detracts from the problem.
3. You're accessing your indices on cf from high to low, but you're accessing the top+1 element of the arrays.
4. The dirivative is just manipulating coeffiecients on index i - 1.

I hope you find this helpful without me giving you my implementation.
Last edited on
well right now in class our teacher wants it to be a class, we havent yet gone over containers, and yeah the derivative is supposed to manipulate the coef, i juss am unexpierienced as to how to achieve it..
1
2
3
double poly::derivative(double x){
	double p, x, h;
	 P = ((p*(x+h) - p*(x-h)) / (2*h));


I don't know what problem are you having while trying to find the derivative, but if you are using the same subroutine (which you commented out) for finding the derivate, then 2 small problem in that code are:
1. You are redeclaring x, which is not valid because parameterized x is also local to that function. So just remove declaration of x from inside the function.
2. 'P' is not defined in the same function.

Also while dealing with dynamic allocation, make sure that you free the memory you allocated as C++ does not do garbage collection. So just add following destructor, like this :

1
2
3
poly :: ~poly() {
delete [] cf; 
}


Also declare ~poly() inside the class, same way you declared you constructor.

Let me know if this helps and how exactly you want it to work.


Peace
ok ive gotten thus far, and now am having problems adding, sub, mult the classes, i wrote something simmilar to what the teacher introduced but am not getting the right print outs for add, sub, mult the arrays.. leme know how to fix this please



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <cmath>

using namespace std;
class poly{
      public: //public class
             poly (int); // Allocation
             void setCo(double[]);
             double getCo(int);
             void printPoly();
             double polyVal(double);
             double polyInt(int, int);
             double polyPx(double);
             double polyPPx(double);
             void printInt();
             void printDer();
             double polyRroot(double, double);
            //start for extra credit constructors
             void poly2(int); // allocation
             void setCo2(double[]);
             void polyAdd();
             void polySub();
             void polyMult(); 
      private: //private class
              int order;
              double *cf;
              double *df;
              };
              //function to allocate and return all values of array to zero
              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::getCo(int n){
                     return cf[n-1];
                     }
             void poly::printPoly(){
                  int n=order;
                  int i=0;
                  for(;n>=0; i++, n--)
                  if(n==0)
                  cout<<cf[i]<<endl;
                  else if(n==1)
                  cout<<cf[i]<<"*x+";
                  else 
                  cout<<cf[i]<<"*x^"<<"+";
                  }
             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::polyInt(int a, int b){
                    double n = 100;
                    double h =(b-a)/n;
                    double sum=0.0;
                    double ans;
                    for (int i=1; i<=n-1; i++){
                        sum+=polyVal(a+(i*h));
                        }
                        ans=(h/2)*(polyVal(a) + polyVal (b) +(2*sum));
                        return ans;
                        }
             double poly::polyPx(double x){
                    double h = 0.001;
                    double ans;
                    ans = ((polyVal(x+h)) - (polyVal(x-h)))/(2*h);
                    return ans;
                    }
             double poly::polyPPx(double x){
                    double h=0.001;
                    double ans;
                    ans = (polyVal(x+h))-(2*(polyVal(x-h)))/(h*h);
                    return ans;
                    }
             void poly::printInt(){
                  int n=order;
                  int i =0;
                  cout<<"The integral is:";
                  for(;n>=0; i++, n--)
                  if(n==0)
                  cout<<cf[i]<<"*x^"<<endl;
				  else if(n==0)
                  cout<<"1";
				  else
                  cout<<cf[i]<<"/"<<n+1<<"*x^"<<n+1<<"+";
                  }
             void poly::printDer(){
                  int n = order;
                  int i = 0;
                  cout<<"The Dericative is:";
                  for(;n>=0; i++, n--)
                  if (n==1)
                  cout<<cf[i]*n<<endl;
                  else if(n==0)
                  cout<<" ";
                  else
                  cout<<cf[i]*n<<"*x^"<<n-1<<"+";
                  }
             double poly::polyRroot(double a, double b){
                    double c;
                    for(;;){
                            c=a-(polyVal(a)/(polyVal(a)-polyVal(b))*(a-b));
                            if(fabs(polyVal(c))<0.05e-6)
                            break;
                            b=a;
                            a=c;
                            }
                            return c;
                            }
             
             //start for extra credit constructors
			 void poly::poly2(int){// allocation
				 
				 df = new double[order+1];
				 for (int i=order; i>-1; i--)
					 df[i]=0;
			 }
			 void poly::setCo2(double[]){
				 for(int i = order; i>-1; i--)
					 df[i]=cf[i];
			 }
			 void poly::polyAdd(){
				 cout<<"The Sum of the two polynomials is: ";
				 int n = order;
				 int i =0;
				 for(;n>=0;i++,n--)
					 if(n==0)
						 cout<<cf[i] + df[i]<<endl;
					 else if(n==1)
						 cout<<cf[i] + df[i]<<"*x+";
					 else
						 cout<<cf[i] + df[i]<<"*x^"<<n<<"+";
			 }
			 void poly::polySub(){
				 cout<<"The difference of the two polynomials is: ";
				 int n = order;
				 int i =0;
				 for(;n>=0;i++,n--)
					 if(n==0)
						 cout<<cf[i] - df[i]<<endl;
					 else if(n==1)
						 cout<<cf[i] - df[i]<<"*x+";
					 else
						 cout<<cf[i] - df[i]<<"*x^"<<n<<"+";
			 }
			 void poly::polyMult(){
				 cout<<"The Multiplication of the two polynomials is: ";
				 int n = order;
				 int i=0;
				 for(;n>=0;i++,n--)
					 if(n==0)
					 cout<<cf[i] * df[i]<<endl;
					 else if(n==1)
						 cout<<cf[i] * df[i]<<"*x+";
					 else
						 cout<<cf[i] * df[i]<<"*x^"<<n<<"+";
			 }
             void main(){
                  poly p(3);
                  double x[]={1, 6, 11, 6};
                  p.setCo(x);
                  p.printPoly();
                  cout<<"The Coefficient at 2 is: "<<p.getCo(2)<<endl;
                  cout<<"The Coefficient at 3 is: "<<p.getCo(3)<<endl;
                  cout<<"The Polynomial at 1 is: "<<p.polyVal(1)<<endl;
                  cout<<"The Polynomial at 2 is: "<<p.polyVal(2)<<endl;
                  cout<<"The Polynomial at -2 is: "<<p.polyVal(-2)<<endl;
                  cout<<"The integral of the Polynomial from 1 to 4 is: "<<p.polyInt(1,4)<<endl;
                  cout<<"The first Derivative at 4 is: "<<p.polyPx(4)<<endl;
                  cout<<"The second Derivative at 4 is: "<<p.polyPPx(4)<<endl;
                  p.printInt();
                  p.printDer();
                  cout<<"The real root of the polynomial is: "<<p.polyRroot(0,100)<<endl;
                  p.poly2(3);
                  double y[]={2, 1, 5, 4};
                  p.setCo(y);
				  p.polyAdd();
				  p.polySub();
				  p.polyMult();
                  }
comon any ideas how to get this to add, sub, and mult class to work??
I've just seen that you missed one requirement for task 5 "poly::eval":

>> Your function should accomplish the mission without calculating the higher power of x.

So you mustn't use pow(x,3).
I'm pretty sure x*x*x isn't allowed either.

The intended solution almost certainly uses this equation:

a*x^2+b*x+c = (a*x+b)*x+c
(which can be adapted to polynoms of any order)

So if you use the right side, you don't calculate higher powers of x.
It's also numerically more stable and faster.
Topic archived. No new replies allowed.