Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do secant method in Java?

Status
Not open for further replies.

Cybertronic

Technical User
May 18, 2006
19
Hi all,

I was wondering how can I create a secant method function in Java?

I've seen a few examples of secant methods created in different programming languages such as Fortran:


I've tried converting the code in the above link into Java but unfortunately I've had no success :(

I've done the function for bisection method in Java which is part of my non-linear equations research:

Code:
// m = midpoint
// f = function (from Function class)

for (i = 0; i <= max; i++)
{
    m = (x0 + x1)/2;
                
    System.out.println(i + "\t" + 
                       x0 + "\t" + 
               f.apply(x0) + "\t" +  
                       x1 + "\t" +  
               f.apply(x1) + "\t" +  
                       m + "\t" +  
               f.apply(m));
         
    if (f.apply(m) < 0)
    {
        x1 = m;
    }
    else
    {
        x0 = m;
    }
}

The above code is only the main part of my bisection method code, could anybody please help me create a secant method in Java please? :)

I appreciate anybody's assistance :)
 
Silly question... isn't the secant equal to the reciprocal of the cosine? You've just got to check for +/- 90, 270, etc.
 
Miros, the secant method I'm doing is for locating roots of nonlinear equations :)

I'm stuck on my own secant method code and I'm using the function: f(x) = x^3 - 3x +1

Code:
System.out.println("\n" + "n  \t" + 
                                     " xn         \t" + 
                                     " f(xn)         \t" + 
                                     " xn+1         \t" + 
                                     " f(xn+1)     \t" + 
                                     " xn+1 - xn     \t" + 
                                     " f(xn+1) - f(xn)");
            
            System.out.println("------------------------------------------------------------------------------------------------------");

div = f.apply(x1) * (x1 - x0) / (f.apply(x1) - f.apply(x0));
            
            for (i = 0; i <= max; i++)
            {
                System.out.println(i + "\t " + 
                         FR.format(x0) + "\t " + 
                         FR.format(f.apply(x0)) + "\t " + 
                         FR.format(x1) + "\t " + 
                         FR.format(f.apply(x1)) + "\t " + 
                         FR.format(x1 - x0) + "\t " + 
                         FR.format(f.apply(x1) - f.apply(x0)) + "\t ");
                
                x0 = x1;
                
                x1 = x0 - div;
            }

My secant method class generates the output below. The values I've used for the function is x0 = 2 and x1 = 4:

Code:
n     xn            f(xn)         xn+1          f(xn+1)        xn+1 - xn     f(xn+1) - f(xn)
------------------------------------------------------------------------------------------------------
0	 2.0000000	 3.0000000	 4.0000000	 53.0000000	 2.0000000	 50.0000000	 
1	 4.0000000	 53.0000000	1.8800000	 2.0046720	  -2.1200000	-50.9953280	 
2	 1.8800000	 2.0046720	 -0.2400000	1.7061760	  -2.1200000	-0.2984960	 
3	 -0.2400000	1.7061760	 -2.3600000	-5.0642560	 -2.1200000	-6.7704320

There's two things that is wrong in this output which are:

For the 2nd loop (n2) under "xn+1 - xn", the calculation is wrong, it should be:

-0.2400000 - 1.8800000 = 1.9040000, not -2.1200000!

The same -2.1200000 repeats for each iteration under the same column until the loop has finished, can anybody please tell what am I doing wrong here? :(

The first two lines of the output is correct but lines after those is where it goes all wrong such as in n3 where xn = -0.2400000 which is should really be:

x3 = x2 - f(x2) (x2 - x1) / f(x2) - f(x1)

x3 = 1.8800000 - (- 4.24990464 / - 50.995328)

x3 = 1.8800000 - 0.083339098

x3 = 1.796660902


x3 is not supposed to be -0.2400000 as shown in my output :(

Can anybody please help me?

I'm pretty desperate!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top