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 << " + ";
}
}
| |