How can I fit multiple lines through a common y-intercept?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!