×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Breaking a 2d cloud of data points into straight line segments

## Breaking a 2d cloud of data points into straight line segments

(OP)
I recently found a couple of ways of doing this. They are not automatic, and i suspect they need some judgement in their use.

The first is from Matlab https://www.mathworks.com/help/signal/ref/findchan...

and the second is the very neat SLM curve fitting toolbox https://www.mathworks.com/matlabcentral/fileexchan...

When i get matlab back then I'll be using these.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

### RE: Breaking a 2d cloud of data points into straight line segments

(OP)
Here's a rather splendid piece of code that does most of what I need. That is, for a given set of knots in x0 it generates contiguous best fit straight lines for the x and y vectors. The result, p, is the y at the defined x0. What it doesn't do is measure the error, or attempt to move the knots to reduce the overall error. That of course, is what i need.

#### CODE -->

function p = fitPiecewiseLinearFunction(x, y, x0)
%
% Fit a piecewise continuous function f(x) to the pairs of data points (x,y)
% such that the sum of squares of error is minimal.
%
% x0 - values of x that define ends of segments of function f(x)
%
% p - end points of the segments p = f(x0)
%
%
% 4-May-2004 Nikolai Golovchenko.
%

numberOfParameters = length(x0);

% separate data in segments
j = {};
for i = 1 : numberOfParameters - 1
j{i} = find(x > x0(i) & x <= x0(i + 1));

if isempty(j{i})
error('Insufficient amount of data points');
end

end

% compute the matrices corresponding to the
% system of equations
A = zeros(numberOfParameters, numberOfParameters);
B = zeros(numberOfParameters, 1);

for i = 1:numberOfParameters
if i ~= 1
% first sum
A(i, i-1) = A(i, i-1) - ...
sum((x(j{i-1}) - x0(i-1)) .* (x(j{i-1}) - x0(i))) / (x0(i) - x0(i-1)) .^ 2;
A(i, i) = A(i, i) + ...
sum((x(j{i-1}) - x0(i-1)) .^ 2) / (x0(i) - x0(i-1)) .^ 2;

B(i) = B(i) + ...
(sum(x(j{i-1}) .* y(j{i-1})) - x0(i-1) * sum(y(j{i-1}))) / (x0(i) - x0(i-1));
end
if i ~= numberOfParameters
% second sum
A(i, i) = A(i, i) + ...
sum((x(j{i}) - x0(i+1)) .^ 2) / (x0(i+1) - x0(i)) .^ 2;
A(i, i+1) = A(i, i+1) - ...
sum((x(j{i}) - x0(i)) .* (x(j{i}) - x0(i+1))) / (x0(i+1) - x0(i)) .^ 2;

B(i) = B(i) + ...
(-sum(x(j{i}) .* y(j{i})) + x0(i+1) * sum(y(j{i}))) / (x0(i+1) - x0(i));
end
end

% find the parameters
p = A^-1 * B; 

The black line is an eyeballed set of knots for the blue data. The circles are 11 year moving averages and the orange line is just a linear regression.

I looked at two built in functions. 'splinefit' tries to equalise the segment lengths. 'polyfit' doesn't guarantee that the lines are contiguous at the knots.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

### RE: Breaking a 2d cloud of data points into straight line segments

Try this. You can try several numbers of specified joints, and pick the 'Best' number based on rms error.

x=1:100;
y(1:25) = 0+rand(25,1);
y(26:50) = 1+rand(25,1);
y(51:75) = 2+rand(25,1);
y(76:100)= -1+rand(25,1);
figure
plot(x,y,'r.')
hold on

sp3=spap2(3,2,x,y) % 3 joints, 1st order( use a 2 for 1st order )
fnplt(sp3,'m')

### RE: Breaking a 2d cloud of data points into straight line segments

Looks like elements derived from the Sunspot Cycle.

### RE: Breaking a 2d cloud of data points into straight line segments

Settle on the number of knots the science seems to suggest, apply lsqcurvefit (Matlab) to the knot locations and turn the crank. I picked 11. Once you get the first solution, bootstrap it by perturbing the lsqcurvefit solution and send it back into the fire a few more times. I do this technique with Pacejka tire model fits and it works very well.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

Research Report - How Engineers are Using Remote Access
Remote access enables engineers to work from anywhere provided they have an internet connection. We surveyed our audience of engineers, designers and product managers to learn how they use remote access within their organizations. We wanted to know which industries have adopted remote access, which software they are using, and what features matter most. Download Now
eBook - Managing the Context of Product Complexity Using the Digital Twin
Keeping track of changes to complex products is difficultâ€”think Aerospace & Defense equipment, new generations of commercial aircraft, and software-based automobiles. A new way to managing the digital context of the physical product is required and the answer is the Digital Twin. This ebook explores the opportunity available for Operations and Maintenance for the Digital Twin. Download Now
White Paper - Trends in Industrial Filtration
Substantial progress has been made in filtration technologies in recent years. New filter media materials, designs and processes have led to filters that are more efficient, reliable, compact and longer lasting. This white paper will discuss the various trends that are impacting operational responsibilities of MROs today and the resources that are available for staying up-to-date on the latest filtration solutions. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!