Clear Filters
Clear Filters

I dont know how to plot more than one regressions in one plot, like the picture i did in excel. I tried basic fitting tool but could not success, and i have look at multiple regression but could not understand becaus of no example. Thank for any help

2 views (last 30 days)

Accepted Answer

Star Strider
Star Strider on 2 May 2016
Something like this should work:
x = 1:10;
y1 = rand(1, 10);
y2 = rand(1, 10);
DM = [x', ones(length(x),1)]; % Design Matrix
B1 = DM \ y1'; % Estimate Parameters Of ‘y1’ Fit
B2 = DM \ y2'; % Estimate Parameters Of ‘y2’ Fit
y1_fit = DM*B1; % Calculate Fit For ‘y1’
y2_fit = DM*B2; % Calculate Fit For ‘y2’
figure(1)
plot(x, y1, '.b', x, y2, '.r') % Plot Data
hold on
plot(x, y1_fit, '--b', x, y2_fit, '--r') % Plot Fitted Regression Lines
hold off
grid

More Answers (1)

Image Analyst
Image Analyst on 2 May 2016
If you want a polyfit() example, see attached. Like Star showed, you just do the fit first for the first set of data, and plot that fit, then call hold on, and do it again for the second set of data.

Community Treasure Hunt

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

Start Hunting!