How to find the maximum point of a fitted curve
Show older comments
I am trying to find the maximum point of a fitted curve, the fitting method is being done using smoothing splines. I have looked around on how this can be done and found the following technique in the link below, which seems to be exactly what I am trying to do, except that I am getting a wrong output.
with reference to similar problem that I found in matlab answer in the link below
Now the output that I am getting is the attched plot where the maximum point appear to have a wrong location on the x-axis, is there any way to fix it or at least to figure out what is causing the issue?
attached below is the used code to generate the plot, I attach also a sample of the data used
function [fitresult, gof] = createFit3(x, y)
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.Normalize = 'on';
opts.SmoothingParam = 0.99602;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
% figure( 'Name', 'untitled fit 1' );
plot( fitresult, xData, yData );
[ymax,xloc] = slmpar(coeffvalues(fitresult),'maxfun');
hold on
plot(xloc,ymax,'b*');
many thanks,
1 Comment
John D'Errico
on 24 Oct 2022
What was wrong with the result? (Since I wrote the SLM tools.)
Can I test your code? Of course not. We are not given the routine prepareCurveData. So how can I even test anything out?
MAYBE, that function just does
xdata = x;
ydata = y;
but how can I know?
Accepted Answer
More Answers (1)
Mathieu NOE
on 24 Oct 2022
hello
a simple and compact code that only uses this Fex submission
code
load('x&y.mat')
z = smoothn(y); % Regular smoothing
[ymax,idx] = max(z);
xloc = x(idx);
plot(xloc,ymax,'b*',x,y,'r.',x,z,'k','Markersize',20,'LineWidth',2)
result :

2 Comments
Salma fathi
on 24 Oct 2022
Mathieu NOE
on 24 Oct 2022
hello again
yes there is a possibility to change the smoothing factor

load('x&y.mat')
z = smoothn(y); % Regular smoothing, S auto
zs = smoothn(y,4); % Regular smoothing, S used % Z = SMOOTHN(Y,S) smoothes the array Y using the smoothing parameter S.
[ymax,idx] = max(zs);
xloc = x(idx);
plot(xloc,ymax,'b*',x,y,'r.',x,z,'k',x,zs,'g','Markersize',20,'LineWidth',2)
legend('peak','data','smoothn auto','smoothn S = 4');
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!