Archived

This topic is now archived and is closed to further replies.

laeuchli

finding derivatives.

Recommended Posts

Dear All, I seem to remember reading a article on writing programs to compute derivates, but I sure can remember where is was. If anyone knows, could they tell me? Thanks, Jesse www.laeuchli.com/jesse/

Share this post


Link to post
Share on other sites
Evaluating a derivative numerically is really easy. Remember its definition:

f''(x) = lim f(x+h) - f(x)
h->0 -------------
h

(Uh... Something doesn''t look right there. Anybody?)

You can apply that definition literally even with a calculator.

Cédric

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A much better approximation of f''(x) is usually given by (f(x+h)-f(x-h))/2h, which is second order accurate. The trick is to pick an optimal value for h. As a rule of thumb, set h to 10^(n/2), where n is the number of significant digits of the number representation.

Share this post


Link to post
Share on other sites
If you really need it, you can make symbollic derivation, using "jets". An old idea of mine that I never implemented is making a C++ class to manipulate 1-jets. This means, the value of a function together with the value of the first derivative. Something like this:


  
struct Jet{
double value,derivative;
Jet(double v, double d = 0.0):value(v),derivative(d){}
Jet operator+(const Jet &j){
return Jet(value+j.value,derivative+j.derivative);
}
Jet operator*(const Jet &j){
return Jet(value*j.value,derivative * j.value+value*j.derivative);
}
// etc.

};

Jet sin(const Jet &j){
return Jet(sin(j.value),cos(j.value)*j.derivative);
}



I haven''t tried this code, but I hope you get the idea. You can then define a function like

  
Jet my_func(Jet j){
return j*(sin(j)+j);
}

and you get both the value of the function and its derivative. How cool is that?

Share this post


Link to post
Share on other sites
quote:
Original post by laeuchli
I seem to remember reading a article on writing programs to compute derivates, but I sure can remember where is was. If anyone knows, could they tell me?



Was it an article posted here at GD.net?

Were you looking to compute symbolic derivatives, numerical approximations of derivatives or find exact derivatives of n order polynomials? I''m guessing the latter, but of course I could be wrong.

Cheers,

Timkin

Share this post


Link to post
Share on other sites
sorry, I can''t rember where I read it. I was looking for info on the latter. Basicly, I''m trying to figure out the algoritim my ti-92+ uses find derivates and integrals.
Jesse....

Share this post


Link to post
Share on other sites
quote:
Original post by laeuchli
sorry, I can''t rember where I read it. I was looking for info on the latter. Basicly, I''m trying to figure out the algoritim my ti-92+ uses find derivates and integrals.
Jesse....

For polynomials? That''s trivial.
(x^n)'' = n * x^(n-1)

If you''re looking for a numerical derivative, then AP''s method is probably the one used.

Cédric

Share this post


Link to post
Share on other sites
sorry, yes I know that. I also know how to do numerical calculation. I meant symbolic, not ployinminal. For example, what do I do if I have a more complex equations that I have to differinate? I know how to do it by hand, but I''m trying to find out if there is a well know good way to solve them on the computer. Or are they usally just computed numerically?
Thanks,
Jesse.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
To do symbolic derivation you will have to build a recursive data structure representing the expression. You can then determine the derivative by recursive substitution. For example, if your expressions are built from the the basic expressions: (expr + expr), (expr * expr), sin(expr), x, and numeral, the derivative of any expression can be determined by recursively applying the rules:

D((expr1 + expr2)) = (D(expr1) + D(expr2))
D((expr1 * expr2)) = ((D(expr1) * expr2) + (expr1 * D(expr2)))
D(sin(expr)) = (cos(expr) * D(expr))
D(x) = 1
D(numeral) = 0

Then you will probably want to simplify the resulting expression, but that''s another topic...

Share this post


Link to post
Share on other sites
The recursive parser thing is definitely the best way to go about the symbolic differentiator. An initial expansion simulates the sum and product rules (along with rules for sin, cos, natural logs, etc.), while the recursive nature simulates the chain rule.

Now, if you really want to blow your mind, write a symbolic integator I''ll let you off and assume the starting condition is (0,0). Muhaha!

Good luck. If anything, this should be a fun and educational endeavor.

Share this post


Link to post
Share on other sites
Numerically, you basically use the formulas given in the above posts. Remember, the derivative describes the rate of change, so it''s really the difference in y values over the difference in x values (slope), as the distance between those two x values approaches zero. So use a really small number instead of zero for how far apart they are So if you wanted to find the derivative at f(5), then take a really small interval (h), like 0.05, and find the slope between two points on either side of f(5):

f(5.05) - f(4.95)
-----------------
0.10

This is basically what AP said before about the midpoint rule.

Share this post


Link to post
Share on other sites
Folks,

Lets keep this thread on topic. laeuchli asked for a link to an article, not a discussion. Since no mention was given of game development or a work/development in progress, I''d prefer that any further posts to this thread be a link to the requested article, nothing more.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites