How can I fit multiple lines through a common y-intercept?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Michael McDonald
le 22 Oct 2020
Modifié(e) : Michael McDonald
le 23 Oct 2020
I would like to linearly fit N sets of data to a functional form yi = mi * x + b, such that the lines have individual slopes mi but are required to share a single y-intercept b.
I've included some example code below for a simple case of two vectors y1 and y2 that perfectly lie on lines that would otherwise intercept the y-axis at y=1 and y=2. In this case with identical numbers of points, all perfectly on a line already, I'm pretty sure the "correct" answer would solve to a shared intercept on the black cross at y=1.5. For this simple case I could brute force it by looping through a bunch of y-intercepts, fitting to all of them, and minimizing the residuals, but is there a more elegant linear algebra way to solve this?
Thanks!
%% Initialization
x = [1:5];
m1 = 1;
b1 = 1;
y1 = m1*x + b1;
m2 = 2;
b2 = 2;
y2 = m2*x + b2;
%% Fitting
M1B1 = polyfit(x,y1,1);
M2B2 = polyfit(x,y2,1);
%% Plotting
% figure()
plot(x,y1,'bo',x,y2,'ro',[0 x],[b1 y1],'b--',[0 x],[b2 y2],'r--',0,(b1+b2)/2,'k+')
xlim([-1 5])
ylim([0 10])
title({'What''s the best red and blue line','to fit the data and share a y-intercept?'})
0 commentaires
Réponse acceptée
Matt J
le 22 Oct 2020
Modifié(e) : Matt J
le 22 Oct 2020
p=[x(:).^[1,0] ,0*x(:); 0*x(:),x(:).^[0,1]] \[y1(:);y2(:)];
m1=p(1)
b=p(2)
m2=p(3)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!