Jonathan
Jonathan

Reputation: 914

How to call Monotonic Spline function

Let me preface this with, I'm not a math whiz by any means. I am interested in fitting a curve like the Monotonic Spline function to my data.

I am trying to get this Javascript version of a Monotonic Spline function to work and can't figure out how to call the function properly. Specifically, the beginning of the code starts like this (I've never seen a function named in this manner before):

var CubicSpline, MonotonicCubicSpline;
MonotonicCubicSpline = function() {
  function MonotonicCubicSpline(x, y) { 

To me, the same function is defined in two different scopes. The first doesn't take any parameters and the second one does. How do you get around this?

I started with just supplying the points I wanted to fit:

MonotonicCubicSpline([0,5,10,15,20,25], [0,6,2,6,7,10])

but that portion of the function doesn't return anything, it seems as if I need to call the function with the same name above that. If you want to test this out, here's a jsfiddle link:

http://jsfiddle.net/SszKr/7/

Any help would be greatly appreciated! Thanks!

Credit for the function goes to: http://blog.mackerron.com/2011/01/01/javascript-cubic-splines/

Upvotes: 1

Views: 1128

Answers (1)

Cattode
Cattode

Reputation: 856

In the MonotonicCubicSpline function declaration, we can see that there is an interpolate function added to the prototype and that the constructor function returns nothing.

So you have to declare a new MonotonicCubicSpline then use its interpolate function.

Example:

var mySpline = new MonotonicCubicSpline([0,5,10,15,20,25], [0,6,2,6,7,10]);
console.log(mySpline.interpolate(3));

I'm not sure but I suppose that the interpolate function argument is the abscissa.

Upvotes: 3

Related Questions