Calculus Limits with Java
Asked Answered
D

2

9

I wish to calculate limits (calculus) with Java. I have the following class Limit that can calculate limits:

package calculus;

public final class Limit {
    private Limit() {

    }

    public static final double limit(Function function, double approach) {
        double below = Limit.limitFromBelow(function, approach);
        double above = Limit.limitFromAbove(function, approach);
        return below == above ? below : Double.NaN;
    }

    public static final double limitFromBelow(Function function, double approach) {
        for (double d = approach - 10; d <= approach; d = approach
                - ((approach - d) / 10)) {
            if (function.apply(d) == Double.POSITIVE_INFINITY) {
                return Double.POSITIVE_INFINITY;
            } else if (function.apply(d) == Double.NEGATIVE_INFINITY) {
                return Double.NEGATIVE_INFINITY;
            } else if (Double.isNaN(function.apply(d))) {
                return function.apply(approach + ((approach - d) * 10));
            } else {
                if (d == approach) {
                    return function.apply(d);
                } else if (approach - d < 0.00000000001) {
                    d = approach;
                }

            }
        }
        return Double.NaN;
    }

    public static final double limitFromAbove(Function function, double approach) {
        for (double d = approach + 10; d >= approach; d = approach
                - ((approach - d) / 10)) {
            if (function.apply(d) == Double.POSITIVE_INFINITY) {
                return Double.POSITIVE_INFINITY;
            } else if (function.apply(d) == Double.NEGATIVE_INFINITY) {
                return Double.NEGATIVE_INFINITY;
            } else if (Double.isNaN(function.apply(d))) {
                return function.apply(approach + ((approach - d) * 10));
            } else {
                if (d == approach) {
                    return function.apply(d);
                } else if (d - approach < 0.00000000001) {
                    d = approach;
                }

            }
        }
        return Double.NaN;
    }
}

However, I was wondering: Is there another way to calculate limits other than exhaustion and recursive testing? Is there a more efficient method?

Derivation answered 10/7, 2015 at 1:12 Comment(1)
I would look into L'Hopital's rule for evaluating limits if i were you. It could be immensely helpful, though you may need to add a a few methods to the Function class. (as seen here: tutorial.math.lamar.edu/Classes/CalcI/LHospitalsRule.aspx )Bandsman
B
4

Your technique is called numerical approximation of a limit. It is widely used, simple to implement, and generally good enough for many applications. It is easy to use because all you need is a function that you can evaluate in order to apply it.

If you want another way, it is called symbolic or algebraic calculation of limits. In this case, you need to have more information than just the capability to evaluate an unknown function. You need to have an entire parse tree expression for a function including an indication of the independent variable. This is more than just the capability to evaluate the function at a given point. One example of this in Python is shown here:

http://scipy-lectures.github.io/advanced/sympy.html#limits

The symbolic style uses real math rules similar to how you might work out the limit by hand using rules of algebra or calculus.

Bibber answered 10/7, 2015 at 1:30 Comment(0)
J
4

Calculating many limit problems cannot be done numerically, ofter involves symbolic rules, such as L'Hopital's rule, and thererfore you need a symbolic algebra toolset, which aren't so great in java because there is no operation overloading, all the math guys go to python, or maple if you are doing some serious business. A numerical implementation alone will not be very good, you can't simply use numerical approximations for limmits, once you enter symbolic maths numerical approximations are very limitted to what you can acheive with them, and you need to check for convergence and all sorts of things but anyway I guess you can have a go for what its worth but don't use this in any serious application.

Joh answered 10/7, 2015 at 1:27 Comment(1)
I see that symbolic rules are important in calculus.Derivation
B
4

Your technique is called numerical approximation of a limit. It is widely used, simple to implement, and generally good enough for many applications. It is easy to use because all you need is a function that you can evaluate in order to apply it.

If you want another way, it is called symbolic or algebraic calculation of limits. In this case, you need to have more information than just the capability to evaluate an unknown function. You need to have an entire parse tree expression for a function including an indication of the independent variable. This is more than just the capability to evaluate the function at a given point. One example of this in Python is shown here:

http://scipy-lectures.github.io/advanced/sympy.html#limits

The symbolic style uses real math rules similar to how you might work out the limit by hand using rules of algebra or calculus.

Bibber answered 10/7, 2015 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.