Results 1 to 13 of 13
  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default HELP :( I am writing a NewtonRaphson program.

    I am lost as to how to even start my testClass, I've written a mainClass. Can someone give me a hint or advice? 0__o

    MainClass:

    Java Code:
    package assignment6;
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author sithr_000
     */
    public class NewtonRaphson {
        
        private static final double MINIMUM = 0.0;
        private String iteration;
        private double difference, x, a, solution;
    
        public void calculate(double x0, int counter) {
    
            if (difference < MINIMUM) {
                solution = x;
            } else {
                x = x0 - (((Math.pow(x0, 2.0)) - a) / (2.0 * x0));
                difference = Math.abs(x0 - x);
                iteration = iteration + "\n" + counter + "\t" + x;
                calculate(x, ++counter);
            }
        }
        
        public String getIteration(){
            return iteration;
        }
        public double getDifference(){
            return difference;
        }
        public double getSol(){
            return solution;
        }
        public double getdifference(){
            return difference;
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: HELP :( I am writing a NewtonRaphson program.

    how to even start my testClass
    What have you tried? There is a certain minimum needed for any class to be executed. Start with that.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Once you choose a starting approximation, then you can either choose a number of iterations or a target delta between successive iterations.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Variable 'a' isn't initialized anywhere and variable 'difference' is the absolute value of something so it can never be less than zero (your stop criterium) and why are you using recursion for this?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: HELP :( I am writing a NewtonRaphson program.

    I havnt written a testClass yet, i dont know how i would start it. Im using the recursion method because Im finding the approximate solution of square root of 'a' using Newton's method, for any positive real number. I am aiming for it to be less than 0.00005 close to the difference between two consecutive approximations, the initial guess is 5.0. I dont have any noted on how i would start the testClass, I'm asking for guidance.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Why not just choose a number and print out the results for each iteration. You can also simply web search the algorithm. The articles should talk about choosing the initial values.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Quote Originally Posted by sithclone3 View Post
    Im using the recursion method because Im finding the approximate solution of square root of 'a' using Newton's method, for any positive real number.
    You should supply a value for that number; Java can't do symbolic calculations (sadly enough) for 'any positive real number'; applying recursion where the recursion stack depth is linear proportional to the size of the problem (here: the number of steps) is silly: you can change it to a do-while loop where the condition is diffierence >= MINIMUM. In pseudo code:

    Java Code:
    do {
       xn= x-f(x)/f'(x);
       difference= Math.abs(x-xn);
       x= xn;
    }
    while (difference >= MINIMUM);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Hey, sorry for late reply, I've written most of the code, I replaced what my recursion with your do/while statement, but I keep getting f as an error. says f(double) cannot find symbol, same when I replace f with a or vice versa.

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    /**
     *
     * @author sithr_000
     */
    public class NewtonRaphson {
    
        private String iteration;
        private double difference, x, f, solution, minimum;
    
        NewtonRaphson(double diff, double num) {
            f = num;
            difference = diff;
            iteration = "";
        }
    
        public void calculate(double x0, int counter) {
    
            do {
                x0 = x - f(x) / f(x);
    
                difference = Math.abs(x - x0);
                x = x0;
            } while (difference >= minimum);
        }
    
        public String getIteration() {
            return iteration;
        }
    
        public double getDifference() {
            return difference;
        }
    
        public double getSol() {
            return solution;
        }
    
        public double getdifference() {
            return difference;
        }
    }
    testClass
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author sithr_000
     */
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    
    public class TestNewton {
       
        public static void main(String args [])
        {
            int num = GetData.getInt("Find the square root for:");
            
            NewtonRaphson sqrt = new NewtonRaphson(0.00005, num);
            
            sqrt.calculate(5.0,1);
            
            JTextArea text = new JTextArea("Iteration\tApproximation\n" + sqrt);
            JScrollPane pane = new JScrollPane(text);
            JOptionPane.showMessageDialog(null,pane);
        }
    }

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Quote Originally Posted by sithclone3 View Post
    Hey, sorry for late reply, I've written most of the code, I replaced what my recursion with your do/while statement, but I keep getting f as an error. says f(double) cannot find symbol, same when I replace f with a or vice versa.
    The problem is your f(x) in the while loop. There is no method f(double x). Also, the denominator needs to the f'(x), not f(x).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: HELP :( I am writing a NewtonRaphson program.

    when I write f'(x) it becomes an unclosed character literal.

    So for the f(double x) I just rewrite
    Java Code:
    public void calculate(double x0, int counter)
    as
    Java Code:
    public void calculate (f(double));
    ??

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: HELP :( I am writing a NewtonRaphson program.

    Quote Originally Posted by jim829 View Post
    The problem is your f(x) in the while loop. There is no method f(double x). Also, the denominator needs to the f'(x), not f(x).
    You took me literally. I was speaking algebraically. You need to have some funcx()/derivativeOfFuncx(). So if your equation is x^2 (^ means exponentiation here) you need.

    Java Code:
    double func(double val) {
        return val*val;
    }
    double derivativeOfFunctionx(double val) {
       return 2 * val;
    }
    or just code it directly,
    Java Code:
    (val*val)/(2*val);
    This would be a great opportunity for an interface where the user supplies the f(x) and f'(x) implementations.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    55
    Rep Power
    0

    Default Re: HELP :( I am writing a NewtonRaphson program.

    aight! thanks a lot man! I finished it!!! you always help so much man, thanks!

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: HELP :( I am writing a NewtonRaphson program.

    The expression f'(x) wasn't recognized as the first derivative of f w.r.t. x in my pseudo code example in reply #7. Note that you can also approximate f'(x) by (f(x+h)-f(x-h))/2*h for a small value of h. It is a much better approximation O(h^2) than the more intuitive text book approximation f'(x) == (f(x+h)-f(x))/h which has an error of O(h). But if you have the analytical form of f'(x) available, use that one of course.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. NewtonRaphson java code help
    By Humraj in forum New To Java
    Replies: 2
    Last Post: 06-19-2013, 01:06 PM
  2. help in writing a program?
    By baldy100 in forum New To Java
    Replies: 3
    Last Post: 01-10-2013, 04:06 PM
  3. Help with writing my own program
    By DMarsh12 in forum New To Java
    Replies: 4
    Last Post: 12-30-2011, 03:34 AM
  4. how begin with writing a new program ?
    By dimitrist in forum New To Java
    Replies: 11
    Last Post: 05-13-2008, 03:49 AM
  5. Help needed writing a program...
    By Francis in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 02:03 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
  •