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

5 views (last 30 days)
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.

Answers (1)

the cyclist
the cyclist on 21 Nov 2019
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 Comments
the cyclist
the cyclist on 25 Nov 2019
Edited: the cyclist on 25 Nov 2019
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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!