# Finding derivative of a function

How do we find a derivative of a function?

There are many different ways to solve the derivative.

Let’s use Newton difference quotient and evaluate derivative of

\[ f(x)= x^2 \]Formula:

$f^\prime(x)$ = $\lim_{h = 0}$ = \(\frac{f(x_0+h)-f(x)}{h}\) (1.0)

h:range[slope] between 2 points

x0 and x+h are data points at f(x0) and f(x+h). The red line is secant.

Example: Determine the derivative of $f(x^2)$ at x = 4

Step1: evaluate h at any 2 points x_0 = 5 x_0 = 8 ,h=3

$f(4) = $$\frac{f(4+3)-f(5)}{3}$ = $\frac{49-25}{3}$ = 8 $(1.0)$

Higher Order Derivative Approximation(Finite difference):

But we want to make a general application for a derivative.For instance we want to calculate solution at f”(2) for the function $f(x) = {x^3} + 2x$

$f”(x)(x)$ = $1 \cdot 1 \cdot f(2+(2-0) \cdot 2) – 1 \cdot 1 \cdot 2 \cdot f(2+(2-1) \cdot 2) + 1 \cdot 1 \cdot f(2+(2-2) \cdot2)$

$f”(x) = $ $\frac{228-144+12}{8}$ = 12 $(1.1)$

Symbolic derivative checking if the answer is correct:

$f”(x) = x^3 + 2x = 3x^2 + 2 = 6x = 12$

Source Code:

public class Main { public static int calcf2(int val_n) { int sum = 1; if(val_n == 1) { sum = 1; } else { for(int i = 1;i<=val_n;i++) { sum *=i; }`} return sum; }`

public static void main(String[] args) { int maxn = 2; // second derivative int x_0 = 2; int h_0 = 2; int value_n = maxn; int nv = calcf2(maxn); // calculate Factorial number int sumval = 1; int sign = 1; // operator 1 (positive) ,-1 (negative) int solution = 0; int[] function = new int[]{ 1,0,2 ,0}; // [ax^3 bx^2 bx c] expression of function in vector form for(int j = 0;j<=maxn;j++) { // loop till end of n`sumval = nv/((calcf2(value_n-j))*calcf2(j)); // calculate binomial coeffiecient n!/(n-k)!`

`if(j%2 == 0) { // change signs over index (-1)^i`

`sign = 1;`

`}`

`else {`

`sign = -1;`

`}`

`int fkt = function(function,j,x_0+(maxn-j)*h_0); // calculate function`

`solution += sign*sumval*fkt; // add to solution with operator and binomial coefficient`

} System.out.println(solution/(h_0h_0h_0)); // try to avoid using Math.pow Library because of high complexity operations(if possible) } static int function(int function[],int j,int x) { // function Method with 3 overloaded parameters int sum = 0; for (int cnt = 0;cnt < function.length;cnt++) {`if(cnt == function.length-1 && function[cnt] == 0) {`

`sum += 0; } else`

`sum += function[cnt]*Math.pow(x,function.length-cnt-1);`

`} return sum; // call back a result`

} }

Solution : 12