Getting the equation of a line
31 views (last 30 days)
Show older comments
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)')
0 Comments
Answers (1)
John D'Errico
on 26 May 2021
Edited: John D'Errico
on 26 May 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
you would have learned exactly that.
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!