Trouble using polyfit that separated straight fit from original data points

2 visualizzazioni (ultimi 30 giorni)
Hi all,
I am plotting some data from a power-inverse law and using the following:
loglog(E2dist,E2PrdB,'+g'), hold on
p = polyfit(log(E2dist),log(E2PrdB),1);
m = p(1);
b = exp(p(2));
%mean=mu(1);
%std=mu(2);
loglog(E2dist, b.*E2dist.^m,'-m*');
This works fine. But when I change to
[p,S,mu] = polyfit(log(E2dist),log(E2PrdB),1);
my straight line fit is separated vertically from my original data points?
Hope you can help.
cheers

Risposta accettata

dpb
dpb il 4 Nov 2013
From
doc polyfit
[P,S,MU] = polyfit(X,Y,N) finds the coefficients of a polynomial in
XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X).
It's a "feature" that's non-intuitive, indeed. Would seem to have been better to use a flag to ask for/control centering rather than just the form of the output.

Più risposte (1)

Image Analyst
Image Analyst il 4 Nov 2013
When you use polyval() to get your estimated x values, you need to pass in S and mu to get values in your original range. See this demo:
x = -10:10;
y = x + 10 + 20*rand(1, length(x));
plot(x,y,'bd-', 'LineWidth', 3, 'MarkerSize', 15);
ylim([0 40]);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
grid on;
% Fit a line to the noisy data
[coefficients, S, mu] = polyfit(x, y, 1);
% Get the fit. YOU NEED TO PASS IN S AND MU!
yFitted = polyval(coefficients, x, S, mu);
% Plot it
hold on;
plot(x,yFitted,'r*-', 'LineWidth', 3, 'MarkerSize', 15);

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by