Computing derivatives/integrals of user-defined functions

In summary, two approaches to finding the derivative of a function were discussed: the numerical approach and the symbolic approach. The numerical approach involves evaluating a user-defined function at a given value of x using existing code or writing a parser to recognize expressions. The secant method is then used to approximate the derivative. On the other hand, the symbolic approach involves converting the function into tokens and recursively differentiating the tokens using properties such as the product, chain, quotient, and power rules. This approach requires a more complex process, especially when using a language like C, but can be easier with a symbolic programming language like Scheme. Ultimately, the choice between the two approaches depends on personal preference and the desired level of difficulty.
  • #1
clope023
992
131
does anyone have ideas or easy to read versions of code for this? I was looking for ways to do it using the limit definition of a derivative of a function and I've found a few applets but nothing that is that clear; any ideas are appreciated.
 
Technology news on Phys.org
  • #2
In general, you can adopt an approximate numerical approach, or an exact symbolic approach.

In the numerical approach, all you need to do is have some way of evaluating a user-defined function for a given value of x. You could write this by yourself (you would have to write a parser that would recognize well-formatted expressions, like a calculator), or you could use already existing code (you should be able to find lots of this kind of thing). So I enter

f(x) = 2*x + (exp(-3*x+2)) and I want f '(3)...

You approximate the derivative using the secant method. Basically,

f '(3) ~ f(3 + d) - f(3 - d) / 2d

You will want to play around with the value of d, but that's about all there is to it.The symbolic approach, on the other hand, will change everything to tokens, and recursively differentiate the tokens. The base cases will consist of polynomial expressions, sines and cosines, exponentials, etc. Then just build into the interpreter the properties of differentiation - you know, like the product, chain, quotient, power, etc. rules - and then you can change the user function into a new function.

Then you can just evaluate the derivative function at the desired point.

The answer to your question depends on which strategy you find more appealing.
 
  • #3
csprof2000 said:
In general, you can adopt an approximate numerical approach, or an exact symbolic approach.

In the numerical approach, all you need to do is have some way of evaluating a user-defined function for a given value of x. You could write this by yourself (you would have to write a parser that would recognize well-formatted expressions, like a calculator), or you could use already existing code (you should be able to find lots of this kind of thing). So I enter

f(x) = 2*x + (exp(-3*x+2)) and I want f '(3)...

You approximate the derivative using the secant method. Basically,

f '(3) ~ f(3 + d) - f(3 - d) / 2d

You will want to play around with the value of d, but that's about all there is to it.


The symbolic approach, on the other hand, will change everything to tokens, and recursively differentiate the tokens. The base cases will consist of polynomial expressions, sines and cosines, exponentials, etc. Then just build into the interpreter the properties of differentiation - you know, like the product, chain, quotient, power, etc. rules - and then you can change the user function into a new function.

Then you can just evaluate the derivative function at the desired point.

The answer to your question depends on which strategy you find more appealing.

interesting, this is what I tried to write for a derivative function but it gave me a 0 so obviously it wasn't correct (in C)

int diff(int function, int h)
{
function = ((function+h)-function)%h;
return 0;
}

you'll have to forgive me but I'm not following how the symbolic approach is done
 
  • #4
clope023 said:
Code:
int diff(int function, int h)
{
function = ((function+h)-function)%h;
return 0;
}

Several issues:

* That function returns 0.
* It has integer types, where you probably want to use a floating-point type like 'float' or 'double'.
* '%' is a modulus operator; '/' is division
* Your 'function' argument is of integer type ('int'), not function type. It is a number, not a function of a number.

The last issue is most significant, and problematic. You will, unfortunately, need to use C's function pointers to implement these kinds of things, functions which take functions as arguments. You will probably want to look this up in whatever C reference you have. For example, differentiation:

Code:
double diff(double (*f)(double), double x, double dx) {
  return ((*f)(x + dx) - (*f)(x)) / dx;
};

Or you can hack together a macro, which is somewhat simpler:

Code:
#define diff(f, x, dx) ((f((x)+(dx)) - f(x))/(dx))

Both approaches are difficult and dangerous (in the sense, it is easy to make errors and not be able to find them). The C language was not designed to support this type of programming. If you know any high-level languages, it becomes much easier. Python, for instance - very easy-to-use, easy-to-learn language:

Code:
def diff(f, x, dx):
   return (f(x+dx) - f(x)) * 1.0 / dx

(Note the lack of type annotations - Python will infer that f is a function, and x and dx are numbers. The 1.0 is there to coerce floating point division, as opposed to integer division with rounding.)

Symbolic differentiation is much harder. To implement it in C, you will need to figure out a way to represent the structure of a symbolic expression as a C data structure (probably a tree, made with pointers). Then you will need to write a function to differentiate such an expression, probably acting recursively - taking the expression, breaking it up, differentiating the parts, and combining them using (e.g.) the product rule.

However, it is easier if you use a symbolic programming language, like the educational language Scheme. For example, there is a worked example of symbolic differentiation in chapter 2 of the free book "Structure and Interpretation of Computer Programs". It's not particularly easy, but if you're motivated...

http://mitpress.mit.edu/sicp/
 
Last edited:

1. How do I compute the derivative of a user-defined function?

To compute the derivative of a user-defined function, you can use the power rule, product rule, quotient rule, or chain rule depending on the form of the function. If the function is in the form of ax^n, the derivative would be nx^(n-1), where a is a constant and n is the power.

2. Can I use software to compute derivatives of user-defined functions?

Yes, there are various software programs and computer languages such as MATLAB, Python, and Wolfram Alpha that can compute derivatives of user-defined functions. These programs use algorithms and numerical methods to calculate derivatives.

3. How can I check if my computed derivative of a user-defined function is correct?

You can check the correctness of your computed derivative by using the derivative rules and comparing your result with the expected result. You can also graph the original function and its derivative to visually verify if the slope at various points is correct.

4. Is there a way to compute integrals of user-defined functions?

Yes, integrals of user-defined functions can be computed using various methods such as substitution, integration by parts, and partial fractions. Computer software programs like MATLAB and Python also have built-in functions for computing integrals.

5. Are there any limitations to computing derivatives/integrals of user-defined functions?

There may be limitations depending on the form of the function and the method used to compute the derivative or integral. Some functions may not have a closed-form solution and require numerical methods for computation. Additionally, the accuracy of the computed result may depend on the precision of the input values and the chosen method of computation.

Similar threads

  • Programming and Computer Science
2
Replies
63
Views
3K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
32
Views
3K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top