# How to shift a fitted curve such that it would coincide on some point

조회 수: 14(최근 30일)
Salma fathi 2022년 12월 20일
댓글: Mathieu NOE 2022년 12월 22일
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,
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Salma fathi 2022년 12월 20일

댓글을 달려면 로그인하십시오.

### 답변(2개)

Mathieu NOE 2022년 12월 21일
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표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Mathieu NOE 2022년 12월 22일
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 2022년 12월 22일
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.
% 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

댓글을 달려면 로그인하십시오.

### 범주

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by