# help: nonlinear secant method program

• 10-07-2009
help: nonlinear secant method program
hi, im a beginner in using C++...

i have a project due in a few days and i've been trying to do the program for a week now... we're supposed to make a program that would allow the user to enter any polynomial equation (always = 0), asks the user the interval and number of iterations and solve it using secant method...

for example, the user entered the polynomial equation of 0.5x^4 + 2x^2 - x + 4 = 0, interval [-0.25, 0], no of iterations is 5, the result should be the following:

http://i37.tinypic.com/339qefn.jpg

the screen should look like this when program is run:

http://i33.tinypic.com/3325hs4.jpg

i already have a code for the secant method formula part for computing the new x sub n, however i don't have a clue as to how i would make a code enabling the user to enter the polynomial equation to be solved and substituting the new x (which is the new computed x sub n obtained from the secant formula) to the polynomial equation…

the question is what I want possible?... is there any kind of library that I need for wat I want?..

hope u guys can help coz honestly im no good in programming… I'm getting crazy with this project coz due is in 2 days time (sobs)… please help…

for additional information, for the secant part of the program… the code is the following:
Code:

```for(int i=1;i<=n;i++) {       c[i+1]=c[i]-(f(c[i]) * (c[i]-c[i-1])) / (f(c[i])-f(c[i-1]));       cout <<"c(" << i+1 <<") = "<< c[i+1] <<"\n"; }```
• 10-07-2009
RockyMarrone
Look at this

Code:

```#include <iostream> #include <math.h> class Polynomial { private:   float m_variable;   float m_interval; public:   Polynomial() : m_variable(0), m_interval(0) {}   Polynomial(const float variable,             const float interval) : m_variable(variable),                                     m_interval(interval) {}   void SetVariable(const float variable) { m_variable = variable; }   void SetInterval(const float interval) { m_interval = interval; }   void Calculate(); }; void Polynomial::Calculate() {   for (int counter = 0; counter < 5; ++counter) {     const float result = (pow((0.5 * m_variable), 4)) +                         (pow(2 * m_variable, 2))    -                         m_variable                  +                         4;     printf("Xn == %f ==  || F(Xn) == %f\n", m_variable, result);     m_variable += m_interval;   } } int main() {   Polynomial obj_poly;   obj_poly.SetVariable(2.5);   obj_poly.SetInterval(-0.25);   obj_poly.Calculate();   return 0; }```
• 10-09-2009
thanks... i'll look into the code u gave me...

oh yeah... i better explain how the values in the example i gave came about so that u guys cud understand wat i want to do more...

1) the polynomial equation that the user will enter will be the f(x) which is always equal to 0 (hence the example equation i used before was equal to 0)
2) the interval that the user will enter is the 1st x and 2nd x (respectively) which the program will evaluate giving a new value that will be shown under the column of f(x sub n)
[in the example, the interval was [-0.25 , 0] and these were substituted to the polynomial equation thus giving the values of 3.876953 (for x=-0.25) and 4 (for x=0)]
3) the new x that will be used for evaluation is obtained through the secant formula which is:
http://i33.tinypic.com/1zfsbhc.jpg
[in the example, the 3rd x in the column under x sub n is obtained through the secant formula:
http://i35.tinypic.com/o7j6s7.jpg
this new x will be evaluated (substituting it to the polynomial equation and getting a value of 2325.374361]
4) the 3rd step is repeated until the number of iterations is satisfied (in the example, the intervals, which are -0.25 and 0 are iteration 0 and iteration 1 respectively)

is what i want possible?..

by the way, i was able to make a program that does wat i want with the exception of entering the polynomial equation... this program solves the equation x - 2 + ln x = 0... however i dont know how to construct the part wherein the program could do the number of iterations that the user wants... i hope this gives you an idea...

Code:

```#include <stdio.h> #include <math.h> double f(double); int main() {     double k, x1, x2;     int n;     double y;         printf("Enter the interval [x1,x2]: ");     scanf("%lf, %lf", &x2, &x1);           printf("Checking the boundaries:\n");     printf("Xn\t\tf(Xn)\n");     printf("%.6lf\t%.6lf\n", x1, f(x1));     printf("%.6lf\t%.6lf\n", x2, f(x2));         for (n = 1; n<=4; n++)     {         y = (x1 - x2) / (f(x1) - f(x2)) * f(x1);         x2 = x1;         x1 = x1 - y;         printf("%.6lf\t%.6lf\n", x1, f(x1));     }         scanf("%lf", &k);     return 0; } double f(double x) {      return((x-2)+log(x)); }```
please if u cud, help me... i wud really really appreciate it...
• 10-09-2009
RockyMarrone
Quote:

thanks... i'll look into the code u gave me...

oh yeah... i better explain how the values in the example i gave came about so that u guys cud understand wat i want to do more...

1) the polynomial equation that the user will enter will be the f(x) which is always equal to 0 (hence the example equation i used before was equal to 0)
2) the interval that the user will enter is the 1st x and 2nd x (respectively) which the program will evaluate giving a new value that will be shown under the column of f(x sub n)
[in the example, the interval was [-0.25 , 0] and these were substituted to the polynomial equation thus giving the values of 3.876953 (for x=-0.25) and 4 (for x=0)]
3) the new x that will be used for evaluation is obtained through the secant formula which is:
http://i33.tinypic.com/1zfsbhc.jpg
[in the example, the 3rd x in the column under x sub n is obtained through the secant formula:
http://i35.tinypic.com/o7j6s7.jpg
this new x will be evaluated (substituting it to the polynomial equation and getting a value of 2325.374361]
4) the 3rd step is repeated until the number of iterations is satisfied (in the example, the intervals, which are -0.25 and 0 are iteration 0 and iteration 1 respectively)

is what i want possible?..

by the way, i was able to make a program that does wat i want with the exception of entering the polynomial equation... this program solves the equation x - 2 + ln x = 0... however i dont know how to construct the part wherein the program could do the number of iterations that the user wants... i hope this gives you an idea...

Code:

```#include <stdio.h> #include <math.h> double f(double); int main() {     double k, x1, x2;     int n;     double y;         printf("Enter the interval [x1,x2]: ");     scanf("%lf, %lf", &x2, &x1);           printf("Checking the boundaries:\n");     printf("Xn\t\tf(Xn)\n");     printf("%.6lf\t%.6lf\n", x1, f(x1));     printf("%.6lf\t%.6lf\n", x2, f(x2));         for (n = 1; n<=4; n++)     {         y = (x1 - x2) / (f(x1) - f(x2)) * f(x1);         x2 = x1;         x1 = x1 - y;         printf("%.6lf\t%.6lf\n", x1, f(x1));     }         scanf("%lf", &k);     return 0; } double f(double x) {      return((x-2)+log(x)); }```
please if u cud, help me... i wud really really appreciate it...

hey i gave u the above code if u can modify that to suite ur needs that will be better or what u can do
try to modify the above code then i can help u to reach ur goal ...
or somethin better u can suggest me that will be better :)
• 10-09-2009
m37h0d
you need to write a parser.

if you're only dealing with regular polynomials, your job is fairly easy. consider each independent variable and constant to be a token. scan over the input string for the independent variables. their exponents should be adjacent to them (separated by some exponentiated operator, ^, perhaps) to the right. each term should be separated by additions. use atof or some other ascii-to-numeric conversion routine to convert the text into numeric types.

pretty straightforward.
• 10-09-2009
MacNilly
There is an easier way to input the terms of the polynomial. Well, easier for the programmer and that's whats really important, isn't it? ;)

Enter number of terms: 3
Enter a1, n1: 2, 2
Enter a2, n2: 4, 1
Enter a3, n3: 4, 0

That would be 2x^2 + 4x + 4.

Something like that would be a heck of a lot easier to write than a real equation parser. If you do want to write a parser you'll have to make a polynomial class first. Also, the strtod() C library function will keep track of your pointer position of your string for you.