
clear
clc
close all
x=[0 1 2 3 4 5 6 7];
y=[0 3 5 7 9 11 15 19];
coeffit=polyfit(x,y,1);
slope=coeffit(1);
intercept=coeffit(2);
figure(1)
scatter(x,y,'filled')
hold on
x_fit = min(x):0.1:max(x);
% y_fit = slope * x_fit + intercept;
y_fit=polyval(coeffit,x_fit)
plot(x_fit, y_fit, 'r');
hold off
grid on
MATLAB中的线性回归示例:使用polyfit进行数据拟合
本文展示了如何在MATLAB中使用polyfit函数对给定的数据集[x,y]进行线性拟合,计算出斜率和截距,并通过scatter和plot函数生成数据点及拟合线图。
496

被折叠的 条评论
为什么被折叠?



