How to draw a line of best fit on filtered discrete data sets
6 次查看(过去 30 天)
显示 更早的评论
How can i draw a line of best fit on a subset of the full data set?
I want a line of best fit but only using the data in blue below
So ignore the green, yellow, orange, red, only use the blue part
Now that is easily filtered down to, so know which parts are the blue part is easy, but getting to somehow draw a line of best fit to only use the blue is the challeging part
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/641930/image.png)
I can filter the data set to only show the data i want to use for the line of best fit, as shown in graph below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/641935/image.png)
Now all i need it to find a way to draw a line of best fit on this data set, but when i try polyfit and other best fit functions, it gets all messed up
I'm assuming because of the gaps in the dataset.
So how can i draw a line of best fit for the chart above?
5 个评论
Scott MacKenzie
2021-6-4
You post data via the Attachments button when you are writing your question:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/642060/image.jpeg)
If you insert code in your question, it's always best to have it properly appear as code. To do this, select the code and click the code button to get the code propertly formatted in your question.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/642065/image.jpeg)
采纳的回答
Mathieu NOE
2021-6-4
I used this simple example and created a gap in the data (not NaN's)
there is no problem to use polyfit in that situation
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/642080/image.png)
% Example: simple linear regression with polyfit
% Fit a polynomial p of degree 1 to the (x,y) data:
x = 1:100;
x(x>25 & x <75) = [];
y = 0.3*x + 2*randn(1,length(x));
p = polyfit(x,y,1);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
legend('data','linear fit')
7 个评论
Mathieu NOE
2021-6-7
this is the plot associated with this demo code :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/644395/image.png)
% Example: simple log regression with polyfit
% Fit a polynomial p of degree 1 to the (x,y) data:
x = 1:100;
x(x>25 & x <75) = [];
y = exp(0.03*x + 0.5*randn(1,length(x)));
yl = log10(y);
%%%%%%%%%%%%%%%%%%%%
n = 1; % Polynomial Order
alfa = 0.99; % Desired Significance
xv = linspace(min(x), max(x))'; % Optional High-Resolution Vector
[ p, yhat, ci ] = polypredci( x, yl, n, alfa, xv ); % Define Both ‘alfa’ & ‘xv’
yhat = 10.^(yhat); % convert back from log to lin
figure(1)
semilogy(x, y, 'bp');
hold on
if length(yhat) == length(x)
semilogy(x, yhat, '--r')
semilogy(x, yhat+ci, '--g')
semilogy(x, yhat-ci, '--g')
elseif length(yhat) == length(xv)
semilogy(xv, yhat, '--r')
semilogy(xv, yhat+ci, '--g')
semilogy(xv, yhat-ci, '--g')
end
hold off
grid on
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fit Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!