How can I add line of best fit into my subplots. I have tried to use polyfit but it doesn't work.

조회 수: 6 (최근 30일)
subplot(2,2,1)
scatter(WS_TG,mWS1)
polyfit(WS_TG,mWS1)
title('113.5N, 22.25N')
xlabel('time')
ylabel('wind speed (ms^-1)')
subplot(2,2,2)
scatter(WS_TG,mWS2)
title('113.5N, 22.5N')
xlabel('time')
ylabel('wind speed (ms^-1)')
subplot(2,2,3)
scatter(WS_TG,mWS3)
title('113.75N, 22E')
xlabel('time')
ylabel('wind speed (ms^-1)')
subplot(2,2,4)
scatter(WS_TG,mWS4)
title('113.75N,22E')
xlabel('time')
ylabel('wind speed (ms^-1)')
And this is the error that I got if I just run the first part:
Not enough input arguments.
Error in polyfit (line 56)
V(:,n+1) = ones(length(x),1,class(x));
Of curse, I also used the toolbox to draw the regression line for me. But every time when I jump to another plot, the line from the previous plot will disappear.

답변 (1개)

the cyclist
the cyclist 2019년 11월 21일
As stated in the polyfit documentation, it requires three inputs. (The third input is the degree of the fitting polynomial.)
You've called it with only two.
  댓글 수: 5
the cyclist
the cyclist 2019년 11월 25일
편집: the cyclist 2019년 11월 25일
Yes, you need to call polyfit with the output arguments, and then use those output arguments to plot the line.
Here is a simple example:
% Simulate some data
N = 20;
x = rand(N,1);
y = 2 + 3*x + 0.5*randn(N,1);
% Fit the polynomial, getting the fit parameters as output
% p(1) is the linear term, and p(2) is the constant term
p = polyfit(x,y,1);
% Pick a couple arbitrary x points, and find the corresponding y points
% from the fit. We'll use these to plot the straight-line fit.
x_fit = [0 1];
y_fit = p(2) + p(1)*x_fit;
% Create the figure window
figure
% Use the "hold" command, for two plots on one axis
hold on
% Plot the data as points
scatter(x,y)
%Plot the fit as a line
plot(x_fit,y_fit)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by