Are these polyfit curves correct?

1 visualizzazione (ultimi 30 giorni)
This is what I have to do:
Create an m-file that uses the polyfit, linspace, polyval and plot functions to create a plot of the data listed below along with a least-squares line, a third-degree polynomial and a five-degree polynomial fit. Plot the curves with 100 data points (linspace default) to produce smooth fit curves.
x = [-8, -6, -4, -2, -1, 0, 1, 2, 4, 6, 8]
y = [0.1, 0.2, 0.5, 0.7, 2.2, 3.8, 5.5, 3.6, 2.8, 4.0, 3.5]
This is what I have so far.
x = linspace(-8,8);
y = linspace(0.1,3.5);
p = polyfit(x,y,3);
x2 = 0:0.1:3.1;
y2 = polyval(p,x2);
v = polyfit(x,y,5);
c = polyval(v,x2);
subplot(2,1,1)
plot(x,y,x2,y2)
legend('x,y,polyfit')
xlabel('x')
ylabel('y')
subplot(2,1,2)
plot(x,y,x2,c)
legend('x,y,polyfit')
xlabel('x')
ylabel('y')

Risposta accettata

madhan ravi
madhan ravi il 19 Lug 2018
Modificato: madhan ravi il 19 Lug 2018
Codes are correct
%one remark
%x2=-8:0.1:8
%because the start and endpoint should be similar to x.
plot(x,y,'ob'x2,y2,'')
%use line markers to distinguish between them :)

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by