Find an offset line from a linear portion of data set graph

Hi everyone!
I would like to plot an offset line (constant offset) from a linear region in my data set. Basically I want to get a line like in the graph below (from excel).
I can't really figure this out. My data set is an array with number of points for x and y and plotting it is not a problem.
Thank you for your suggestions,
Dominika

 Accepted Answer

Just shift the X by the amount you want to shift it:
shiftedX = x + 0.6;
plot(x, y, 'b-', 'LineWidth', 3);
hold on;
plot(shiftedX, y, 'r-', 'LineWidth', 3);

2 Comments

Thanks for an answer. But that doesn't solve my problem. I have more than 50 graphs so I need a code to find the linear region in each graph and shift it with a certain value.
Since you don't have very many points, the brute force way might be fine. Just fit lines from every point to every later point that is at least 2 points away. Then compute the residuals (which is the fitted minus the actual). Any line that fits reasonably well will have a low average residual and lines that are poor fits will have high average residuals. As you can imagine, there are going to be several lines that are reasonably straight so you'll have to pick some criteria for what line is "best", like it must be longer than 0.8 in the x direction and have an average residual less than 100, or whatever. Even then, there might be a few lines that meet your criteria.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!