MATLAB: Linear fit line for multiple Y-axis

polyfit. polyval

I need to plot the best fit linear line for multiple y-axis data. I am able to do this with single y-axis but do not know it for multiple y-axes scenario.
(Here is the graph which i want to plot, but the best fit linear line in it, is corresponding to only single y-axis data.)

Best Answer

You'll get same coefficients if you fit
b=polyfit(t,mean(y,2)); % assume columns are variables, rows, observations
as will by fitting all data by duplicating the time vector for all columns and fitting all observations individually. Statistics will be somewhat different owing to the independent variables, but if all one cares about are the coefficients, the average works and is simpler to code.
x=[1:10].'; % an x-vector
y=rand(10,10); % 10 column vectors to go with x
>> b1=polyfit(x,mean(y,2),1) % fit mean by column
b1 =
0.0178 0.3858
>> b2=polyfit(repmat(x,10,1),y(:),1) % fit all observations individually
b2 =
0.0178 0.3858
>>
As can see, one gets same coefficient array either way -- altho in reality it's not that much more to "do it right".
Related Question