Thanks Dennis.
I got you.
My main consern was how to differentiate a series correctly.
I think the first procedure is more accurate.
Regards.
On 27 May 2011 16:27, Dennis Murphy <djmuser at gmail.com>
wrote:> Hi:
>
> A function and its spline approximation are not equivalent functions,
> hence neither are their corresponding derivatives. I modified an
> example from the splinefun() help page to illustrate this.
>
> op <- par(mfrow = c(2, 1))
> x <- seq(1, 9, by = 0.01)
> # u is the true function, u1-u3 are its symbolic derivatives wrt x
> # u1 = f', u2 = f'', u3 = f'''
> u <- expression(sin(pi * (x - 0.5)))
> u1 <- as.expression(D(u, 'x'))
> u2 <- as.expression(D(u1, 'x'))
> u3 <- as.expression(D(u2, 'x'))
> plot(x, eval(u), type = 'l', ylim = c(-30, 30),
> ? ? ylab = "", xlab = "",
> ? ? main = expression(f(x) == sin(pi * (x - 0.5))))
> lines(x, eval(u1), type = 'l', col = 'red')
> lines(x, eval(u2), type = 'l', col = 'blue')
> lines(x, eval(u3), type = 'l', col = 'green')
> # legend('topright', legend = c("f", "f'",
"f''", "f'''"),
> # ? ? ? ?col = c('black', 'red', 'blue',
'green'), lwd = 2)
>
> # y2 is an evaluation of u at each x point
> y2 <- sin((x-0.5)*pi)
> # Construct the spline function approximation to u
> f <- splinefun(x, y2)
> # Plot the interpolation function and its first three derivatives
> curve(f(x), 1, 10, col = "black", lwd = 1.5, ylim = c(-30, 30),
> ? ? ?ylab = "", main = 'Spline interpolation of f')
> curve(f(x, deriv=1), 1, 10, col = 'red', lwd = 1.5, add = TRUE)
> curve(f(x, deriv=2), 1, 10, col = 'blue', lwd = 1.5, add = TRUE)
> curve(f(x, deriv=3), 1, 10, col = 'green', lwd = 1.5, add = TRUE)
> par(op)
>
> Notice that the peaks and troughs of the derivatives of the spline
> approximation are not at the same x locations as in the original
> function. Also notice the linearity in the derivatives when x is
> between 9 and 10.
>
> I suppose you could improve the approximations by setting some knot
> points, but I don't have the time to chase down that hypothesis for
> you right now. I'll leave that as homework :)
>
> Your example is simpler since it is polynomial, but the concept is the
> same: the derivative of the interpolator shouldn't necessarily match
> the derivative of the function exactly. Obviously, though, you want
> them to be close.
>
> HTH,
> Dennis
>
> On Fri, May 27, 2011 at 1:23 AM, nandan amar <nandan.amar at
gmail.com> wrote:
>> Dear All,
>> I tried following for getting derivative of a polynomial in R
>>
>> i<- -10:10
>> x<-i*i*i+3*i*i+2
>> fun_spline<-splinefun(i,x)
>> plot(x,type="l")
>> lines(x,fx_spline(x, deriv=1), col='green')
>> lines(x,fx_spline(x, deriv=2), col='green')
>>
>> Now when I plot
>> 3*i*i + 6*i and 6*i + 6
>> the plot was not same for first deivative.
>> where as the 2nd derivative was same
>>
>> Is this a correct method for getting derivative.
>> where am I doing wrong as first derivative? lines(x,fx_spline(x,
>> deriv=1), col='green') is not correct.
>>
>> amar
>> --
>> Amar Kumar Nandan
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
--
Amar Kumar Nandan
Karnataka, India, 560100
http://aknandan.co.nr