2
\$\begingroup\$

1.11 Arranging the derivative of a function
The derivative of any function f (x) at x0 can be estimated according to the following formula:

enter image description here

Write a program that prints three columns of numbers: x, f (x) and the derivative f '(x) for some simple functions, e.g. sin (x) or arctan(x).

public static class Derrivative
{
    public static double Compute(Func<double, double> f, double x0, double h)
    {
        return (f(x0+h) - f(x0-h)) / (2*h);
    }
}

class Program
{
    static void Main(string[] args)
    {
        const int N = 10;

        Console.WriteLine("Derivative of sin(x)");

        Func<double, double> function = Math.Sin;

        for (int i = 0; i < N; i++)
        {
            Console.WriteLine($"{i}\t{function(i)}\t{Derrivative.Compute(function, i, 0.5)}");
        }

        Console.WriteLine();

        Console.WriteLine("Derivative of arctan(x)");

        function = Math.Atan;

        for (int i = 0; i < N; i++)
        {
            Console.WriteLine($"{i}\t{function(i)}\t{Derrivative.Compute(function, i, 0.5)}");
        }

        Console.ReadLine();
    }
}

Kindly, review this source code.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Separate the for loop to a function printFunction(f, N)

Since you are using a known formula I suggest you use the names in the formula. Meaning i->x function->f.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.