Nonlinear regression in case of many fits

1 view (last 30 days)
I have tried to fit certain data points using equation of the form y=ax^-1/3. My first question is , Is it possible to linearise the equation as I do in the code below? The other problem is that how can I get the coefficients in case of many regression fits as indicated in the for loop. Here is the code I tried. The loop doesn't give me as many coefficients as equal to w. Only one coefficient is obtained.
for w= 1:3684
z(:,w) = R(1:12,w).^-1/3;
y(:,w)=S(1:12,w);
p(w,:) = polyfit(z,y,1);
%yy = p(1) * z + p(2);
f = polyval(p,z(:,w));
plot(R(1:12,w),y(:,w),'o',R(1:12,w),f(:,w),'-')
end

Accepted Answer

Jos (10584)
Jos (10584) on 18 Jul 2013
First, note this
x = [1 6 9]
x.^-1/3
x.^(-1/3)
(x.^-1)/3
Then, you do not need to store z and w. Here is a more efficient solution:
idx = [1 10 100 3684] ;
N = numel(idx) ;
p = zeros(N,2) ; % pre-allocation
for k = 1:N
w = idx(k) ;
x = R(1:12,w) ;
xz = x.^-1/3;
y = S(1:12,w);
p(k,:) = polyfit(xz,y,1);
f = polyval(p,xz) ;
plot(x, y, 'o', x, f, '-')
end
If you want to store the fitted values as well, pre-allocate f
f = zeros(12,N) ;
...
f(:,k) = polyval(p,xz) ;
  3 Comments
Ede gerlderlands
Ede gerlderlands on 18 Jul 2013
Thank you. This take my burden away ..
Jos (10584)
Jos (10584) on 18 Jul 2013
I meant
f = polyval(p(k,:), xz)
but you probably already figured that out yourself.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!