How to shift a fitted curve such that it would coincide on some point
6 views (last 30 days)
Show older comments
we are trying to shift the fiited curve that we have in the figure below such that its peak point (in black) would coincide on the green point. Is there any function that would simplify doing this for us or what would be a simple way to acheive this. Taking into account that I have the coordinates for both points but I am not sure how I can shift the full curve. I am doing the fitting using smoothing splines.
This is how I am generating the plot
plot(x,y, '-.', 'LineWidth', 1.5, 'MarkerSize', 10);
hold on
[fitresult, gof]= createFit(x, y); %using smoothing splines throgh the curve fitting toolbox
hold on
plot(hmf2,NmF2,'g*', 'LineWidth', 1.5,'MarkerSize', 10)%this is the point that we want to shift the curve to it
[ymax,fitPeakxloc] = slmpar(coeffvalues(fitresult),'maxfun'); %this is to find the max point of the fitted curve
F = struct(fitresult);
fitPeakxloc=fitPeakxloc*F.stdx + F.meanx;
hold on
plot(fitPeakxloc,ymax,'k*', 'LineWidth', 1.5,'MarkerSize', 10);
I attach the data that I a, using here,
Thanks in advance.
2 Comments
Karim
on 20 Dec 2022
Hi Salma, we will need to the functions createFit and slmpar in order to asses the outputs.
Answers (2)
Mathieu NOE
on 21 Dec 2022
hello
I simplified your code as I don't have the CFT
but the principle is simple by simply applying a x and y shiift on you x,y curve data
plot(x,y, '-.', 'LineWidth', 1.5, 'MarkerSize', 10);
hold on
plot(hmf2,NmF2,'g*', 'LineWidth', 1.5,'MarkerSize', 10)%this is the point that we want to shift the curve to it
[ymax,fitPeakxloc] = max(y); %this is to find the max point of the fitted curve
x_peak = x(fitPeakxloc);
plot(x_peak,ymax,'k*', 'LineWidth', 1.5,'MarkerSize', 10);
% shifted curve
x_delta = hmf2-x_peak;
y_delta = NmF2-ymax;
figure
xx = x+x_delta;
yy = y+y_delta;
plot(xx,yy, '-.', 'LineWidth', 1.5, 'MarkerSize', 10);
hold on
plot(hmf2,NmF2,'g*', 'LineWidth', 1.5,'MarkerSize', 10)%this is the point that we want to shift the curve to it
2 Comments
Mathieu NOE
on 22 Dec 2022
Sure !
I was too thinking that was not a big deal for you
fortunately there is the full answer below (I don't have the CFT)
Karim
on 22 Dec 2022
Hi @Salma fathi, you can evaluate the fitted curve using the feval function. Below I provided a demonstration on how you could shift the fitted data.
% load the data
load("data.mat")
% first create the fit, using the curve fitting toolbox
[xData, yData] = prepareCurveData( x, y );
[fitresult, gof] = fit( xData, yData, 'smoothingspline' );
% now evaluate the fit
x_fit = linspace(min(x),max(x),1000);
y_fit = feval(fitresult,x_fit);
% find the max point of the fitted curve
[y_peak, peak_idx] = max(y_fit);
x_peak = x_fit(peak_idx);
% deltas we need to shift the fitted curve
x_delta = hmf2 - x_peak;
y_delta = NmF2 - y_peak;
% now shift the fitted data
x_fit_shift = x_fit + x_delta;
y_fit_shift = y_fit + y_delta;
% plot original data
figure
hold on
plot(x,y,'-.')
plot(x_fit,y_fit)
scatter(hmf2,NmF2,25,'green')
scatter(x_peak,y_peak,50,'black','*')
title('Original fitted data')
legend("raw data","fitted data","shift point","max fitted data")
hold off
grid on
% plot the shifted data
figure
hold on
plot(x,y,'-.')
plot(x_fit_shift,y_fit_shift)
scatter(hmf2,NmF2,25,'green')
scatter(x_peak+x_delta,y_peak+y_delta,50,'black','*')
title('Shifted fitted data')
legend("raw data","shifted fitted data","shift point","max shifted fitted data")
hold off
grid on
0 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox 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!