Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Created September 20, 2014 13:51
Show Gist options
  • Save H2CO3/3695a3fd4d2720680897 to your computer and use it in GitHub Desktop.
Save H2CO3/3695a3fd4d2720680897 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/spn
# Playing around with differential geometry is fun!
let derivative = function derivative(fn) {
return function(x0) {
let f = fn;
let dx = 1.0e-5;
let x = x0 + dx;
let y0 = f(x0), y = f(x);
return (y - y0) / dx;
};
};
let curvature = function curvature(f, x1) {
let x2 = x1 + 1.0e-5;
let y1 = f(x1), y2 = f(x2);
let fprime = derivative(f);
let dy1 = fprime(x1), dy2 = fprime(x2);
let xc = ((y1 - y2) * dy1 * dy2 + x1 * dy2 - x2 * dy1) / (dy2 - dy1);
let yc = y1 - (xc - x1) / dy1;
let c = 1.0 / hypot(xc - x1, yc - y1);
return c;
};
let circle = function circle(x) {
return sqrt(1.0 - x * x);
};
let functions = {
{ "sin", sin, M_PI_2 },
{ "cosh", cosh, 0.0 },
{ "circle", circle, 0.35 },
{ "exp", exp, 1.0 }
};
foreach(functions, function(k, v) {
printf("curvature of %s(%.5f)\t= %.5f\n", v[0], v[2], curvature(v[1], v[2]));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment