Getting the equation of a line

273 vues (au cours des 30 derniers jours)
Ben Komita
Ben Komita le 26 Mai 2021
Commenté : darova le 28 Mai 2021
So I used polyfit to create a trendline for a data set and that worked great. Now I am trying to get the equation of that trendline but I am a little stuck so any direction on that would be great. The code I used can be found here:
subplot (2,1,2)
time = (0:.00274:21.3644); %time in years to be able to plot the NAVD88 data
% Fit a polynomial p of degree 1 to the NAVD88 data. This will give a
% trendline and allow to solve a projection
x = (0:.00491814:21.3644);
y = Hclean;
p = polyfit(x,y,1);
% Evaluate the fitted polynomial p and plot:
projection = polyval(p,x);
plot(time,H,x,projection,'-')%Plot of all the waves over 21 years
title ('NAVD88 Low-High Daily Water Height for 21 year data set')
xlabel ('Years since Jan. 18 2000')
ylabel ('NAVD88 (ft)')

Réponses (1)

John D'Errico
John D'Errico le 26 Mai 2021
Modifié(e) : John D'Errico le 26 Mai 2021
You did the fit, with the estimates of the coefficients in p. p is a vector, of length 2.
The line equation is simple.
y = p(1)*x + p(2)
Had you just read the help for polyfit,
help polyfit
POLYFIT Fit polynomial to data. P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of degree N that fits the data Y best in a least-squares sense. P is a row vector of length N+1 containing the polynomial coefficients in descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1). [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a structure S for use with POLYVAL to obtain error estimates for predictions. S contains these fields: R - Triangular R factor (possibly permuted) from a QR decomposition of the Vandermonde matrix of X df - Degrees of freedom normr - Norm of the residuals If the data Y are random, an estimate of the covariance matrix of P is (Rinv*Rinv')*normr^2/df, where Rinv is the inverse of R. [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm. Warning messages result if N is >= length(X), if X has repeated, or nearly repeated, points, or if X might need centering and scaling. Example: simple linear regression with polyfit % Fit a polynomial p of degree 1 to the (x,y) data: x = 1:50; y = -0.3*x + 2*randn(1,50); p = polyfit(x,y,1); % Evaluate the fitted polynomial p and plot: f = polyval(p,x); plot(x,y,'o',x,f,'-') legend('data','linear fit') Class support for inputs X,Y: float: double, single See also POLY, POLYVAL, ROOTS, LSCOV. Documentation for polyfit doc polyfit Other functions named polyfit codistributed/polyfit gpuArray/polyfit tall/polyfit
you would have learned exactly that.
  2 commentaires
Ben Komita
Ben Komita le 26 Mai 2021
great thank you so much. I was trying to do that but rather than p(1) and p(2) I was doing projection(1) and projection(2) which is where I ran into problems. Thanks!
darova
darova le 28 Mai 2021

Connectez-vous pour commenter.

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by