Question

For a programming project in Calculus we were instructed to code a program that models the Simpson's 1/3 and 3/8 rule.

We are supposed to take in a polynomial(i.e. 5x^2+7x+10) but I am struggling conceptualizing this. I have began by using scanner but is there a better way to correctly read the polynomial?

Any examples or reference materials will be greatly appreciated.

Was it helpful?

Solution

I'd suggest that you start with a Function interface that takes in a number of input values and returns an output value:

public interface Function {
    double evaluate(double x);
}

Write a polynomial implementation:

public class Poly {

    public static double evaluate(double x, double [] coeffs) {
        double value = 0.0;
        if (coeffs != null) {
            // Use Horner's method to evaluate.
            for (int i = coeffs.length-1; i >= 0; --i) {
                value = coeffs[i] + (x*value);
            }
        }
        return value;
    }
}

Pass that to your integrator and let it do its thing.

OTHER TIPS

A simple way (to get you started) is to use an array.
In your example: 5x^2 + 7x + 10 would be:
{10,7,5}
I.e. at index 0 is the factor 10 for x^0 at index 1 is 7 for x^1 at index 2 is 10 for x^2.

Of course this not the best approach. To figure out way figure out how you would represent x^20

In java it would be easiest to pre-format your input and just ask for constants--as in, "Please enter the X^2 term" (and then the X term, and then the constant).

If that's not acceptable, you are going to be quite vulnerable to input style differences. You can separate the terms by String.split[ting] on + and -, that will leave you something like:

[5x^2], [7x], [10]

You could then search for strings containing "x^2" and "x" to differentiate your terms

Remove spaces and .toLowerCase() first to counter user variances, of course.

When you split your string you will need to identify the - cases so you can negate those constants.

You could do two splits, one on + the other on -. You could also use StringTokenizer with the option to keep the "Tokens" which might be more straight-forward but StringTokenizer makes some people a little uncomfortable, so go with whatever works for you.

Note that this will succeed even if the user types "5x^2 + 10 + 7 x", which can be handy.

I believe parsing is my problem. I am somewhat new to java so this is troubling me.

You should use a parser generator.

A parser generator is a tool that reads a grammar specification and converts it to a Java program that can recognize matches to the grammar. In addition to the parser generator itself, JavaCC provides other standard capabilities related to parser generation such as tree building (via a tool called JJTree included with JavaCC), actions, debugging, etc.

JavaCC's FAQ answers How do I parse arithmetic expressions?

See the examples that come with JavaCC.

See any text on compiling.

See Parsing Epressions by Recursive Descent and a tutorial by Theodore Norvell.

Also, see JavaCC - Parse math expressions into a class structure

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top