Why am I getting garbage values? - Using arrays to calc polynomial roots

Hello,

I am writing a program that will take coefficients of a polynomial, store them in an array and then using functions calculate the differntial of that polynomial and store it in another array.

Following this, I use the newton raphson formula to calculate a value after a set number of iterations and given a specific convergence factor.

I have got it to compile, but get garbage values when testing it with some quadratic equations.

For example, for the formula 2x^2 + 5x - 3 = 0, the roots would -3 and 0.5 respectively.

I can get the +ve root, but something mad with negative one.

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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

#include <iostream> 
#include <cmath> 
#include <iomanip>

using namespace std;

const int polynomial_size = 100;
int highest_index = 0;
int highest_index_for_differentiated_polynomial = 0;

double polynomial[polynomial_size];
double diff_polynomial[polynomial_size];

void newton_raphson(double, double, int); // function prototype 
double f(double); // function prototype for f(x)
double f_prime(double); //function prototype for f'(x)
void make_polynomial(int); //make a coeff array for a polynomial, where the index = power, value = coeff.
void display_polynomial(double[], int);
void differentiate_polynomial(int);

int main() {
	int imax; // maximum number of iterations 
	double x_guess_negative; //declaration for initial guess of negative root
	double x_guess_positive;//declaration for the initial guess of the +ve root

	double epsilon; // convergence criterion
					// obtain the input data 
	int index;

	//Ask for max index of polynomial
	//Call make polynomial function
	//Call displayt polynomial function
	cout << "What is the highest index of your polynomial function?" << endl;
		cin >> highest_index;

	make_polynomial(highest_index);
	display_polynomial(polynomial, highest_index);
	differentiate_polynomial(highest_index);

	cout << endl;

	cout << "Enter the initial guess for the negative root" << endl;
	cin >> x_guess_negative;

	cout << "Enter the initial guess for the +ve root" << endl;
	cin >> x_guess_positive;

	cout << "Enter the convergence criteria: " << endl;
	cin >> epsilon;

	if (x_guess_negative > 0)
	{
		cout << "re-enter the initial guess for the negative root" << endl;
		cin >> x_guess_negative;
	}

	if (x_guess_positive < 0)
	{
		cout << "re-enter the initial guess for the +ve root" << endl;
		cin >> x_guess_positive;
	}


	cout << "Enter the maximum number of iterations allowed: ";
	cin >> imax;

	cout << endl;
	// find the -ve root
	newton_raphson(x_guess_negative, epsilon, imax);

	//find the +ve root
	newton_raphson(x_guess_positive, epsilon, imax);

	system("pause");

	return 0;
}

// This function implements the newton_raphson method for finding 
// a root of a function 
void newton_raphson(double x, double eps, int i_max)
{
	double x_i, x_i_plus_1, x0;

	int i = 0; // current iteration counte
			   // echo back the passed input data 
	cout << "\nThe original root is search interval is from " << x
		<< "\nThe convergence criterion is: interval  "
		<< eps << "\nThe maximum number of iterations allowed is " << i_max << endl;

	x0 = x;
	x_i = x;
	x_i_plus_1 = 0;

	//Headings for output as table
	cout << "Iteration" << setw(10) << "x(i)" << endl;

	for (i = 0; i < i_max; i++)
	{

		if (abs(x_i_plus_1 - x0) >= eps)
		{
			x_i_plus_1 = x_i - (f(x_i) / f_prime(x_i));

			x_i = x_i_plus_1;

			cout << setprecision(4) << i << setw(20) << x_i_plus_1 << endl;
		}
		else
		{
			return;
		}
	}
	/*do {

	if (abs(x_i_plus_1 - x0 >= eps))
	{
	x_i_plus_1 = x_i - (f(x_i) / f_prime(x_i));

	x_i = x_i_plus_1;

	i++;
	}
	else
	{
	return;
	}


	cout << i << "			" << x_i << "			" << x << endl;
	} while (i < i_max); */

	cout << "\nAfter " << i << " iterations, The root is " << x_i << endl;
}

// function to evaluate f(x) 
double f(double x)
{
	double sum = 0;
	for (int i = highest_index; i >= 0; i--)
	{
		sum = sum + (polynomial[i] * pow(x, i));
	}
	return sum;
}

//function to evaluate f'(x)
double f_prime(double x)
{
	double sum = 0;

	for (int i = highest_index_for_differentiated_polynomial; i >= 0; i--)
	{
		sum = sum + (diff_polynomial[i] * pow(x, i));
	}
	
	return sum;
}

void make_polynomial(int max_index)
{
	double coefficient;

	cout << "The highest index of your polynomial is" << "x^" << highest_index << endl;

	cout << "What are the coeffiencients for your polynomial when it is written in orders of ascending powers?" << endl;

	for (int i = highest_index; i >= 0; i--)
	{
		cin >> coefficient;

	polynomial[i] = coefficient;
	}
}

void display_polynomial(double p[], int h_index)
{
	int maxindex = sizeof(polynomial);

	cout << "Your polynomial is" << endl;

	for (int i = highest_index; i >= 0; i--)
	{
		cout << polynomial[i] << "x^" << i << " + ";
	}

	cout << "The array indexes to store the coeffs are" << endl;

	for (int i = highest_index; i >= 0; i--)
	{
		cout << "Array index " << i << " Coeff stored " << polynomial[i] << endl;
	}
}

void differentiate_polynomial(int h_index)
{
	highest_index_for_differentiated_polynomial = highest_index - 1;

	cout << "Differentiating each power by using the standard result of n * x ^(n-1)" << endl;

	for (int i = highest_index_for_differentiated_polynomial; i >= 0; i--)
	{
		diff_polynomial[i - 1] = polynomial[i];
	}

	cout << "Your differentiated polynomial is " << endl;

	for (int i = highest_index_for_differentiated_polynomial; i >= 0; i--)
	{
		cout << diff_polynomial[i] << "x^" << i << " + ";
	}
}



What is the highest index of your polynomial function?
2
The highest index of your polynomial isx^2
What are the coeffiencients for your polynomial when it is written in orders of ascending powers?
2
5
-3
Your polynomial is
2x^2 + 5x^1 + -3x^0 + The array indexes to store the coeffs are
Array index 2 Coeff stored 2
Array index 1 Coeff stored 5
Array index 0 Coeff stored -3
Differentiating each power by using the standard result of n * x ^(n-1)
Your differentiated polynomial is
0x^1 + 5x^0 +
Enter the initial guess for the negative root
-2.5
Enter the initial guess for the +ve root
0.1
Enter the convergence criteria:
0.1
Enter the maximum number of iterations allowed: 10


The original root is search interval is from -2.5
The convergence criterion is: interval  0.1
The maximum number of iterations allowed is 10
Iteration      x(i)
0                -1.9
1              -0.844
2              0.3151
3              0.5603
4              0.4744
5                0.51
6               0.496
7              0.5016
8              0.4994
9              0.5003

After 10 iterations, The root is 0.5003

The original root is search interval is from 0.1
The convergence criterion is: interval  0.1
The maximum number of iterations allowed is 10
Iteration      x(i)
0               0.596
1              0.4579
2              0.5161
3              0.4934
4              0.5026
5               0.499
6              0.5004
7              0.4998
8              0.5001
9                 0.5

After 10 iterations, The root is 0.5
Last edited on
diff_polynomial[i - 1] is out of bounds when i is 0.
Finished it ! Thanks for your 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
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
190
191
192
193
194
#include <iostream> 
#include <cmath> 
#include <iomanip>

using namespace std;

const int polynomial_size = 100;
int highest_index = 0;
int highest_index_for_differentiated_polynomial = 0;

double polynomial[polynomial_size];
double diff_polynomial[polynomial_size];

void newton_raphson(double, double, int); // function prototype 
double f(double); // function prototype for f(x)
double f_prime(double); //function prototype for f'(x)
void make_polynomial(int); //make a coeff array for a polynomial, where the index = power, value = coeff.
void display_polynomial(double[], int);
void differentiate_polynomial(int);

int main() {
	int imax; // maximum number of iterations 
	double x_guess_negative; //declaration for initial guess of negative root
	double x_guess_positive;//declaration for the initial guess of the +ve root

	double epsilon; // convergence criterion
					// obtain the input data 
	int index;

	//Ask for max index of polynomial
	//Call make polynomial function
	//Call displayt polynomial function
	cout << "What is the highest index of your polynomial function?" << endl;
		cin >> highest_index;

	make_polynomial(highest_index);
	display_polynomial(polynomial, highest_index);
	differentiate_polynomial(highest_index);

	cout << endl;

	cout << "Enter the initial guess for the negative root" << endl;
	cin >> x_guess_negative;

	cout << "Enter the initial guess for the +ve root" << endl;
	cin >> x_guess_positive;

	cout << "Enter the convergence criteria: " << endl;
	cin >> epsilon;

	if (x_guess_negative > 0)
	{
		cout << "re-enter the initial guess for the negative root" << endl;
		cin >> x_guess_negative;
	}

	if (x_guess_positive < 0)
	{
		cout << "re-enter the initial guess for the +ve root" << endl;
		cin >> x_guess_positive;
	}


	cout << "Enter the maximum number of iterations allowed: ";
	cin >> imax;

	cout << endl;
	// find the -ve root
	newton_raphson(x_guess_negative, epsilon, imax);

	//find the +ve root
	newton_raphson(x_guess_positive, epsilon, imax);

	system("pause");

	return 0;
}

// This function implements the newton_raphson method for finding 
// a root of a function 
void newton_raphson(double x, double eps, int i_max)
{
	double x_i, x_i_plus_1, x0;

	int i = 0; // current iteration counte
			   // echo back the passed input data 
	cout	<< "\nThe original root is search interval is from " << x
			<< "\nThe convergence criterion is: interval  "
			<< eps << "\nThe maximum number of iterations allowed is " << i_max << endl;

	x0 = x;
	x_i = x;
	x_i_plus_1 = 0;

	//Headings for output as table
	cout << "Iteration" << setw(15) << "x(i)" << setw(15) << "f(x)" << setw(15) << "f'(x)" <<endl;

	for (i = 0; i < i_max; i++)
	{

		if (abs(x_i_plus_1 - x0) >= eps)
		{
			x_i_plus_1 = x_i - (f(x_i) / f_prime(x_i));

			x_i = x_i_plus_1;

			cout << setprecision(4) << i << setw(20) << x_i_plus_1 << setw(20) << f(x)<< setw(20) << f_prime(x) <<endl;
		}
		else
		{
			return;
		}
	}
	cout << "\nAfter " << i << " iterations, The root is " << x_i << endl;
}

// function to evaluate f(x) 
double f(double x)
{
	double sum = 0;
	for (int i = highest_index; i >= 0; i--)
	{
		sum = sum + (polynomial[i] * pow(x, i));
	}
	return sum;
}

//function to evaluate f'(x)
double f_prime(double x)
{
	double sum = 0;

	for (int i = 0; i <=highest_index_for_differentiated_polynomial; i++)
	{
		sum = sum + (diff_polynomial[i] * pow(x, i));
	}
	
	return sum;
}

void make_polynomial(int max_index)
{
	double coefficient;

	cout << "The highest index of your polynomial is" << "x^" << highest_index << endl;

	cout << "What are the coeffiencients for your polynomial when it is written in orders of descending powers?" << endl;

	for (int i = highest_index; i >= 0; i--)
	{
		cin >> coefficient;
		polynomial[i] = coefficient;
	}
}

void display_polynomial(double p[], int h_index)
{
	int maxindex = sizeof(polynomial);

	cout << "Your polynomial is" << endl;

	for (int i = highest_index; i >= 0; i--)
	{
		cout << polynomial[i] << "x^" << i << " + ";
	}

	cout << "The array indexes to store the coeffs are" << endl;

	for (int i = highest_index; i >= 0; i--)
	{
		cout << "Array index " << i << " Coeff stored " << polynomial[i] << endl;
	}
}

void differentiate_polynomial(int h_index)
{
	h_index = highest_index;
	highest_index_for_differentiated_polynomial = h_index - 1;

	cout << "Differentiating each power by using the standard result of n * x ^(n-1)" << endl;

	for (int i = 1; i <= h_index; i++)
	{	
		diff_polynomial[i - 1] = polynomial[i] * i ;
	}
	cout << endl; 

	cout << "Your differentiated polynomial is " << endl;

	for (int i = 0; i <=highest_index_for_differentiated_polynomial; i++)
	{
		cout << diff_polynomial[i] << " * x^" << i << " + ";
	}
}
Topic archived. No new replies allowed.