Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculus Limits with Java

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?

like image 874
hyper-neutrino Avatar asked Jul 10 '15 01:07

hyper-neutrino


People also ask

Is there a limit under calculus?

In Mathematics, a limit is defined as a value that a function approaches the output for the given input values. Limits are important in calculus and mathematical analysis and used to define integrals, derivatives, and continuity.

Can Matlab solve limits?

MATLAB provides the limit function for calculating limits. In its most basic form, the limit function takes expression as an argument and finds the limit of the expression as the independent variable goes to zero.


2 Answers

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.

like image 89
Rudi Cilibrasi Avatar answered Oct 06 '22 02:10

Rudi Cilibrasi


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.

like image 31
Derrops Avatar answered Oct 06 '22 01:10

Derrops