Search code examples
c#visual-studioderivative

Complex derivative of the function at the point


I'm writing a method to find the derivative of a function at a point.

static void Main(string[] args)
    {
        double x = 1;
        double dx = 0.000001;
        double rate;
        rate = (((f(x + dx) - f(x)) / dx));            
    }

    static double f(double x)
    {            
        return Math.Cos(x);
        //return 20*x - 5*Math.Pow(x, 2)+8*Math.Pow(x, 5/4);
    }

Derivative of simple functions (like sin(x)) is calculated correctly, but a complex function (like (20*x - 5*Math.Pow(x, 2)+8*Math.Pow(x, 5/4))) - not true.


Solution

  • It looks as though the issue is caused by your integer division

    5 / 4 == 1
    5.0 / 4.0 == 1.25