fitting to a pure cubic and getting residuals

5 views (last 30 days)
Hi. I want to perform a cubic fit (i.e. jsut a term in x^3 with no x^2 or x^1 so cannot use polyfit:
I need to do the fit of my data x and N(:,1). I also need the residuals.
This is my x:
x=[-0.625,-0.5,-0.4,-0.3,-0.2,0,0.2,0.3,0.4,0.5,0.625]'
ft=fittype('a*x^3')
[fitobj,~,~,~]=fit(x,N(:,i),ft)
coeff=fitobj.a
y=N(:,1)
yfit=coeff*x^3
res=y-yfit

Accepted Answer

John D'Errico
John D'Errico on 6 May 2015
Simple is to download polyfitn. But even easier is to just do this:
coeff = (x.^3)\y;
resid = y - coeff*x.^3;
  3 Comments
Jason
Jason on 6 May 2015
Thanks both of you. It was the dot after the x I was missing. I've used polyfit before, but I don't want any other terms (i.e. x^2, x etc)
John D'Errico
John D'Errico on 6 May 2015
Yes, what a difference a dot makes. But I meant that you can use polyfitn (a download from the file exchange) to do this. It is hardly worth the effort for such a simple model though, when backslash is so easy to use here.

Sign in to comment.

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression 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!