How to create a best fit line for data with an aymptote?

42 views (last 30 days)
I am currently trying to find the point at which the data reaches its max, the asymptote. However, te data fluctuates, so I need to create a best fit line, since finding the derivative of the data as it is now would not work. How would I programatically go about creating a best fit line for this? I need to be able to acess the equation from the code. The data takes the form of the attached images.

Answers (2)

Image Analyst
Image Analyst on 19 Jan 2020
How about if you just fit a line to the last N points? Like N=20 or N = 10% of the total width or whatever. Or you can figure out how many final points to fit to the line by using diff() and seeing when the difference starts getting small.
N = 20; % Whatever.
coefficients = polyfit(x(end N : end), y(end - N : end), 1)
slope = coefficients(1)
offset = coefficients(2)
% Show the assymptote line over the original graph.
hold on;
xa = x(end N : end);
ya = slope * xa + offset;
plot(xa, ya, 'r-', 'LineWidth', 2);
grid on;

John D'Errico
John D'Errico on 20 Jan 2020
It partly depends on your goal. Do you just want to best estimate the constant asymptote? Or, are you looking for some function that will approximate the entire curve? The latter is of course far more difficult, since there is no reasonable model that will fit any possible such relationship, especially when it appears that your curve increases smoothly, then suddenly flattens out, with a virtual break in the slope at one point in one of the examples you show. Worse, at the bottom end, it looks like there is some garbage, SOME of the time.
If your goal is to just find the best possible constant asymptote estimate, then it is simple enough. Pick the set of points at the upper end where it appears to be essentially constant, then take the mean of y over that interval. The mean would be a good estimate of a constant function over an interval, the best estimate, in fact. You could be more sophisticated of course, in lots of ways, but that is simplest, and should be entirely adequate.
If your goal is really to get the entire curve fit smoothly, then I might recommend a smoothing spline, as most general. But a smoothing spline will not understand that the upper end of the curve approaches an asymptote. You could build that information into the spline model, but again, that will take more effort. And of course, splines have real problems with transitions from a high slope region into a constant slope region. You will always find ringing there near the transition, unless you use a spline model that includes a monotonicity constraint. Again - that will be more difficult to deal with.
Anyway, your question seems to be how to find a best estimate of the asymptote, which as I said, is not that difficult.
  4 Comments
Evan Shimoun
Evan Shimoun on 20 Jan 2020
The amount of data points varies depending on the set. The datasets are of the time taken for a motor to reach its maximum speed and what I am trying to figure out is the time taken to reach that speed. Generally the maximum speed, where the dataset flattens off, is at a y-value of around 740, though there is variation among the datasets, with some being closer to 730 or 725. The slope of the curve to reach the asymptote is different in each dataset, as it takes a different amount of time to reach the max. By threshold I meant a hardcoded value for what the max speed, the asymptote, would be. For example, right now, to get the function to work in the meantime, I have a threshold of around 736 set, so I take the first point which is greater than or equal to that number in each dataset and declare that to be the point where the max speed, the asymptote, is reached. My goal in figuring out the actual asymptote for each dataset is to create a more accurate analysis if possible.
Image Analyst
Image Analyst on 20 Jan 2020
OK, so I think my strategy should work. Didn't it?
You might be interested in my piecewise linear fit demo. It fits lines to the left part and the right part of a signal and determines where the "kink" is in the curve by identifying the location where the slopes between the two linear fit pieces is most different.
00_Screenshot.png

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!