Ask Your Question
0

How to plot derivative and antiderivative of a spline

asked 2012-11-01 06:20:14 +0100

sagefan gravatar image

updated 2012-11-01 08:26:54 +0100

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)

output

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-11-01 09:05:18 +0100

achrzesz gravatar image

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
edit flag offensive delete link more

Comments

Thanks. What's about the plots?

sagefan gravatar imagesagefan ( 2012-11-01 14:07:02 +0100 )edit
0

answered 2012-11-01 07:04:44 +0100

sagefan gravatar image

updated 2012-11-01 07:06:28 +0100

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')

output

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.

edit flag offensive delete link more

Comments

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

kcrisman gravatar imagekcrisman ( 2012-11-01 12:13:49 +0100 )edit

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

sagefan gravatar imagesagefan ( 2012-11-01 14:08:01 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-11-01 06:20:14 +0100

Seen: 1,479 times

Last updated: Nov 01 '12