Trouble using polyfit that separated straight fit from original data points

조회 수: 2 (최근 30일)
James 2013년 11월 4일
댓글: James 2013년 11월 6일
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));
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.

채택된 답변

dpb 2013년 11월 4일
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.

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 11월 4일
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);


Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기


Community Treasure Hunt

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

Start Hunting!

Translated by