Results 1 to 2 of 2
  1. #1
    kienph2004 is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default Solution for Newton-Raphson method

    I have written my problem using Java language .
    I post it to the Jav forum .
    I hope that it may be useful for someone:D
    Thanks to mr Norm because your answer that i must study java language programming to solve my problem (wrap hand).



    /**
    * @(#)Newtonlastest.java
    *
    * Newtonlastest application
    *

    * @version 1.00 2008/8/15
    */
    package Iterative.Newton;
    import java.io.*;
    import java.text.DecimalFormat;
    public class Newtonlastest {
    public static void main(String[] args) throws Exception{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    //Format to display with six decimal places
    DecimalFormat root = new DecimalFormat("####0.000000");
    //Format to display with seven decimal places
    DecimalFormat rootseven = new DecimalFormat("####0.0000000");
    //This is the const to select delta
    final double select_delta = 0.5;
    double xnew;
    double fxold;
    double fdashxold;
    double absfxnew;
    int iteration;
    System.out.print("Enter the initial approximation for the root: ");
    double xold = Double.parseDouble(in.readLine());
    iteration =0;
    System.out.print("step x fx\n");
    System.out.print("--------------------------------------------------------------------------\n");
    do
    {
    // determine f(xold) and f·xold)
    fxold = func(xold);
    // printf temp result
    System.out.print( "\n");
    System.out.print( iteration );
    System.out.print( " " + root.format(xold) );
    System.out.print( " " + rootseven.format(fxold) );
    iteration = iteration + 1;
    //fdashxold = derivative_func(xold);

    //if calculate by approximate derivative then comment above line, and uncomment below line
    fdashxold = appro_derivative(xold, select_delta);
    xnew = xold - (fxold/fdashxold);
    absfxnew = func(xnew);
    if(absfxnew < 0)
    {
    absfxnew = -absfxnew;
    }
    xold = xnew;

    // check for convergence using the criterion |f(x)| < 0.000001
    }
    while (absfxnew > 0.000001);
    System.out.println("\n");
    System.out.print( iteration );
    System.out.print(" " + root.format(xold) );
    System.out.print( " "+ rootseven.format(func(xold)) );
    System.out.println();
    System.out.print("\nroot to six decimal places is :");
    System.out.print(root.format(xnew));

    }
    public static double func(double x)
    {
    return (0.66 *pow_int(x, 5) - 8.7 *pow_int(x, 4) + 42 *pow_int(x, 3) - 90 *x *x + 80 *x -24);
    }

    public static double derivative_func(double x)
    {
    return (3.3 *pow_int(x, 4) - 34.8 *pow_int(x, 3) + 126 *pow_int(x, 2) - 180 *x + 80);
    }

    public static double appro_derivative(double x, double delta)
    {
    return (func(x+delta) - func(x))/delta;
    }
    public static double pow_int(double x, int n)
    {
    double result = 1;
    int i;

    for(i = 0; i < n; i++)
    {
    result *= x;
    }
    return result;
    }
    }

  2. #2
    badri003 is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Thanks a lot

    Dear Java program writer,
    Your code is very helpful to me.
    Thanks a lot...

    Thank you very much dear sweet heart.
    :)

Similar Threads

  1. Pls Help me for Newton-Raphson.
    By kienph2004 in forum Advanced Java
    Replies: 1
    Last Post: 08-15-2008, 02:50 PM
  2. Pls Help me for Newton-Raphson method by Java
    By kienph2004 in forum Advanced Java
    Replies: 3
    Last Post: 08-13-2008, 06:07 PM
  3. solution for my project
    By shkelqa in forum AWT / Swing
    Replies: 4
    Last Post: 05-28-2008, 11:31 PM
  4. solution for my project
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-21-2008, 02:03 PM
  5. Please need solution
    By prithvi in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •