MATLAB: I can’t figure out how to display the week that the snow will be gone.

homework

A homework problem we were assigned asks that we fit a quadratic curve to a set of data points that tells when the snow in a town will be gone. I've been able to generate the graph and fit the curve to the graph fine but I can't figure out how to display what is essentially the x-intercept of the graph.
y=[8 20 31 42 55 65 77 88 95 97 89 72 68 53 44];
x=[1:length(y)];
altx=[1:20];
coefs=polyfit(x,y,2);
curve=polyval(coefs,altx);
plot(x,y,'ro',altx,curve)
axis([0 20 0 100])
That's what I have so far. Any help would be much appreciated.

Best Answer

The x-intercepts are the roots of the quadratic equation estimated by polyfit:
zero_snow = roots(coefs)
zero_snow =
17.5211
1.0973
EDIT — Another option if you want to calculate it manually is the quadratic formula:
snow_gone(1) = (-coefs(2) + sqrt(coefs(2)^2 - 4*coefs(1)*coefs(3)))/(2*coefs(1))
snow_gone(2) = (-coefs(2) - sqrt(coefs(2)^2 - 4*coefs(1)*coefs(3)))/(2*coefs(1))
Related Question