# Thread: HELP :( I am writing a NewtonRaphson program.

1. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## 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 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. ## 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.

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

4. ## 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

5. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

7. ## Re: HELP :( I am writing a NewtonRaphson program.

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:

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

Jos

8. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## 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 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 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

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

10. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

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.

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

12. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

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

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

13. ## 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

#### Posting Permissions

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