INTERNAL: Calculating the derivative of a polynomial (POLY_DER)
Anonym
Topic:
This is an example of a simple function that calculates the derivative of a polynomial.Discussion:
function POLY_DER, p
;returns the first derivative of the polynomial
if (n_elements(p) le 1) then return, 0
pder=(p*lindgen(n_elements(p)))[1:n_elements(p)-1]
return, pder
end
This simple function calculates the derivative of a polynomial. The polynomial coefficients are on the same format as for example IDL's POLY function.
Here is an example of using this funciton:
IDL> y=[4,-3,2,1] ; define a polynomial y(x) = 4 - 3x + 2x^2 + x^3
IDL> yder=poly_der(y) ; calculate y'(x) (the derivative)
IDL> print, fz_roots(yder) ; use fz_roots to solve y'(x) = 0 and find the x values for the local min/max points
( -1.86852, 0.000000)( 0.535184, 0.000000)
IDL> x=findgen(700)/100-4 ; generate x values from -4 to +3 for plotting
IDL> plot, x, poly(x,y) ; plot y(x)
IDL> oplot, x, poly(x,yder), color='ff'xL ; plot y'(x)
IDL> oplot, replicate((float(fz_roots(yder)))[0],2), !y.crange, color='ff00'xL ; plot a line at the first solution to y'(x)=0
IDL> oplot, replicate((float(fz_roots(yder)))[1],2), !y.crange, color='ff0000'xL ; plot a line at the second solution to y'(x)=0
Solution:
function POLY_DER, p
;returns the first derivative of the polynomial
if (n_elements(p) le 1) then return, 0
pder=(p*lindgen(n_elements(p)))[1:n_elements(p)-1]
return, pder
end