# HELP :( I am writing a NewtonRaphson program.

• 07-10-2014, 06:24 PM
sithclone3
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:

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;     } }```
• 07-10-2014, 06:52 PM
Norm
Re: HELP :( I am writing a NewtonRaphson program.
Quote:

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.
• 07-10-2014, 07:16 PM
jim829
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
• 07-10-2014, 07:38 PM
JosAH
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
• 07-10-2014, 10:05 PM
sithclone3
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.
• 07-10-2014, 11:27 PM
jim829
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
• 07-11-2014, 08:56 AM
JosAH
Re: HELP :( I am writing a NewtonRaphson program.
Quote:

Originally Posted by sithclone3
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:

Code:

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

Jos
• 07-16-2014, 11:43 PM
sithclone3
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.

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
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);     } }```
• 07-17-2014, 12:02 AM
jim829
Re: HELP :( I am writing a NewtonRaphson program.
Quote:

Originally Posted by sithclone3
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
• 07-17-2014, 12:09 AM
sithclone3
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
Code:

`public void calculate(double x0, int counter)`
as
Code:

`public void calculate (f(double));`
??
• 07-17-2014, 12:49 AM
jim829
Re: HELP :( I am writing a NewtonRaphson program.
Quote:

Originally Posted by jim829
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.

Code:

```double func(double val) {     return val*val; } double derivativeOfFunctionx(double val) {   return 2 * val; }```
or just code it directly,
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
• 07-17-2014, 02:06 AM
sithclone3
Re: HELP :( I am writing a NewtonRaphson program.
aight! thanks a lot man! I finished it!!! you always help so much man, thanks!
• 07-17-2014, 07:10 AM
JosAH
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