Linear regression in x log plot matlab

15 Ansichten (letzte 30 Tage)
Reto Kurz
Reto Kurz am 11 Jul. 2022
Bearbeitet: Praveen Reddy am 1 Sep. 2023
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')

Antworten (1)

Praveen Reddy
Praveen Reddy am 1 Sep. 2023
Bearbeitet: Praveen Reddy am 1 Sep. 2023
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!

Kategorien

Mehr zu Linear and Nonlinear Regression finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by