I'm brand new at writing programs in any language and I'm trying to write a simple Newton-Raphson method which I think works in C (haven't compiled but going from a previous example that did work so I'm making this assumption) but realised that I don't know exactly how to call a method from Java... I'm getting these errors:

NewtRaphEx.java:5: class, interface, or enum expected
double f = (double); 
^
NewtRaphEx.java:6: class, interface, or enum expected
double df = (double); 
^
NewtRaphEx.java:39: class, interface, or enum expected
double f(double x) {
^

I assume I'm calling the f and df methods incorrectly? This is not for a homework question, simply my own practice!

My program so far is:

/* Newton Raphson Method*/

import Scanner.java.util; 

double f = (double); 
double df = (double); 

public class NewtRaphEx {

    public static void main (String[] args) {

        double xn, e_allow, fn, fnew, eps, dfn, dfnew; 
        int n;
        int nMax = 10000;

        e_allow = 0.001;
        xn = 0.6;

        while (eps <= e_allow || n < nMax) {
            for (n = 0; n <= nMax; n++) {
                fn = f(xn); 
                dfn = df(xn);
                dx = -(fn / dfn); 
                xnew = xn + dx; 
                eps = Math.abs(dx / xn);
                n = n + 1; 
                System.out.println("N" + "\t" + "X" + "\t" + "F(x)" + "\t" + "dF(x)" + "\t" + "delX" + "\t" + "X_new" + "\t" + "Epsilon");
                System.out.println(n + "\t" + xn + "\t" + fn + "\t" + dfn + "\t" + dx + "\t" + xnew + "\t" + eps);
            }
        }
    }
}



// Creating Function f = x - cos(3.5x)

double f(double x) {
    return (x - cos(3.5 * x)) 
}

double df (double x) {
    return (1 + sin(3.5 * x))
}
有帮助吗?

解决方案 3

Methods f and df are outside the class. They must be declared inside it. They should be static since they are not bound to an instance and you will call them from a static method.

This double f = (double); is illegal. You must provide a value or nothing double f;

sin and cos should be java.lang.Math.sin and java.lang.Math.cos unless you use static import.

其他提示

Java differs from C/C++ in many respects, including (particular to this issue) that you need most everything within a class definition e.g.

package ...

import ...

public class NewtRaphEx {
   ...
}

So the package defines the namespace, the imports define what namespaces are within scope and then the class definition (the same name as the .java filename) contains everything else. Note that you can only have one public class within a .java file (Java is restrictive in this fashion)

Given your question above I think a good starting point is to put everything within a main() method (analagous to main in a C program).

public class NewtRaphEx {
   public static void main(String[] args) {
       // put your code here...
   }
}

You intended to declare 2 functions, f and df before the main function, then define both f and df. You're allowed to do this kind of things in C, but this is not C, is Java.

In Java, you can only declare classes and interfaces at the top level, and the methods are defined inside them. So, these functions are invalid, along with their definitions below the class:

import Scanner.java.util; 

//invalid
double f = (double);
//invalid
double df = (double); 

public class NewtRaphEx {
    //class content...
}
//invalid
// Creating Function f = x - cos(3.5x)
double f(double x) {
    return (x - cos(3.5 * x)) 
}
//invalid
double df (double x) {
    return (1 + sin(3.5 * x))
}

To make your application work, one way would be to redefine these functions as static methods inside your class.

import Scanner.java.util; 

public class NewtRaphEx {
    //valid
    // Creating Function f = x - cos(3.5x)
    static double f(double x) {
        return (x - cos(3.5 * x)) 
    }
    //valid
    static double df (double x) {
        return (1 + sin(3.5 * x))
    }
    //class content...
}

I highly recommend you to review the basics of Java programming by following The Java Tutorials.

Sorry for my last answer (I didn't read carefuly).

There are 2 problems:

(1) d and df are functions/method, not variables. Then, you can safely delete the top declarations since the functions/method are declared at the bottom of the file:

//Delete this
double f = (double);
double df = (double); 

(2) You are trying to call a member function(f and df) inside a static function (main). That won't work. Then, you should declare those functions as static and move it inside the class:

 static double f(double x) {
        return (x - cos(3.5 * x)) 
    }

    static double df (double x) {
        return (1 + sin(3.5 * x))
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top