Skip to content

Instantly share code, notes, and snippets.

@v0dro
Created June 10, 2016 19:10
Show Gist options
  • Save v0dro/1a7c3cd3006955de08f02589982fb40c to your computer and use it in GitHub Desktop.
Save v0dro/1a7c3cd3006955de08f02589982fb40c to your computer and use it in GitHub Desktop.
Octave program for 2D interpolation.
%% saddle: saddle
function [z] = saddle(x, y)
z = x*x - y*y;
endfunction
x = [-20.0,-19.0,-18.0,-17.0,-16.0,-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0];
y = [-20.0,-19.0,-18.0,-17.0,-16.0,-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0,20.0];
temp = [];
z = [];
for xi = x
for yi = y
temp(end + 1) = saddle(xi, yi);
endfor
z = [z; temp];
temp = [];
endfor
% z values generated by Ruby GSL
% z = [0.0, 39.0, 76.0, 111.0, 144.0, 175.0, 204.0, 231.0, 256.0, 279.0, 300.0, 319.0, 336.0, 351.0, 364.0, 375.0, 384.0, 391.0, 396.0, 399.0, 400.0, 399.0, 396.0, 391.0, 384.0, 375.0, 364.0, 351.0, 336.0, 319.0, 300.0, 279.0, 256.0, 231.0, 204.0, 175.0, 144.0, 111.0, 76.0, 39.0, 0.0, -39.0, 0.0, 37.0, 72.0, 105.0, 136.0, 165.0, 192.0, 217.0, 240.0, 261.0, 280.0, 297.0, 312.0, 325.0, 336.0, 345.0, 352.0, 357.0, 360.0, 361.0, 360.0, 357.0, 352.0, 345.0, 336.0, 325.0, 312.0, 297.0, 280.0, 261.0, 240.0, 217.0, 192.0, 165.0, 136.0, 105.0, 72.0, 37.0, 0.0, -39.0, -76.0, -37.0, 0.0, 35.0, 68.0, 99.0, 128.0, 155.0, 180.0, 203.0, 224.0, 243.0, 260.0, 275.0, 288.0, 299.0, 308.0, 315.0, 320.0, 323.0, 324.0, 323.0, 320.0, 315.0, 308.0, 299.0, 288.0, 275.0, 260.0, 243.0, 224.0, 203.0, 180.0, 155.0, 128.0, 99.0, 68.0, 35.0, 0.0, -37.0, -76.0, -111.0, -72.0, -35.0, 0.0, 33.0, 64.0, 93.0, 120.0, 145.0, 168.0, 189.0, 208.0, 225.0, 240.0, 253.0, 264.0, 273.0, 280.0, 285.0, 288.0, 289.0, 288.0, 285.0, 280.0, 273.0, 264.0, 253.0, 240.0, 225.0, 208.0, 189.0, 168.0, 145.0, 120.0, 93.0, 64.0, 33.0, 0.0, -35.0, -72.0, -111.0, -144.0, -105.0, -68.0, -33.0, 0.0, 31.0, 60.0, 87.0, 112.0, 135.0, 156.0, 175.0, 192.0, 207.0, 220.0, 231.0, 240.0, 247.0, 252.0, 255.0, 256.0, 255.0, 252.0, 247.0, 240.0, 231.0, 220.0, 207.0, 192.0, 175.0, 156.0, 135.0, 112.0, 87.0, 60.0, 31.0, 0.0, -33.0, -68.0, -105.0, -144.0, -175.0, -136.0, -99.0, -64.0, -31.0, 0.0, 29.0, 56.0, 81.0, 104.0, 125.0, 144.0, 161.0, 176.0, 189.0, 200.0, 209.0, 216.0, 221.0, 224.0, 225.0, 224.0, 221.0, 216.0, 209.0, 200.0, 189.0, 176.0, 161.0, 144.0, 125.0, 104.0, 81.0, 56.0, 29.0, 0.0, -31.0, -64.0, -99.0, -136.0, -175.0, -204.0, -165.0, -128.0, -93.0, -60.0, -29.0, 0.0, 27.0, 52.0, 75.0, 96.0, 115.0, 132.0, 147.0, 160.0, 171.0, 180.0, 187.0, 192.0, 195.0, 196.0, 195.0, 192.0, 187.0, 180.0, 171.0, 160.0, 147.0, 132.0, 115.0, 96.0, 75.0, 52.0, 27.0, 0.0, -29.0, -60.0, -93.0, -128.0, -165.0, -204.0, -231.0, -192.0, -155.0, -120.0, -87.0, -56.0, -27.0, 0.0, 25.0, 48.0, 69.0, 88.0, 105.0, 120.0, 133.0, 144.0, 153.0, 160.0, 165.0, 168.0, 169.0, 168.0, 165.0, 160.0, 153.0, 144.0, 133.0, 120.0, 105.0, 88.0, 69.0, 48.0, 25.0, 0.0, -27.0, -56.0, -87.0, -120.0, -155.0, -192.0, -231.0, -256.0, -217.0, -180.0, -145.0, -112.0, -81.0, -52.0, -25.0, 0.0, 23.0, 44.0, 63.0, 80.0, 95.0, 108.0, 119.0, 128.0, 135.0, 140.0, 143.0, 144.0, 143.0, 140.0, 135.0, 128.0, 119.0, 108.0, 95.0, 80.0, 63.0, 44.0, 23.0, 0.0, -25.0, -52.0, -81.0, -112.0, -145.0, -180.0, -217.0, -256.0, -279.0, -240.0, -203.0, -168.0, -135.0, -104.0, -75.0, -48.0, -23.0, 0.0, 21.0, 40.0, 57.0, 72.0, 85.0, 96.0, 105.0, 112.0, 117.0, 120.0, 121.0, 120.0, 117.0, 112.0, 105.0, 96.0, 85.0, 72.0, 57.0, 40.0, 21.0, 0.0, -23.0, -48.0, -75.0, -104.0, -135.0, -168.0, -203.0, -240.0, -279.0, -300.0, -261.0, -224.0, -189.0, -156.0, -125.0, -96.0, -69.0, -44.0, -21.0, 0.0, 19.0, 36.0, 51.0, 64.0, 75.0, 84.0, 91.0, 96.0, 99.0, 100.0, 99.0, 96.0, 91.0, 84.0, 75.0, 64.0, 51.0, 36.0, 19.0, 0.0, -21.0, -44.0, -69.0, -96.0, -125.0, -156.0, -189.0, -224.0, -261.0, -300.0, -319.0, -280.0, -243.0, -208.0, -175.0, -144.0, -115.0, -88.0, -63.0, -40.0, -19.0, 0.0, 17.0, 32.0, 45.0, 56.0, 65.0, 72.0, 77.0, 80.0, 81.0, 80.0, 77.0, 72.0, 65.0, 56.0, 45.0, 32.0, 17.0, 0.0, -19.0, -40.0, -63.0, -88.0, -115.0, -144.0, -175.0, -208.0, -243.0, -280.0, -319.0, -336.0, -297.0, -260.0, -225.0, -192.0, -161.0, -132.0, -105.0, -80.0, -57.0, -36.0, -17.0, 0.0, 15.0, 28.0, 39.0, 48.0, 55.0, 60.0, 63.0, 64.0, 63.0, 60.0, 55.0, 48.0, 39.0, 28.0, 15.0, 0.0, -17.0, -36.0, -57.0, -80.0, -105.0, -132.0, -161.0, -192.0, -225.0, -260.0, -297.0, -336.0, -351.0, -312.0, -275.0, -240.0, -207.0, -176.0, -147.0, -120.0, -95.0, -72.0, -51.0, -32.0, -15.0, 0.0, 13.0, 24.0, 33.0, 40.0, 45.0, 48.0, 49.0, 48.0, 45.0, 40.0, 33.0, 24.0, 13.0, 0.0, -15.0, -32.0, -51.0, -72.0, -95.0, -120.0, -147.0, -176.0, -207.0, -240.0, -275.0, -312.0, -351.0, -364.0, -325.0, -288.0, -253.0, -220.0, -189.0, -160.0, -133.0, -108.0, -85.0, -64.0, -45.0, -28.0, -13.0, 0.0, 11.0, 20.0, 27.0, 32.0, 35.0, 36.0, 35.0, 32.0, 27.0, 20.0, 11.0, 0.0, -13.0, -28.0, -45.0, -64.0, -85.0, -108.0, -133.0, -160.0, -189.0, -220.0, -253.0, -288.0, -325.0, -364.0, -375.0, -336.0, -299.0, -264.0, -231.0, -200.0, -171.0, -144.0, -119.0, -96.0, -75.0, -56.0, -39.0, -24.0, -11.0, 0.0, 9.0, 16.0, 21.0, 24.0, 25.0, 24.0, 21.0, 16.0, 9.0, 0.0, -11.0, -24.0, -39.0, -56.0, -75.0, -96.0, -119.0, -144.0, -171.0, -200.0, -231.0, -264.0, -299.0, -336.0, -375.0, -384.0, -345.0, -308.0, -273.0, -240.0, -209.0, -180.0, -153.0, -128.0, -105.0, -84.0, -65.0, -48.0, -33.0, -20.0, -9.0, 0.0, 7.0, 12.0, 15.0, 16.0, 15.0, 12.0, 7.0, 0.0, -9.0, -20.0, -33.0, -48.0, -65.0, -84.0, -105.0, -128.0, -153.0, -180.0, -209.0, -240.0, -273.0, -308.0, -345.0, -384.0, -391.0, -352.0, -315.0, -280.0, -247.0, -216.0, -187.0, -160.0, -135.0, -112.0, -91.0, -72.0, -55.0, -40.0, -27.0, -16.0, -7.0, 0.0, 5.0, 8.0, 9.0, 8.0, 5.0, 0.0, -7.0, -16.0, -27.0, -40.0, -55.0, -72.0, -91.0, -112.0, -135.0, -160.0, -187.0, -216.0, -247.0, -280.0, -315.0, -352.0, -391.0, -396.0, -357.0, -320.0, -285.0, -252.0, -221.0, -192.0, -165.0, -140.0, -117.0, -96.0, -77.0, -60.0, -45.0, -32.0, -21.0, -12.0, -5.0, 0.0, 3.0, 4.0, 3.0, 0.0, -5.0, -12.0, -21.0, -32.0, -45.0, -60.0, -77.0, -96.0, -117.0, -140.0, -165.0, -192.0, -221.0, -252.0, -285.0, -320.0, -357.0, -396.0, -399.0, -360.0, -323.0, -288.0, -255.0, -224.0, -195.0, -168.0, -143.0, -120.0, -99.0, -80.0, -63.0, -48.0, -35.0, -24.0, -15.0, -8.0, -3.0, 0.0, 1.0, 0.0, -3.0, -8.0, -15.0, -24.0, -35.0, -48.0, -63.0, -80.0, -99.0, -120.0, -143.0, -168.0, -195.0, -224.0, -255.0, -288.0, -323.0, -360.0, -399.0, -400.0, -361.0, -324.0, -289.0, -256.0, -225.0, -196.0, -169.0, -144.0, -121.0, -100.0, -81.0, -64.0, -49.0, -36.0, -25.0, -16.0, -9.0, -4.0, -1.0, 0.0, -1.0, -4.0, -9.0, -16.0, -25.0, -36.0, -49.0, -64.0, -81.0, -100.0, -121.0, -144.0, -169.0, -196.0, -225.0, -256.0, -289.0, -324.0, -361.0, -400.0, -399.0, -360.0, -323.0, -288.0, -255.0, -224.0, -195.0, -168.0, -143.0, -120.0, -99.0, -80.0, -63.0, -48.0, -35.0, -24.0, -15.0, -8.0, -3.0, 0.0, 1.0, 0.0, -3.0, -8.0, -15.0, -24.0, -35.0, -48.0, -63.0, -80.0, -99.0, -120.0, -143.0, -168.0, -195.0, -224.0, -255.0, -288.0, -323.0, -360.0, -399.0, -396.0, -357.0, -320.0, -285.0, -252.0, -221.0, -192.0, -165.0, -140.0, -117.0, -96.0, -77.0, -60.0, -45.0, -32.0, -21.0, -12.0, -5.0, 0.0, 3.0, 4.0, 3.0, 0.0, -5.0, -12.0, -21.0, -32.0, -45.0, -60.0, -77.0, -96.0, -117.0, -140.0, -165.0, -192.0, -221.0, -252.0, -285.0, -320.0, -357.0, -396.0, -391.0, -352.0, -315.0, -280.0, -247.0, -216.0, -187.0, -160.0, -135.0, -112.0, -91.0, -72.0, -55.0, -40.0, -27.0, -16.0, -7.0, 0.0, 5.0, 8.0, 9.0, 8.0, 5.0, 0.0, -7.0, -16.0, -27.0, -40.0, -55.0, -72.0, -91.0, -112.0, -135.0, -160.0, -187.0, -216.0, -247.0, -280.0, -315.0, -352.0, -391.0, -384.0, -345.0, -308.0, -273.0, -240.0, -209.0, -180.0, -153.0, -128.0, -105.0, -84.0, -65.0, -48.0, -33.0, -20.0, -9.0, 0.0, 7.0, 12.0, 15.0, 16.0, 15.0, 12.0, 7.0, 0.0, -9.0, -20.0, -33.0, -48.0, -65.0, -84.0, -105.0, -128.0, -153.0, -180.0, -209.0, -240.0, -273.0, -308.0, -345.0, -384.0, -375.0, -336.0, -299.0, -264.0, -231.0, -200.0, -171.0, -144.0, -119.0, -96.0, -75.0, -56.0, -39.0, -24.0, -11.0, 0.0, 9.0, 16.0, 21.0, 24.0, 25.0, 24.0, 21.0, 16.0, 9.0, 0.0, -11.0, -24.0, -39.0, -56.0, -75.0, -96.0, -119.0, -144.0, -171.0, -200.0, -231.0, -264.0, -299.0, -336.0, -375.0, -364.0, -325.0, -288.0, -253.0, -220.0, -189.0, -160.0, -133.0, -108.0, -85.0, -64.0, -45.0, -28.0, -13.0, 0.0, 11.0, 20.0, 27.0, 32.0, 35.0, 36.0, 35.0, 32.0, 27.0, 20.0, 11.0, 0.0, -13.0, -28.0, -45.0, -64.0, -85.0, -108.0, -133.0, -160.0, -189.0, -220.0, -253.0, -288.0, -325.0, -364.0, -351.0, -312.0, -275.0, -240.0, -207.0, -176.0, -147.0, -120.0, -95.0, -72.0, -51.0, -32.0, -15.0, 0.0, 13.0, 24.0, 33.0, 40.0, 45.0, 48.0, 49.0, 48.0, 45.0, 40.0, 33.0, 24.0, 13.0, 0.0, -15.0, -32.0, -51.0, -72.0, -95.0, -120.0, -147.0, -176.0, -207.0, -240.0, -275.0, -312.0, -351.0, -336.0, -297.0, -260.0, -225.0, -192.0, -161.0, -132.0, -105.0, -80.0, -57.0, -36.0, -17.0, 0.0, 15.0, 28.0, 39.0, 48.0, 55.0, 60.0, 63.0, 64.0, 63.0, 60.0, 55.0, 48.0, 39.0, 28.0, 15.0, 0.0, -17.0, -36.0, -57.0, -80.0, -105.0, -132.0, -161.0, -192.0, -225.0, -260.0, -297.0, -336.0, -319.0, -280.0, -243.0, -208.0, -175.0, -144.0, -115.0, -88.0, -63.0, -40.0, -19.0, 0.0, 17.0, 32.0, 45.0, 56.0, 65.0, 72.0, 77.0, 80.0, 81.0, 80.0, 77.0, 72.0, 65.0, 56.0, 45.0, 32.0, 17.0, 0.0, -19.0, -40.0, -63.0, -88.0, -115.0, -144.0, -175.0, -208.0, -243.0, -280.0, -319.0, -300.0, -261.0, -224.0, -189.0, -156.0, -125.0, -96.0, -69.0, -44.0, -21.0, 0.0, 19.0, 36.0, 51.0, 64.0, 75.0, 84.0, 91.0, 96.0, 99.0, 100.0, 99.0, 96.0, 91.0, 84.0, 75.0, 64.0, 51.0, 36.0, 19.0, 0.0, -21.0, -44.0, -69.0, -96.0, -125.0, -156.0, -189.0, -224.0, -261.0, -300.0, -279.0, -240.0, -203.0, -168.0, -135.0, -104.0, -75.0, -48.0, -23.0, 0.0, 21.0, 40.0, 57.0, 72.0, 85.0, 96.0, 105.0, 112.0, 117.0, 120.0, 121.0, 120.0, 117.0, 112.0, 105.0, 96.0, 85.0, 72.0, 57.0, 40.0, 21.0, 0.0, -23.0, -48.0, -75.0, -104.0, -135.0, -168.0, -203.0, -240.0, -279.0, -256.0, -217.0, -180.0, -145.0, -112.0, -81.0, -52.0, -25.0, 0.0, 23.0, 44.0, 63.0, 80.0, 95.0, 108.0, 119.0, 128.0, 135.0, 140.0, 143.0, 144.0, 143.0, 140.0, 135.0, 128.0, 119.0, 108.0, 95.0, 80.0, 63.0, 44.0, 23.0, 0.0, -25.0, -52.0, -81.0, -112.0, -145.0, -180.0, -217.0, -256.0, -231.0, -192.0, -155.0, -120.0, -87.0, -56.0, -27.0, 0.0, 25.0, 48.0, 69.0, 88.0, 105.0, 120.0, 133.0, 144.0, 153.0, 160.0, 165.0, 168.0, 169.0, 168.0, 165.0, 160.0, 153.0, 144.0, 133.0, 120.0, 105.0, 88.0, 69.0, 48.0, 25.0, 0.0, -27.0, -56.0, -87.0, -120.0, -155.0, -192.0, -231.0, -204.0, -165.0, -128.0, -93.0, -60.0, -29.0, 0.0, 27.0, 52.0, 75.0, 96.0, 115.0, 132.0, 147.0, 160.0, 171.0, 180.0, 187.0, 192.0, 195.0, 196.0, 195.0, 192.0, 187.0, 180.0, 171.0, 160.0, 147.0, 132.0, 115.0, 96.0, 75.0, 52.0, 27.0, 0.0, -29.0, -60.0, -93.0, -128.0, -165.0, -204.0, -175.0, -136.0, -99.0, -64.0, -31.0, 0.0, 29.0, 56.0, 81.0, 104.0, 125.0, 144.0, 161.0, 176.0, 189.0, 200.0, 209.0, 216.0, 221.0, 224.0, 225.0, 224.0, 221.0, 216.0, 209.0, 200.0, 189.0, 176.0, 161.0, 144.0, 125.0, 104.0, 81.0, 56.0, 29.0, 0.0, -31.0, -64.0, -99.0, -136.0, -175.0, -144.0, -105.0, -68.0, -33.0, 0.0, 31.0, 60.0, 87.0, 112.0, 135.0, 156.0, 175.0, 192.0, 207.0, 220.0, 231.0, 240.0, 247.0, 252.0, 255.0, 256.0, 255.0, 252.0, 247.0, 240.0, 231.0, 220.0, 207.0, 192.0, 175.0, 156.0, 135.0, 112.0, 87.0, 60.0, 31.0, 0.0, -33.0, -68.0, -105.0, -144.0, -111.0, -72.0, -35.0, 0.0, 33.0, 64.0, 93.0, 120.0, 145.0, 168.0, 189.0, 208.0, 225.0, 240.0, 253.0, 264.0, 273.0, 280.0, 285.0, 288.0, 289.0, 288.0, 285.0, 280.0, 273.0, 264.0, 253.0, 240.0, 225.0, 208.0, 189.0, 168.0, 145.0, 120.0, 93.0, 64.0, 33.0, 0.0, -35.0, -72.0, -111.0, -76.0, -37.0, 0.0, 35.0, 68.0, 99.0, 128.0, 155.0, 180.0, 203.0, 224.0, 243.0, 260.0, 275.0, 288.0, 299.0, 308.0, 315.0, 320.0, 323.0, 324.0, 323.0, 320.0, 315.0, 308.0, 299.0, 288.0, 275.0, 260.0, 243.0, 224.0, 203.0, 180.0, 155.0, 128.0, 99.0, 68.0, 35.0, 0.0, -37.0, -76.0, -39.0, 0.0, 37.0, 72.0, 105.0, 136.0, 165.0, 192.0, 217.0, 240.0, 261.0, 280.0, 297.0, 312.0, 325.0, 336.0, 345.0, 352.0, 357.0, 360.0, 361.0, 360.0, 357.0, 352.0, 345.0, 336.0, 325.0, 312.0, 297.0, 280.0, 261.0, 240.0, 217.0, 192.0, 165.0, 136.0, 105.0, 72.0, 37.0, 0.0, -39.0, 0.0, 39.0, 76.0, 111.0, 144.0, 175.0, 204.0, 231.0, 256.0, 279.0, 300.0, 319.0, 336.0, 351.0, 364.0, 375.0, 384.0, 391.0, 396.0, 399.0, 400.0, 399.0, 396.0, 391.0, 384.0, 375.0, 364.0, 351.0, 336.0, 319.0, 300.0, 279.0, 256.0, 231.0, 204.0, 175.0, 144.0, 111.0, 76.0, 39.0, 0.0]
disp('Precomputed values of Z:');
disp(z)
res = [];
temp = [];
for xi = x
for yi = y
temp(end + 1) = interp2(x,y,z,xi,yi,"cubic");
endfor
res = [res; temp];
temp = [];
endfor
size(z)
size(res)
disp('Interpolated values of res:');
disp(res)
% Interstitial values
disp('Interstitial value calculation....');
inter_x = x(1:end-1).+0.5;
inter_y = y(1:end-1).+0.3;
res = [];
temp = [];
for xi = inter_x
for yi = inter_y
temp(end + 1) = interp2(x,y,z,xi,yi,"cubic");
endfor
res = [res; temp];
temp = [];
endfor
disp(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment