reneeccski

09-26-2004, 10:47 PM

Hello everyone! I was hoping someone might be able to give me a hand with the program I am writing for my intro cs class. We are studying algorithms and implementing them with java. I am totally new to programming, though I DO remember programming my TI-85 graphing calculator to output formulas back in high school several years ago! I am struggling with the code, not the math.

Here is the problem:

for f(x) = 0.1x^3 + 3.6

create an algorithm to find the root of the function and implement it in java

For the algorithm, I am using Newton's Method to find the zero. Newton's Method is:

xn+1 = xn - ( f(xn) / ( f'(xn) )

Here is the code I have written:

public class root {

public static void main(String[] args) { //begin arguments

//introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1])

double u=-10000;

double v;

//for function

for(u=-10000; u<=10000; v=(u-(((0.3*u*u*u)+36)/((0.9*u)*(0.9*u)))))

{if ((abs(double u))-(abs(double v)) <= 0.001);

{System.out.println("The root of the function is " );

//I used Newton's Method here. In general terms, the formula is:

//(x sub n+1) = (x sub n) - [ f(x) / f'(x) ]

}

}

}

}

Could someone tell me where I'm going wrong? I want the solution accurate to 0.001. The root is actually appriximately -3.301. It is very frustrating to try to implement algorithms in code when my instructor isn't actually teaching java to the class and just expects us to be able to do it!

--Renee

Here is the problem:

for f(x) = 0.1x^3 + 3.6

create an algorithm to find the root of the function and implement it in java

For the algorithm, I am using Newton's Method to find the zero. Newton's Method is:

xn+1 = xn - ( f(xn) / ( f'(xn) )

Here is the code I have written:

public class root {

public static void main(String[] args) { //begin arguments

//introduce variable u (the "guess" or [x sub n]), variable v ("guess +" or [x sub n+1])

double u=-10000;

double v;

//for function

for(u=-10000; u<=10000; v=(u-(((0.3*u*u*u)+36)/((0.9*u)*(0.9*u)))))

{if ((abs(double u))-(abs(double v)) <= 0.001);

{System.out.println("The root of the function is " );

//I used Newton's Method here. In general terms, the formula is:

//(x sub n+1) = (x sub n) - [ f(x) / f'(x) ]

}

}

}

}

Could someone tell me where I'm going wrong? I want the solution accurate to 0.001. The root is actually appriximately -3.301. It is very frustrating to try to implement algorithms in code when my instructor isn't actually teaching java to the class and just expects us to be able to do it!

--Renee