Calculate Derivative Functions in Java

Fellow coders, in this tutorial we are going to learn how to calculate the derivative of a function in Java. As we all know that, the derivative of a function measures the change in the output value with respect to the change in its input value.

The derivative of a function in Java

Let us take a look at the function below:

y = f(x)

The derivative of the function above is the measure of the rate at which the value y of the above function changes with respect to the change of variable x. So, this is called a derivative of f with respect to x.

Working with the code:

We will calculate and print the derivative of:

a x^2 + b x + c

abstract class Expr {  
  public abstract Expr d(String v);  
  public Expr plus(Expr e2) { return new Plus(this, e2); }  
  public Expr times(Expr e2) { return new Times(this, e2); }  
}  
  
class Int extends Expr {  
  private final int n;  
  public Int(int m) { n = m; }  
  public Expr d(String v) { return new Int(0); }  
  public String toString() { return Integer.toString(n); }  
}  
  
class Var extends Expr {  
  private final String var;  
  public Var(String v) { var = v; }  
  public Expr d(String v) { return new Int(var.equals(v) ? 1 : 0); }  
  public String toString() { return var; }  
}  
  
class Plus extends Expr {  
  private final Expr e1, e2;  
  public Plus(Expr a, Expr b) { e1 = a; e2 = b; }  
  public Expr d(String v) { return new Plus(e1.d(v), e2.d(v)); }  
  public String toString() { return "(" + e1 + " + " + e2 + ")"; }  
}  
  
class Times extends Expr {  
  private final Expr e1, e2;  
  public Times(Expr a, Expr b) { e1 = a; e2 = b; }  
  public Expr d(String v)  
  { return new Plus(new Times(e1, e2.d(v)), new Times(e1.d(v), e2)); }  
  public String toString() { return "(" + e1 + " * " + e2 + ")"; }  
}  
  
public class derivative {  
  public static void main(String[] args) {  
    Expr x = new Var("x"), a = new Var("a"),  
         b = new Var("b"), c = new Var("c");  
    Expr e = a.times(x).times(x).plus(b.times(x)).plus(c);  
    System.out.println("d(" + e + ", x) = " + e.d("x"));  
  }  
}

The output of the above code is:

d(((((a * x) * x) + (b * x)) + c), x) = (((((a * x) * 1) + (((a * 1) + (0 * x)) * x)) + ((b * 1) + (0 * x))) + 0)

Leave a Reply

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