Linear regression in x log plot matlab

24 ビュー (過去 30 日間)
Reto Kurz
Reto Kurz 2022 年 7 月 11 日
編集済み: Praveen Reddy 2023 年 9 月 1 日
Hey,
I would like to show the linear regression (straight line) of this data with the x-axis in log . So far I only get a mossy line.
Can I ask for a little help?
y = [156.547 173.256 126.514 131.344 158.640 105.313 132.794 134.500 130.340 126.969 91.931 107 104 75.717 85 65.500 145];
x = [36000 21000 18300 61300 53000 65000 37000 160000 470000 43000 93000 160000 82000 214000 120000 160000 40000]
x = 1×17
36000 21000 18300 61300 53000 65000 37000 160000 470000 43000 93000 160000 82000 214000 120000 160000 40000
mdl = fitlm(x,y);
hold on
plot(mdl)
set(gca, 'XScale', 'log')

回答 (1 件)

Praveen Reddy
Praveen Reddy 2023 年 9 月 1 日
編集済み: Praveen Reddy 2023 年 9 月 1 日
Hi Reto,
I understand that you intended to display the linear regression of the data with the x-axis in a logarithmic scale. However, you encountered a situation where the resulting line appeared curved instead of being straight. The reason you are seeing a curved line instead of straight line in your regression plot is because you are using a logarithmic scale for the x-axis. When you apply a logarithmic scale to the x-axis, the relationship between the x-values and the y-values will no longer be linear. To obtain a straight line in your regression plot, you should fit the model using a logarithmic transformation of the x-values. Please refer to the following modified code:
y = [156.547 173.256 126.514 131.344 158.640 105.313 132.794 134.500 130.340 126.969 91.931 107 104 75.717 85 65.500 145];
x = [36000 21000 18300 61300 53000 65000 37000 160000 470000 43000 93000 160000 82000 214000 120000 160000 40000];
mdl = fitlm(log(x), y); % Fit linear regression with logarithmic x-axis
plot(mdl); % Plot the linear regression line
set(gca, 'XScale', 'log'); % Set x-axis to logarithmic scale
ylim([20, max(y)]); % Set the y-axis limits from 20 to the maximum value of y
Modifying “mdl=fitlm(x,y)” to “mdl=fitlm(log(x),y)” will ensure that the relationship between the “x-values” and the “y-values” is linear, resulting in a straight line in the plot.
Hope this helps!

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by