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?'})
Best Answer