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.

asked Apr 14, 2020 at 18:10
\$\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.

answered Apr 15, 2020 at 12:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.