Register to reply 
Computing derivatives/integrals of userdefined functions 
Share this thread: 
#1
Mar509, 12:58 PM

P: 618

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.



#2
Mar509, 02:12 PM

P: 287

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 userdefined function for a given value of x. You could write this by yourself (you would have to write a parser that would recognize wellformatted 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 in to 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
Mar509, 04:45 PM

P: 618

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
Mar509, 07:45 PM

P: 223

Computing derivatives/integrals of userdefined functions
* That function returns 0. * It has integer types, where you probably want to use a floatingpoint 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:
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/ 


Register to reply 
Related Discussions  
Piecewisedefined functions  Calculus  2  
All of the functions that achieve defined  Calculus & Beyond Homework  7  
A matlab question(about user defined functions)  Math & Science Software  0  
Starting point not defined for contour integrals?  Calculus  1  
Well defined integrals  Calculus & Beyond Homework  2 