What is the best way to get an equation of a curve?

25 views (last 30 days)
I have a set of x and y values and I need to plot them and get an equation of the curve. I have used the curve fitting tool and the smoothing spline to get a graph of the curve but there is no way to get a simple equation from that. I am looking for a way to find a curve fit for the values that an equation can also be found for.
  2 Comments
Varun Gunda
Varun Gunda on 13 Jan 2017
You can get the equations in the following way: Suppose the data is stored in x and y,
pp = spline(x,y);
where pp is a struct data type containing coefficients of the required piece wise polynomials, breaks etc., |
John D'Errico
John D'Errico on 15 Jan 2017
Since it looks like you have changed your question since I answered it...
Now, you are just looking for a general function that represents your data. The problem is, this is something that is not trivial to do for some totally arbitrary curve. There are infinitely many functions that will interpolate any set of points. And there are infinitely many possible functions that will approximate any set of points to within any supplied tolerance.
So if you have no idea what the underlying model is for some general curve, then the best you can do is to use a spline. Sometimes you might get lucky, and be able to fit a low order polynomial, if you absolutely need to have some function you can write down on paper.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 13 Jan 2017
CONGRATULATIONS! You are the one million'th person to ask this question. Your prize is a free trip to the Hackensack Meadowlands, where you will be free to search for Jimmy Hoffa, or whatever else you might want to do there. (Small print: winner must provide transportation, lodging, food, etc.)
Seriously, there is NO "equation".
There are MANY equations. A smoothing spline is a set of functions (generally cubic segments) that are pieced together (so they are sufficiently smooth across the breaks) into one large mess of a function. Easy to evaluate, since you use the provided tools. But no simple "function" that you can write down.
  3 Comments
prachi jain
prachi jain on 13 Jun 2019
Hello! Did you find a solution to your problem? I am doing the exact same thing i.e moving the spline along the axis.
Please help.
John D'Errico
John D'Errico on 13 Jun 2019
Edited: John D'Errico on 13 Jun 2019
I never saw the followup question. Anyway, it is a bad idea to ask an unrelated question in a comment. How to do it? Trivial.
x0 = 0:10;
y0 = rand(1,11);
spl = spline(x0,y0);
x = linspace(0,10,100);
plot(x0,y0,'o',x,fnval(spl,x),'-')
Now, shift the spline up. by 1 unit in y. We see that spl is a struct, containing an array of cubic polynomial coefficietns for each of the 10 segments.
spl
spl =
struct with fields:
form: 'pp'
breaks: [0 1 2 3 4 5 6 7 8 9 10]
coefs: [10×4 double]
pieces: 10
order: 4
dim: 1
The shift is trivally easy. Sorry, but it is. Well, perhaps slightly more difficult than just adding 1 to the output of fnval. So, yes, I could just do this:
fnval(spl,x) + 1
In fact, I'm not sure why Patrick or Prachi would not just do that. But lets translate the spline by 1 unit upwards in y, creating a new spline that we can then evaluate freely.
spl2 = spl;
spl2.coefs(:,end) = spl2.coefs(:,end) +1;
That is all it requires. I said trivial, no?
hold on
plot(x,fnval(spl2,x),'r--')
Note that fnval is from the curve fitting toolbox as I recall. Users of MATLAB that do not have the curvefitting toolbox will just use ppval instead of fnval.

Sign in to comment.

Categories

Find more on Get Started with Curve Fitting Toolbox 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!