Linear regression in x log plot matlab
11 次查看(过去 30 天)
显示 更早的评论
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]
mdl = fitlm(x,y);
hold on
plot(mdl)
set(gca, 'XScale', 'log')
0 个评论
回答(1 个)
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!