author | francesco-ST <francesco.abbate@st.com> | 2010年11月02日 11:03:04 +0100 |
---|---|---|
committer | francesco-ST <francesco.abbate@st.com> | 2010年11月02日 11:03:04 +0100 |
commit | eb3acd162831b5af1fbeb59dfc6e5e9bf0f27e45 (patch) | |
tree | 6db0339464abf7ed4bb2488cdc7999a2e54a94b0 /examples/interp.lua | |
parent | 2ec481f397fa96e11871f077e859182b83e87704 (diff) | |
download | gsl-shell-eb3acd162831b5af1fbeb59dfc6e5e9bf0f27e45.tar.gz |
-rw-r--r-- | examples/interp.lua | 30 |
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 |