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

6 ビュー (過去 30 日間)
On In Liem
On In Liem 2019 年 11 月 21 日
コメント済み: On In Liem 2019 年 11 月 25 日
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 Center および File ExchangeFit Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by