# How to plot derivative and antiderivative of a spline

Suppose I have a spline like that:

from sage.gsl.all import spline
values = [(-3,-2),(-2,0),(1,2),(3,1),(4,5)]
interpolation = spline(values)
plot(interpolation,(-3,5)) + list_plot(values)


What's the easiest way to plot the derivative and the antiderivative functions of the spline?

Note that I want just the plot, not a symbolic expression of both, so numerical methods should apply. However getting a symbolic expression would be the best.

How can I find maxima, minima and roots of the spline? Numerically would be good enough.

edit retag close merge delete

Sort by » oldest newest most voted

I have put something together but I think there might be better solutions:

from sage.gsl.all import spline
values = [(-3,-2),(-2,0),(1,2),(3,1),(4,5)]
interpolation = spline(values)
import scipy as sp
var('x')
f = lambda x: sp.derivative(interpolation,x)
F = lambda x: numerical_integral(interpolation,-3,x)[0]
plot(interpolation,(-3,5)) + list_plot(values) + plot(f,(-3,5),color='red') + plot(F,(-3,5),color='green')


local_maximum = plot(interpolation,(-4,3)).ymax()
print('local maximum ' + str(local_maximum))
print('root ' + str(find_root(interpolation,-3,4)))


Output:

local maximum 2.4229598692250027
root -2.0


However this way I don't get the x value of my local maximum.

more

Yeah, we don't really have numerical derivative/antiderivative stuff in Sage yet. Perhaps that would be useful.

( 2012-11-01 06:13:49 -0500 )edit

How can I get the x value for the maximum and why does the red plot only go from -2 to 3?

( 2012-11-01 08:08:01 -0500 )edit

Maxima can give you the exact values of the local maximum coordinates:

maxima('load(interpol)')
maxima('v:[[-3,-2],[-2,0],[1,2],[3,1],[4,5]]')
maxima('cspline(v)')
p4=maxima('part(cspline(v),4,1)').sage();p4
diff(p4,x).roots()
#[(-1/58*sqrt(35558) - 88/29, 1), (1/58*sqrt(35558) - 88/29, 1)]
diff(p4,x).roots(ring=RR)
#[(-6.28566017270985, 1), (0.216694655468470, 1)]
p4(x=0.216694655468470)
#2.42298183710243

more

( 2012-11-01 08:07:02 -0500 )edit