Hi everyone,
I'm trying to write a basic program to find the root of an equation. I just tried compiling it for the first time and I get the following error:
RootFinder.java:6: <identifier> expected
public static double searchValue(double x0, x1, a) {
^
RootFinder.java:6: <identifier> expected
public static double searchValue(double x0, x1, a) {
^
2 errors
Here's my program so far:
public class RootFinder {
public static final double EPSILON = 0.00001;
public static double function(double x) {
return (Math.log(x + 1) + 1);
}
public static double searchValue(double x0, x1, a) {
double x;
boolean valid = (function(x0)<a) && (a<function(x1));
if (!valid) {
x = -1;
}
else if ( (x0 <= -1)||(x1 <= -1) ) {
x = -1;
}
else {
x = (x0 + x1)/2;
}
return function(x);
}
public static void main(String[] args) {
java.util.Scanner reader = new java.util.Scanner(System.in);
System.out.println("Enter a value for x0:");
double x0 = reader.nextDouble();
System.out.println("Enter a value for x1:");
double x1 = reader.nextDouble();
System.out.println("Enter a value for a:");
double a = reader.nextDouble();
searchValue(x0, x1, a);
if (function(x) == NaN) {
System.out.println("No such value exists, try again…");
} else if(Math.abs(function(x) - a) > EPSILON) {
System.out.println("The root of the equation [f(x) = ln(x + 1) +1] is " + function(x));
System.out.pringln("However the difference between the root and 'a' is too great. You can do better…");
} else {
System.out.println("The root of the equation is " + function(x));
}
}
}
I'm just not sure what I've done wrong...
Thanks in advance!