help with fitlm (least squares fit)

I don't understand how to use fitlm if indeed that's the right function to use.
I want to fit 5 sets of data to the function y = ax^2 + bx + c.
tbl = table(Y,X); yes?
but
mdl = fitlm(tbl,'model') what do I use for model?
thanks.

5 Comments

Per the doc, the modelspec for a quadratic is 'purequadratic' or, with only the one variable, 'quadratic' will end up with the same model as there are no variables to do the interaction terms between.
You'll have to call fitlm once for each set of X,Y; the function isn't vectorized to compute multiple responses in one call. You can do this most effeciently coding-wise by having the data in an array and address the columns programmatically in a for...end loop rather than writing the code five-times over.
To fit with polynomial curve, polyfit function would be sufficient for most cases.
p = polyfit(x,y,2);
I would recommend not to "employ a steam-hammer to crack a nut" :-)
If one only wants/needs the coefficients, that is so; if one needs/wants the statistics to go with the estimates, the "hammer" is indicated...
Douglas's non-Answer moved here since it's not an answer to the original question:
It seems that polyfit is crashing mathlab.
If MATLAB itself crashed and shut down completely, then call the Mathworks.
If it's just your script that throws an error with red text in the command window, then most likely there is a problem with your m-file. Post your data with the paper clip icon, along with code to read it in and call the polyfit() function.
I'm attaching my polyfit demo - perhaps you can adapt it.

Sign in to comment.

 Accepted Answer

More Answers (2)

Douglas Brenner
Douglas Brenner on 19 Dec 2018
X = [1,2,3,4,5]
Y = [1,5,6,4,2]
yFit = polyfit(X,Y, 2)
Matlab itself crashed.
Douglas Brenner
Douglas Brenner on 19 Dec 2018
I could try a Gaussian fit. That would work for me. Do you have an example for that?
Thanks.

Categories

Community Treasure Hunt

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

Start Hunting!