Linear regression in x log plot matlab

조회 수: 26 (최근 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 CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by