How to draw a line of best fit on filtered discrete data sets

2 views (last 30 days)
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
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
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?
Scott MacKenzie
Scott MacKenzie on 4 Jun 2021
You post data via the Attachments button when you are writing your question:
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.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Jun 2021
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
% 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);
legend('data','linear fit')
Mathieu NOE
Mathieu NOE on 7 Jun 2021
Thank you @Star Strider for your help
@Rizwan Khan try ths , hopefully I didn't make any wrong usage of polypredci
this is the plot associated with this demo code :
% 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
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')
hold off
grid on

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by