Linear Regression not working

3 次查看(过去 30 天)
Grace Rembold
Grace Rembold 2019-11-25
I'm trying to plot the same data multiple times but with a different degree of curve each time. Every time I try, the code runs through fine, but my plot does not show any line fit at all, it just shows my data points.
here is my current code.
X =
NaN
0.2200
0.3000
0.4400
0.4900
0.7000
0.8000
0.9000
1.1000
1.0000
1.1500
1.2000
1.2200
1.2600
1.2500
1.2700
1.2000
1.2200
1.2600
1.2000
1.2200
1.2600
1.2000
1.2200
1.2600
1.2500
t =
NaN
0
4
7
10
13
16
22
25
30
33
36
40
48
51
55
60
80
100
120
140
160
180
200
220
240
x=(t);
y6=(X);
% First Degree
subplot(1,3,1);
p=polyfit(x,y6,1);
f=polyval(p,x);
plot(x,y6,'o',x,f,'r-')
legend('data','linear fit')
% Second Degree
subplot(1,3,2);
p=polyfit(x,y6,2)
f=polyval(p,x)
plot(x,y6,'o',x,f,'r-')
% Third Degree
subplot(1,3,3);
p=polyfit(x,y6,3)
f=polyval(p,x)
plot(x,y6,'o',x,f,'r')

回答(1 个)

Reshma Nerella
Reshma Nerella 2020-3-13
In the plot command,
plot(x,y6,'o',x,f,'r-')
you didn’t specify any line style, so you are getting only the data points
You may use the following to display the line
plot(x,y6,'-o',x,f,'r-') % specify line style along with marker.
Refer the following link for documentation of plot

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by