From: John D'Errico on
"Rupika Bandara" <rupika23(a)yahoo.com> wrote in message <hpc8qm$s25$1(a)fred.mathworks.com>...
> TideMan <mulgor(a)gmail.com> wrote in message <b95e7444-c62c-4f66-a905-60cfe75e5df7(a)x3g2000yqd.googlegroups.com>...
> > On Apr 5, 3:19 pm, "Rupika Bandara" <rupik...(a)yahoo.com> wrote:
> > > "John D'Errico" <woodch...(a)rochester.rr.com> wrote in message <hp7dt4$2q...(a)fred.mathworks.com>...
> > > > "Rupika Bandara" <rupik...(a)yahoo.com> wrote in message <hp7dae$om...(a)fred.mathworks.com>...
> > > > > I want to get second derivative of mode shape of a pin supported beam. I have deflection values corresponding to beam length. I do not know the function of the mode shape. Total length of beam is 2.4m. mode shape value is y.
> > > > > x=[0 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4]
> > > > > y=(-0.000230888 0.167612145 0.355166168 0.497419808 0.54998773 0.497419808 0.355166168 0.167612145 -0.000230888)
> > > > > How can I find the second derivative of this curve?
> > >
> > > > Interpolate with a spline, then differentiate the spline.
> > > > Or model it with a LOW order polynomial, then differentiate.
> > >
> > > > John
> > >
> > > Hi John,
> > > I do not know how to interpolate with a spline. Can you please explain me using an example? Please help me. This is urgent.
> > > Rupika
> >
> > Urgent? Humpf............
> > Lack of planning on your part DOES NOT constitute an emergency on
> > ours.
> >
> > For splines:
> > help interp1
>
> Hi John,
> Thank you very much for your help. I interpolated with a spline. This is the method I used
> x=[0:0.3:2.4]
> y=(-0.000230888 0.167612145 0.355166168 0.497419808 0.54998773 0.497419808 0.355166168 0.167612145 -0.000230888)
> xi=[0:0.3:2.4]
> yi = interp1(x,y,xi,'spline')
> firstder=diff(yi)
> seconder=diff(firstder)
> I used same point as x for xi also. As I want second derivative only at 7 points. Is this right?
> I checked it with xi=[0:0.15:2.4] also. But it gave very different values compared to first method. Please help me to clarify this.
> Rupika

Ok. You finally tried something. Had you not tried
something, I'd not have helped.

Note that diff is only a DIFFERENCE. When applied
to an equally spaced data series as you did, it will
not give a true derivative estimate. You need to
divide by the step size squared to get a valid second
derivative estimate. The simple application of diff
(twice) to the data series is not that bad though.
For example...

diff(diff(y))/0.3^2
ans =
0.21901 -0.50334 -0.99651 -1.1682 -0.99651 -0.50334 0.21901

Compare this to the results from a spline fitted to the
data series, then interpolated. This will give a more
accurate estimate in general. As an example of that
using my slm tool from the file exchange...

slm = slmengine(x,y);
slmeval(x,slm,2)
ans =
1.0615 0.21447 -0.51011 -1.051 -1.1592 -1.051 -0.51011 0.21447 1.0615

Note that slmeval is able to give estimates at the first
and last point in the series too. Also, I did not divide by
the step size here, since slmeval is actually differentiating
the spline. Find SLM here:

http://www.mathworks.com/matlabcentral/fileexchange/24443

Had you just wanted to fit a polynomial to the data
series, that too would have been easy enough. Lets
try it...

P = polyfit(x,y,4);
P2 = polyder(polyder(P));
polyval(P2,x)
ans =
1.3057 0.21969 -0.55605 -1.0215 -1.1766 -1.0215 -0.55605 0.21969 1.3057

I hate to try too high an order polynomial. A quartic
fit is probably entirely enough here. But since your
data is smooth, a 6th order polynomial actually gives
reasonable estimates. This is rarely a good choice,
but here it is successful.

P = polyfit(x,y,6);
P2 = polyder(polyder(P));
polyval(P2,x)
ans =
1.0376 0.21136 -0.52375 -1.0228 -1.1988 -1.0228 -0.52375 0.21136 1.0376

We can also fit a,d then differentiate a cubic spline.
Since I have the spline toolbox, I'll use it to do the
differentiation.

S = spline(x,y);
S2 = fnder(S,2);
ppval(S2,x)
ans =
0.98493 0.21901 -0.54691 -1.0514 -1.2266 -1.0514 -0.54691 0.21901 0.98493

Which of these estimates is best? That is difficult to
know.

John