Problem With Secant Method

Can someone help me find the error of this code? it displays wrong results. Here is a sample output usuing the polynomial x^2-5x+2.


Enter the order of the equation: 2
Enter the coefficient of x^2:    1
Enter the coefficient of x^1:    -5
Enter the constant term:         2

Enter first initial guess: 4
Enter second initial guess: 5
Set maximum number of iterations: 10

__________________________________
k          x[k]            error
__________________________________
0           4              -2
1           5               2
2           0               2
3        1.#INF          -1.#IND
4       -1.#IND          -1.#IND
5       -1.#IND          -1.#IND
6       -1.#IND          -1.#IND
7       -1.#IND          -1.#IND
8       -1.#IND          -1.#IND
9       -1.#IND          -1.#IND
_________________________________

The root is approximately:        -1.#IND0000000000000
With error of:                    -1.#IND0000000000000


and here is my code


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
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int p;
const int MAX = 1000;
double coeff[MAX];
double f(double);

int main()
{
    double guess1, guess2;
    int imax;
    double x[MAX];
    

              //Get Function
          cout << "\tEnter the order of the equation: ";
          cin  >> p;
   
          for(int i = p; i > 0; i--)
          { 
             cout << "\tEnter the coefficient of x^" << i << ":    ";
             cin >> coeff[i];
          }
          cout << "\tEnter the constant term:         ";
          cin >> coeff[0];
          cout << endl;
          
          //Final inputs
            cout << "\tEnter first  initial guess: ";
            cin  >> guess1;
            cout << "\tEnter second initial guess: ";
            cin  >> guess2;
            cout << "\tSet maximum number of iterations: ";
            cin  >> imax;
            
            //assigning guesses
            if(guess2>guess1)
            {
                             x[0] = guess1;
                             x[1] = guess2;
                             
            }
            else
            {
                             x[0] = guess2;
                             x[1] = guess1;
            }
            
            //the heart of secant method
            
            for(int i = 2; i<imax; i++)
            {
                    
                    x[i+1] = x[i] - f(x[i]) * ( x[i] - x[i-1] ) / (f(x[i]) - f(x[i-1]));
            }
            
                //For Good Output.
    
    cout << endl << endl;
    cout << "\t____________________________"<< endl;
    cout << "\t  k       x[k]         error" << endl;
	cout << "\t____________________________" << endl;
    
    
    for(int i = 0; i < imax; i++)
    {
      cout <<"\t" << setw(5) << i    << ' '
           << setw(10) << x[i]     << ' '
           << setw(12) << f(x[i]) << ' '   
           << endl;
    }
    
    cout << "\t_____________________________"<< endl;
    cout << endl << endl << endl;
    cout << "\tThe root is approximately: ";
   
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(20);
    cout << "\t" << x[imax-1];
    
    cout << "\n\tWith error of: " <<"\t\t\t" << f(x[imax-1]);
    
  cin.get();
  cin.get(); 
  return 0;
}
double f(double x)
{
       int i;
       double s = 0;
       
       for(i = 0; i <= p; i++)
       {
             s += coeff[i] * pow(x, i);
       }
       
       return s;

}
1
2
3
4
           for(int i = 2; i<imax; i++)
            {      
                    x[i+1] = x[i] - f(x[i]) * ( x[i] - x[i-1] ) / (f(x[i]) - f(x[i-1]));
            }
`x[2]' is uninitialized.
got it men! THANK YOU SO MUCH!
Topic archived. No new replies allowed.