mXparser – user defined functions applied to Fundamental theorem of calculus

Fundamental Theorem of Calculus is a kind of a link between two most important calculus concepts: derivative and integral.

Fundamental Theorem of Calculus – formal statement

For continuous real-valued function $$f:[a,b]\to\mathbb{R}$$ defined on closed interval $$[a,b]$$ let $$F:[a,b]\to\mathbb{R}$$ be the function given by

$$F(x)=\int_a^x f(t)\text{d}t$$

The $$F$$ is uniformly continuous on $$[a, b]$$, differentiable on the open interval $$(a, b)$$, and

$$F'(x)=f(x)$$

Fundamental Theorem of Calculus – mXparser test

import org.mariuszgromada.math.mxparser.*;
...
/* Function */
Function f = new Function("f(x) = sin(x)");
		
/* Antiderivative */
Function F = new Function("F(x) = int(f(t), t, 0, x)", f);
		
/* function = derivative ( antiderivative ) */
Argument x = new Argument("x = pi");
Expression e = new Expression("f(x) - der(F(x), x)", x, f, F);
mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Computing time = " + e.getComputingTime() + " s.");
Res : f(x) - der(F(x), x) = 6.237833817291525E-8
Computing time = 0.411 s.

Best regards,
Mariusz Gromada

12 thoughts on “mXparser – user defined functions applied to Fundamental theorem of calculus

  1. Hello, i feel that i saw you visited my website so i got here to go back the
    prefer?.I’m trying to find issues to enhance my site!I assume its ok to use a few of your ideas!!

    1. Formula string is a String type – if limitation exist this is related to String class. Additionally internal iterators are “int” types, so forula length is definitely limited by max int 🙂

  2. I need your help i use mxparser for a curve discussion programm, I use the 2nd derivation and sometimes it is Nan, although it shouldn’t be. Any idea to fix it?
    Examples:
    Expression e2 = new Expression(“solve( der( der(” + fkt + “, x), x), x, ” + z + “,” + (z + 1) + “)”, x);
    Expression e2ex = new Expression(“solve( der( der(” + fkt + “, x), x), x, ” + i + “,” + (i + 1) + “)”, x);

  3. ty for your fast response, here is the code:
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package projekt.syp;

    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    import java.util.TreeSet;

    import javafx.scene.control.Alert;
    import javafx.scene.control.Alert.AlertType;
    import org.mariuszgromada.math.mxparser.Argument;
    import org.mariuszgromada.math.mxparser.Expression;

    /**
    * @author laura
    */
    public class Extrempunktrechner {

    public static double eps = 0.0001;

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    Map<String, Set> m = calcExtrem(“1/16*(x^4-24*x^2+128)”, “-5”, “5”);
    for (String s : m.keySet()) {
    System.out.println(s + “->” + m.get(s));
    }
    }

    public static Map<String, Set> calcExtrem(String fkt, String minlimit, String maxLimit) {
    Set hSet = new TreeSet(); //Set für Hochpunkte
    Set tSet = new TreeSet(); //Set für Tiefpunkte
    Set sSet = new TreeSet(); //Set für Sattelpunkte
    Map<String, Set> extrem = new TreeMap(); //Map für alle Punkte
    extrem.put(“Hochpunkt”, hSet);
    extrem.put(“Tiefpunkt”, tSet);
    extrem.put(“Sattelpunkt”, sSet);
    Double min, max;
    try {
    min = Double.parseDouble(minlimit); //min und max in Double umwandeln
    max = Double.parseDouble(maxLimit);
    if (max < min) {
    throw new NumberFormatException();
    }
    } catch (NumberFormatException nfe) {
    Alert alert = new Alert(AlertType.ERROR); //Fehler falls min oder max keine Zahl oder max kleiner wie min
    alert.setTitle("Min/Max Error!");
    alert.setContentText("Min oder Max war keine Zahl!!");
    alert.show();
    return null;
    }
    for (double i = min; i <= max; i++) { //alle Werte im Intervall auf Extrempunkte überprüfen
    Argument x = new Argument("x");
    Expression e = new Expression("solve(der(" + fkt + ", x), x, " + i + "," + (i + 1) + ")", x);
    double z = e.calculate(); //Wert der ersten Ableitung ausrechnen
    Expression e2 = new Expression("solve( der( der(" + fkt + ", x), x), x, " + z + "," + (z + 1) + ")", x); //Zweite Ableitung
    Expression e2ex = new Expression("solve( der( der(" + fkt + ", x), x), x, " + i + "," + (i + 1) + ")", x); //Falls irgendwie z Nan ist, jedoch die mit i ein Ergebnis rauskommen würde
    double erg = e2.calculate(), ergx = e2ex.calculate(); //Ergebnis kalkulieren
    if (erg < 0 || ergx 0 || ergx > 0) {
    extrem.get(“Tiefpunkt”).add(e.calculate());
    }
    }
    for (String s : extrem.keySet()) {
    Iterator iterator = extrem.get(s).iterator(); //Iterator erzeugen zwecks entfernung von NaN werten
    while (iterator.hasNext()) {
    Double d = iterator.next();
    if (Double.toString(d).equals(Double.toString(Double.NaN))) {
    iterator.remove();
    }
    }
    }

    return extrem;
    }

    }

    1. The problem is here.

      You are trying to run “solve(der(1/16*(x^4-24*x^2+128), x), x, -5.0,-4.0)”, but

      Function derf = new Function(“derf(x0) = der(1/16*(x^4-24*x^2+128), x, x0)”);
      mXparser.consolePrintln(derf.calculate(-5));
      mXparser.consolePrintln(derf.calculate(-4));

      shows that derf do not change sign in “-5” and “-4”
      [mXparser-v.4.2.0] -16.250000002983143
      [mXparser-v.4.2.0] -4.000000002415618

      thus it can not be solved numerically causing z = NaN

Leave a Reply

Your email address will not be published. Required fields are marked *