Fitting lines to a scatter plot

5 ビュー (過去 30 日間)
Honey
Honey 2020 年 5 月 11 日
回答済み: Ameer Hamza 2020 年 5 月 11 日
Hello,
I have a scatterplot which I want to fit lines to it. but not one line, I want to consider my data in two parts and for each part I want to fit a line.
How should I do for this?
I used to apply lsline for fiting up to now. below you can find what I want to do. for example the threshould is 2000. Before and after that I want to fit a seperate line.
Thanks in advance.

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 11 日
See this example. It does not use lsline. It uses matrix operation to find the least square estimation for the data. It specifies the point to partition the dataset.
% Example dataset
x = rand(1, 10000)*3000;
y = randn(1, 10000)*1 - ((x-1000)/1000).^2;
x_partition = 1500;
x1 = x(x <= x_partition);
y1 = y(x <= x_partition);
x2 = x(x > x_partition);
y2 = y(x > x_partition);
X1 = [x1(:) ones(size(x1(:)))];
Y1 = y1(:);
X2 = [x2(:) ones(size(x2(:)))];
Y2 = y2(:);
coeff1 = X1\Y1; % least suare estimation of line
coeff2 = X2\Y2;
figure;
ax = axes();
hold(ax);
scatter(x1, y1, 'b.')
scatter(x2, y2, 'b.')
plot(x1, X1*coeff1, 'r', 'LineWidth', 3);
plot(x2, X2*coeff2, 'r', 'LineWidth', 3);

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by