Linear best fit model

Hello Community,
I have a query about best fit model.
From a large data set, I could generate a linear best fit line, which has a specific slope.
Now, now I want to compare how deviated this best fit is from another line having a different slope.
How can I force a specific slope value to draw another fit to this dataset?
Thanks in advance,
Ani

5 comentarios

William Rose
William Rose el 14 de Jul. de 2022
Editada: William Rose el 14 de Jul. de 2022
I am glad that helped. I moved my comment to the Answer section, which is what I had originally intended.
Adam Danz
Adam Danz el 14 de Jul. de 2022
> How can I force a specific slope value to draw another fit to this dataset?
What do you mean to draw another fit? There is only 1 best fit for a set of data.
Aniruddha Das
Aniruddha Das el 14 de Jul. de 2022
Thank you @William Rose. That helps.
Hi @Adam Danz, let me rephrase my question. That should not be the best fit, but a linear 'fit' which has a different slope compare to the best fit line.
Adam Danz
Adam Danz el 14 de Jul. de 2022
Thanks for the follow-up. Looks like @William Rose nailed it.
William Rose
William Rose el 15 de Jul. de 2022
@Adam Danz, thank you. Means a lot, coming from you!

Iniciar sesión para comentar.

Respuestas (1)

William Rose
William Rose el 14 de Jul. de 2022
Here is an example in which the fixed slope is 1.3 and the best fit slope is approximately 1.0.
x=0:1:100;
N=length(x); %number of data points
y=x+5*randn(1,N); %noisy data
% Find best-fit straight line
P = polyfit(x,y,1);
y1=P(1)*x+P(2);
rmse1=sqrt(sum((y-y1).^2)/N);
% Fit straight line with fixed slope
% The straight line should pass through the mean of the data
fixedslope=1.3;
xm=mean(x); ym=mean(y);
intercept=ym-fixedslope*xm;
y2=fixedslope*x+intercept;
rmse2=sqrt(sum((y-y2).^2)/N);
%% Display results on console
fprintf('Fit 1 (best fit) : y1=%.3fx%+.3f. RMS Error=%.2f\n',P(1),P(2),rmse1)
Fit 1 (best fit) : y1=1.014x-0.274. RMS Error=4.48
fprintf('Fit 2 (fixed slope): y2=%.3fx%+.3f. RMS Error=%.2f\n',fixedslope,intercept,rmse2)
Fit 2 (fixed slope): y2=1.300x-14.588. RMS Error=9.47
%% Plot results
figure; plot(x,y,'rx',x,y1,'-g',x,y2,'-b')
xlabel('X'); ylabel('Y'); grid on; legend('data','fit 1','fit 2')

Categorías

Más información sobre Linear and Nonlinear Regression en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Jul. de 2022

Comentada:

el 15 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by