3

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
user2132947
  • 33
  • 1
  • 5

4 Answers4

1

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

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

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.

Bill K
  • 62,186
  • 18
  • 105
  • 157
  • What if the type of polynomial is not limited exponentially? So the input could be a function (4x^7 + 10x^4 +3x + 10) or ( x^3)? – user2132947 Mar 04 '13 at 19:00
  • You have to make some assumptions about the input or build a general calculator. The more assumptions you make, the easier it is. Your suggested input wouldn't cause any difficulty, just a slight modification to pull off the powers as well as the constants... – Bill K Mar 04 '13 at 21:23
1

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • In your example, instead of hardcoding values(10.0, 7.0, 5.0) I could very well pass the coefficients that are consistent with the function the user entered in, correct? – user2132947 Mar 04 '13 at 19:05
0

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

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245