Using cubic splines to fit data

Hi, I have a quite involved code for fitting cubic splines to data. I have noticed something odd and wondered if anyone had experience with this. For horizontal data, the spline fits extremely well, however for data within a small x range and large y, i.e. a series of vertical points, the spline is very odd! I have attached two images, first the data set and next the resulting fits.. Any help would be appreciated.

Answers (1)

John D'Errico
John D'Errico on 18 Oct 2016
First of all, why are you writing cubic spline codes to fit data, if you don't really understand cubic splines, and the issues involved with them?
I'm not sure why I made this an answer, since you say nothing concrete about what EXACTLY you are doing. There are lots of ways you can fit a spline to data, and the data that you have is nothing I would ever decide that a spline would be a good choice for. Do you presume noise in the fit? Is this an interpolating spline? (Why would you do that, EVER? But people do.)
Are you fitting the horizontal lines as separate functions, thus y as function of x for each horizontal set of points? Are the vertical lines fit as x as a function of y? If not, then why not? If you are trying to fit a spline through essentially vertical data in the form of y(x), you should realize that is an insane thing to do. (Sorry but it is.) Or are you somehow trying to fit all of these points at once? (How can I guess what you're doing wrong, when you say nothing?)
The fact is, there are spline codes out there. You would do far better to learn to use an appropriate spline code before trying to write your own. As it is, I'm not at all sure why you are trying to use splines on that data at all.

4 Comments

Matlab User
Matlab User on 18 Oct 2016
Edited: Matlab User on 18 Oct 2016
I realise my question was vague, apologies. The points associated with each line has a unique ID, for this example there are 6 IDs : Three horizontal and three vertical. For each horizontal series of points, y(x). I know it seems strange to fit a vertical data series in the form y(x), I just wanted a universal way to fit each data series regardless of orientation. If you have any suggestions that would suit this form of data that would be great. Essentially, I am starting from a simple point pattern of horizontal and vertical lines of points, and trying to find a way to fit this data and later apply it to a much harder series of point patterns.
I found the above function really helpful, including the word document. For data that is not exactly vertical it works really well. I was just hoping for some insight I guess into this common problem I am having. I was wondering if it could be due to the small x range over which the vertical data series is evaluated ?
Oh well, at least you are using a good code. What can I say, since it is my own? :) :)
The bad fits for the vertical lines are a direct result of trying to fit them in the form y(x). After all, what can you expect then? If the line was perfectly vertical, with no noise at all in it, then you would have a result that is not even a single valued function of x. It is even worse when there is noise in x though, because then you get the crappy oscillations that you see. SLM cannot possibly know what you are doing, so it assumes that you really have some function y(x), over a TINY range in x, with huge variations in y. It tries to fit the curve as you gave it.
So you CANNOT fit a vertical line in the form y(x). But there is no reason why you cannot invert the data, fitting it as x(y) for the vertical lines. That would resolve many of your problems.
A problem with fitting each line independently is there is no connection at the ends. Is that a problem? Should this be treated as a problem of fitting a perturbed rectangular lattice in some way? So the result could be a set of perturbed rects, that fit together, but need not have 90 degree corners?
I can probably think of scheme that does this in two dimensions. I'm not sure why you are trying to fit a smooth spline to these edges though. Do you really need a smooth curve there? Or would it be sufficient to have some sort of piecewise linear segments?
You did post the data as a figure, so I can extract the data. I think I can figure out how to employ gridfit to solve this problem...
open Fibres.fig
H = get(gca,'children');
x = H.XData;
y = H.YData;
I guess the question is, before I go further, how much of the variability in this data do you need to follow? What is your eventual goal? For example, do you want to turn this into a set of 4 slightly curvy-edged rects? Or would it be sufficient to simply replace those noisy lines with straight edges, though not necessarily meeting at right angles? So those nominal squares might turn into general 4 sided polygons?
Thankyou for your reply - i now understand why it is unreasonable to fit the vertical data in this way. I think including a loop to calculate the x range over which the ID is known, then if it is extremely small, inverting that ID is a great idea.
No connections is okay, in the example I gave, there are unique IDs associated to the series of points forming the horizontal and vertical lines. So, I do not want to join these "IDs" together..
The main problem I guess is data variability. The example i gave is a simple case of a series of horizontal and vertical IDs. I have written a code to generate at random, the orientation of these 6 unique series of points. I've attached an example in case my text is unclear. In this case, the lines are coded to be linear. Although the idea is to fit this data without too much restriction on orientation or linearity.
I have also written code to generate randomly placed curved series of points, given a unique ID. So I was hoping that I could be able to fit these types of data without restriction on linearity/curvature and came across your function and thought it may be just what I was looking for!! Although, perhaps the variability in the data series could be too much and I need to tell the model more about each system ?

Sign in to comment.

Categories

Asked:

on 18 Oct 2016

Commented:

on 20 Oct 2016

Community Treasure Hunt

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

Start Hunting!