MATLAB Answers

Linear Regression not working

7 views (last 30 days)
Grace Rembold
Grace Rembold on 25 Nov 2019
Answered: Reshma Nerella on 13 Mar 2020
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')

  0 Comments

Sign in to comment.

Answers (1)

Reshma Nerella
Reshma Nerella on 13 Mar 2020
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

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!