Question

Translate the following code to java with comments x0 = 1 # The initial guess f(x)...

Translate the following code to java with comments

x0 = 1  # The initial guess
f(x) = x^2 - 2  # The function whose root we are trying to find
fprime(x) = 2 * x  # The derivative of the function
tolerance = 10^(-7)  # 7 digit accuracy is desired
epsilon = 10^(-14)  # Do not divide by a number smaller than this
maxIterations = 20  # Do not allow the iterations to continue indefinitely
solutionFound = false  # Have not converged to a solution yet

for i = 1:maxIterations
  y = f(x0)
  yprime = fprime(x0)
 
  if abs(yprime) < epsilon  # Stop if the denominator is too small
    break
  end
 
  global x1 = x0 - y/yprime  # Do Newton's computation
 
  if abs(x1 - x0) <= tolerance  # Stop when the result is within the desired tolerance
    global solutionFound = true
    break
  end
 
  global x0 = x1  # Update x0 to start the process again
end

if solutionFound
  println("Solution: ", x1)  # x1 is a solution within tolerance and maximum number of iterations
else
  println("Did not converge")  # Newton's method did not converge
end
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

public class HelloWorld{
public static double f(double x)
{
return x*x-2.0;
}
public static double fprime(double x)
{
return x*2.0;
}
  
public static void main(String []args){

double x0 = 1; //The initial guess

double tolerance = 1e-7; // 7 digit accuracy is desired
double epsilon = 1e-14; // Do not divide by a number smaller than this
int maxIterations = 20; // Do not allow the iterations to continue indefinitely
boolean solutionFound = false; // Have not converged to a solution yet
double x1=x0;
for(int i=1;i<=maxIterations;i++)
{
double y=f(x0);
double yprime = fprime(x0);
if(Math.abs(yprime) < epsilon)
{
break;
}
x1 = x0 - y/yprime;
if(Math.abs(x1 - x0) <= tolerance) // Stop when the result is within the desired tolerance
{
solutionFound = true;
break;
}
x0 = x1;

}
if(solutionFound)
{
System.out.println("Solution: "+x1); // x1 is a solution within tolerance and
  
}
else
System.out.println("Did not converge"); // Newton's method did not converge

}
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Translate the following code to java with comments x0 = 1 # The initial guess f(x)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT