-rw-r--r-- | examples/interp.lua | 30 | ||||
-rw-r--r-- | interp.c | 37 |
diff --git a/examples/interp.lua b/examples/interp.lua index bfc976b2..f733ef63 100644 --- a/examples/interp.lua +++ b/examples/interp.lua @@ -1,13 +1,26 @@ function demo1() + + local w = window('v..') local N = 8 local xsmp = |k| 2*pi*(k-1)/N - local x, y = new(N, 1, xsmp), new(N, 1, |k| sin(xsmp(k))) + local x, y = new(N+1, 1, xsmp), new(N+1, 1, |k| sin(xsmp(k))) + + local function interp_plot(tp) + local p = plot(string.format('%s interpolation', tp)) + p:add(xyline(x, y), 'black', {{'marker', size=5}}) + local ap = interp(x, y, tp) + p:addline(fxline(|x| ap:eval(x), 0, 2*pi), 'blue', {{'dash', 7, 3, 3, 3}}) + p:addline(fxline(|x| ap:deriv(x), 0, 2*pi)) + p:add(fxline(cos, 0, 2*pi), 'blue', {{'stroke', width=0.75}, {'dash', 7,3}}) + print(string.format('%s interp / integral between (%g, %g) = %g', tp, + 0, 2*pi, ap:integ(0, 2*pi))) + return p + end + + local p = plot 'Akima Interpolation' - p:show() - p:addline(xyline(x, y)) - local ap = interp(x, y, 'akima') - p:addline(fxline(|x| ap:eval(x), 0, 2*pi), 'blue', {{'dash', 7, 3, 3, 3}}) - return p, ap + w:attach(interp_plot('akima'), '1') + w:attach(interp_plot('cspline'), '2') end function demo2() @@ -27,7 +40,10 @@ function demo2() p:addline(xyline(x, y)) p:add(xyline(x, y), 'black', {{'marker', size= 5}}) local ap = interp(x, y, 'cspline') - p:addline(fxline(|x| ap:eval(x), 0, x[N]), 'blue', {{'dash', 7, 3, 3, 3}}) + local a, b = 0, x[N] + p:addline(fxline(|x| ap:eval(x), a, b), 'blue', {{'dash', 7, 3, 3, 3}}) + p:addline(fxline(|x| ap:deriv(x), a, b), 'green') + print(string.format('Integral between (%g, %g) = %g', a, b, ap:integ(a, b))) return p, ap end @@ -23,10 +23,16 @@ enum fenv_pos { static int interp_new (lua_State *L); static int interp_free (lua_State *L); static int interp_eval (lua_State *L); +static int interp_deriv (lua_State *L); +static int interp_deriv2 (lua_State *L); +static int interp_integ (lua_State *L); static const struct luaL_Reg interp_methods[] = { {"__gc", interp_free}, {"eval", interp_eval}, + {"deriv", interp_deriv}, + {"deriv2", interp_deriv2}, + {"integ", interp_integ}, {NULL, NULL} }; @@ -127,6 +133,37 @@ interp_eval (lua_State *L) return 1; }; +int +interp_deriv (lua_State *L) +{ + struct interp *obj = gs_check_userdata (L, 1, GS_INTERP); + double x = gs_check_number (L, 2, true); + double d = gsl_interp_eval_deriv (obj->interp, obj->xsrc, obj->ysrc, x, obj->acc); + lua_pushnumber (L, d); + return 1; +}; + +int +interp_deriv2 (lua_State *L) +{ + struct interp *obj = gs_check_userdata (L, 1, GS_INTERP); + double x = gs_check_number (L, 2, true); + double d = gsl_interp_eval_deriv2 (obj->interp, obj->xsrc, obj->ysrc, x, obj->acc); + lua_pushnumber (L, d); + return 1; +}; + +int +interp_integ (lua_State *L) +{ + struct interp *obj = gs_check_userdata (L, 1, GS_INTERP); + double a = gs_check_number (L, 2, true); + double b = gs_check_number (L, 3, true); + double v = gsl_interp_eval_integ (obj->interp, obj->xsrc, obj->ysrc, a, b, obj->acc); + lua_pushnumber (L, v); + return 1; +}; + void interp_register (lua_State *L) { |