Results 1 to 13 of 13
 07102014, 06:24 PM #1Member
 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 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; } }
 07102014, 06:52 PM #2
Re: HELP :( I am writing a NewtonRaphson program.
how to even start my testClassIf you don't understand my response, don't ignore it, ask a question.
 07102014, 07:16 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,361
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07102014, 07:38 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07102014, 10:05 PM #5Member
 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.
 07102014, 11:27 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,361
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07112014, 08:56 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Re: HELP :( I am writing a NewtonRaphson program.
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 dowhile loop where the condition is diffierence >= MINIMUM. In pseudo code:
Java Code:do { xn= xf(x)/f'(x); difference= Math.abs(xxn); x= xn; } while (difference >= MINIMUM);
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07162014, 11:43 PM #8Member
 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 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; } }
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); } }
 07172014, 12:02 AM #9Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,361
 Rep Power
 9
Re: HELP :( I am writing a NewtonRaphson program.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07172014, 12:09 AM #10Member
 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 rewriteJava Code:public void calculate(double x0, int counter)
Java Code:public void calculate (f(double));
 07172014, 12:49 AM #11Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,361
 Rep Power
 9
Re: HELP :( I am writing a NewtonRaphson program.
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; }
Java Code:(val*val)/(2*val);
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 07172014, 02:06 AM #12Member
 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!
 07172014, 07:10 AM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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(xh))/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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

NewtonRaphson java code help
By Humraj in forum New To JavaReplies: 2Last Post: 06192013, 01:06 PM 
help in writing a program?
By baldy100 in forum New To JavaReplies: 3Last Post: 01102013, 05:06 PM 
Help with writing my own program
By DMarsh12 in forum New To JavaReplies: 4Last Post: 12302011, 04:34 AM 
how begin with writing a new program ?
By dimitrist in forum New To JavaReplies: 11Last Post: 05132008, 03:49 AM 
Help needed writing a program...
By Francis in forum New To JavaReplies: 2Last Post: 11222007, 03:03 PM
Bookmarks