Simple algorithm to fit a one-dimensional 4 point cubic curve to a given dataset

289 Views Asked by At

I want to fit a basic cubic curve to a given data set of multiple points.

It's a fairly simple fitting and I'm looking for an "easy" or "reduced" algorithm to help me do that.

I'm dealing with an "easing curve" here - it's always starting with 0,0 and ending with 1,1.

Some curve like this one.

--> Here you can also see the special kind of cubic bezier curve I'm talking about

In between it may reach values over 1 or below 0 but it will always start and end like this.

Now I want to fit such a dataset with a 4 point cubic bezier curve. P0 and P3 being (0|0) and (1|1).

All I need to know is P1 and P2, so that the resulting cubic curve is as close to the given data set as possible.

p.E. my dataset looks like this:

PX | 0.00 | 0.08 | 0.17 | 0.25 | 0.33 | 0.42 | 0.50 | 0.58 ... 0.92 | 1.00 |

PY | 0.00 | 0.01 | 0.03 | 0.07 | 0.15 | 0.29 | 0.48 | 0.69 ... 0.99 | 1.00 |

And my desired output something like

P0 ( 0 | 0 )

P1 ( 0.625 | 0 )

P2 ( 0.39 | 1 )

P3 ( 1 | 1 ).

Can you help me out? Thanks a lot in advance!!

1

There are 1 best solutions below

6
On

The basic idea is to define a function $f(PX,P1,P2)$ which is the calculated value of $PY$ based on the input. Then add up the squares of the errors $(PY-f(PX,P1,P2))^2$ over all the points. Use a function minimizer to find the values of P1 and P2. If $f$ is linearly dependent on P1 and P2 you can use a linear least squares method, otherwise you need a multidimensional minimizer. Any numerical analysis text will have a discussion. You can also use Excel and its goal seek to do the minimization.