Effacer les filtres
Effacer les filtres

is this code of plotting correct?

2 vues (au cours des 30 derniers jours)
Aminata camara
Aminata camara le 19 Mar 2021
Commenté : KSSV le 19 Mar 2021
I wanted to create a code that load data and use it to create scatter plot,Perform a least-squares
fit of loaded data to a straight line,and also Perform a least-squares fit of this
data to a parabola. I also want to plot it on the same plot for comparasion. can you tell me if this is correct.
THIS IS THE CODE I WROTE
load samples
%create a scatter plot
scatter(t,y)
title('Scatter data plot')
xlabel('T')
ylabel('Y')%create a least-square fit of date to a straight line
hold on
plot(polyfit(t,y,1));
hold on
plot(polyfit(t,y,2));
hold off
%the fit is better with the parabola than it is with the line

Réponses (1)

KSSV
KSSV le 19 Mar 2021
load samples
% Fit a line to data
p1 = polyfit(t,y,1) ;
y1 = polyval(p1,t) ;
% Fit second degree polynomial to data
p2 = polyfit(t,y,2) ;
y2 = polyval(p2,t) ;
% plot
plot(t,y,'.r')
title('Scatter data plot')
xlabel('T')
ylabel('Y')
hold on
plot(t,y1);
plot(t,y2)
  2 commentaires
Aminata camara
Aminata camara le 19 Mar 2021
Thank you so much for your help
KSSV
KSSV le 19 Mar 2021
Thanks is accepting the answer :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by