FInd maximum in cubic spline interpolant

27 views (last 30 days)
I got a set of data, x and y
I was trying to find an eqaution that bu basic fitting and it comes out that spline interpolant is the best one to fit in. (quad and cubic has a peak lower than actual value, so i can not use them)
Now, I want an actual equation that represents a line between two point.
Also I want to find a maximum y-value in a line of cubic spline interpolant, not from the actual data.
I tried findpeak, but it display the y-vale from my data.
So is there a way to do it??
x =
0.7000
0.7500
0.8000
0.8500
0.9000
0.9500
1.0000
1.0500
1.1000
1.1500
1.2000
1.2500
1.3000
y =
1.0e+003 *
1.8367
1.9180
1.9953
2.0677
2.1333
2.1883
2.2251
2.2318
2.2097
2.1746
2.1356
2.0958
2.0562
plot (x,y)
using basic fit tool, clicked on a spline interpolant
and got stuck from here
Cheers,

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 26 Mar 2014
Edited: Andrei Bobrov on 27 Mar 2014
EDIT
new data
x = [0.70000
0.75000
0.80000
0.85000
0.90000
0.95000
1.00000
1.05000
1.10000
1.15000
1.20000
1.25000
1.30000];
y = [1836.7
1918.0
1995.3
2067.7
2133.3
2188.3
2225.1
2231.8
2209.7
2174.6
2135.6
2095.8
2056.2];
for MATLAB R2013a and later
[~,ii] = findpeaks(y);
F = griddedInterpolant(x,y,'spline');
xmaxs = arrayfun(@(xx)fminsearch(@(x)-F(x),xx),x(ii));
x1 = linspace(x(1),x(end),100);
plot(x,y,'go',x1,F(x1),'b-',xmaxs,F(xmaxs),'r+');
for older releases MATLAB
[~,ii] = findpeaks(y);
F = interp1(x,y,'spline','pp');
xmaxs = arrayfun(@(xx)fminsearch(@(x)-ppval(F,x),xx),x(ii));
x1 = linspace(x(1),x(end),100);
plot(x,y,'go',x1,ppval(F,x1),'b-',xmaxs,ppval(F,xmaxs),'r+');

More Answers (2)

Mark Kittisopikul
Mark Kittisopikul on 12 Dec 2017
You can calculate the maximum by using calculus by finding a zero of the first derivative where the second derivative is negative.
x = [0.70000
0.75000
0.80000
0.85000
0.90000
0.95000
1.00000
1.05000
1.10000
1.15000
1.20000
1.25000
1.30000];
y = [1836.7
1918.0
1995.3
2067.7
2133.3
2188.3
2225.1
2231.8
2209.7
2174.6
2135.6
2095.8
2056.2];
Find the spline interpolant in piecewise polynomial form.
>> sp = spline(x,y)
sp =
struct with fields:
form: 'pp'
breaks: [1×13 double]
coefs: [12×4 double]
pieces: 12
order: 4
dim: 1
Take the derivative of the spline:
>> fnder(sp)
ans =
struct with fields:
form: 'pp'
breaks: [1×13 double]
coefs: [12×3 double]
pieces: 12
order: 3
dim: 1
Find the zeros of the 1st derivative:
>> fnzeros(fnder(sp))
ans =
1.0352
1.0352
Confirm the second derivative is negative:
>> fnval(fnder(fnder(sp)),fnzeros(fnder(sp)))
ans =
1.0e+04 *
-1.2996
-1.2996
Check the zero of the first derivative:
>> fnval(fnder(sp),fnzeros(fnder(sp)))
ans =
1.0e-11 *
0.2558
0.2558
Plot a nice figure for visual verification.
>> figure; fnplt(sp);
hold on; plot(fnzeros(fnder(sp)),fnval(sp,fnzeros(fnder(sp))),'o');
grid on
hold on; plot(fnzeros(fnder(sp)).',ylim.','-');
hold on; plot(x,y,'ko');

John D'Errico
John D'Errico on 12 Dec 2017
Edited: John D'Errico on 12 Dec 2017
Too late possibly, but you can always use my SLM toolbox , which has tools that can operate on splines created by tools like spline or pchip.
Based on the vectors x and y as you posed...
pp = spline(x,y);
[maxf,maxloc] = slmpar(pp,'maxfun')
maxf =
2233.2
maxloc =
1.0352
This is the global maximum value of the spline created in pp.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!