{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Illustration of robust polynomial fitting  \n",
    "2019-02-28 Jeff Fessler, University of Michigan  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using LinearAlgebra: Diagonal, svd\n",
    "using Random: seed!\n",
    "using Plots\n",
    "include(ENV[\"HOME\"] * \"/l/g/teach/w19-598-opt/hw/auto/test/ncg_inv.jl\")\n",
    "using Distributions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = (t) -> atan(4*(t-0.5)); # nonlinear function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "seed!(1) # seed rng\n",
    "M = 15\n",
    "tm = sort(rand(M)) # M random sample locations\n",
    "y = s.(tm) + 0.1 * randn(M); # noisy samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip0800\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2000\" height=\"2000\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<defs>\n",
       "  <clipPath id=\"clip0801\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip0801)\" points=\"\n",
       "0,1600 2400,1600 2400,0 0,0 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip0802\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip0801)\" points=\"\n",
       "252.496,1440.48 2321.26,1440.48 2321.26,47.2441 252.496,47.2441 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip0803\">\n",
       "    <rect x=\"252\" y=\"47\" width=\"2070\" height=\"1394\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,1249.39 2321.26,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,996.628 2321.26,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,743.863 2321.26,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,491.099 2321.26,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,238.334 2321.26,238.334 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 2321.26,1440.48 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 252.496,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1249.39 283.528,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,996.628 283.528,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,743.863 283.528,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,491.099 283.528,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,238.334 283.528,238.334 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 311.046, 1494.48)\" x=\"311.046\" y=\"1494.48\">0.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 798.962, 1494.48)\" x=\"798.962\" y=\"1494.48\">0.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1494.48)\" x=\"1286.88\" y=\"1494.48\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1774.79, 1494.48)\" x=\"1774.79\" y=\"1494.48\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2262.71, 1494.48)\" x=\"2262.71\" y=\"1494.48\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1266.89)\" x=\"228.496\" y=\"1266.89\">-1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1014.13)\" x=\"228.496\" y=\"1014.13\">-0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 761.363)\" x=\"228.496\" y=\"761.363\">0.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 508.599)\" x=\"228.496\" y=\"508.599\">0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 255.834)\" x=\"228.496\" y=\"255.834\">1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1590.4)\" x=\"1286.88\" y=\"1590.4\">t</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 57.6, 743.863)\" x=\"57.6\" y=\"743.863\">y</text>\n",
       "</g>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"327.602\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"327.602\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip0803)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"14\"/>\n",
       "<polyline clip-path=\"url(#clip0803)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1303.56 330.563,1299.45 350.079,1295.2 369.596,1290.82 389.113,1286.28 408.629,1281.59 428.146,1276.74 447.663,1271.72 467.179,1266.52 486.696,1261.14 \n",
       "  506.213,1255.56 525.729,1249.77 545.246,1243.78 564.762,1237.55 584.279,1231.1 603.796,1224.39 623.312,1217.43 642.829,1210.2 662.346,1202.68 681.862,1194.86 \n",
       "  701.379,1186.74 720.896,1178.28 740.412,1169.49 759.929,1160.34 779.445,1150.82 798.962,1140.91 818.479,1130.59 837.995,1119.85 857.512,1108.68 877.029,1097.06 \n",
       "  896.545,1084.96 916.062,1072.39 935.579,1059.33 955.095,1045.75 974.612,1031.67 994.128,1017.06 1013.65,1001.93 1033.16,986.274 1052.68,970.098 1072.2,953.409 \n",
       "  1091.71,936.22 1111.23,918.552 1130.74,900.427 1150.26,881.877 1169.78,862.938 1189.29,843.653 1208.81,824.068 1228.33,804.238 1247.84,784.22 1267.36,764.074 \n",
       "  1286.88,743.863 1306.39,723.653 1325.91,703.507 1345.43,683.489 1364.94,663.658 1384.46,644.074 1403.98,624.788 1423.49,605.849 1443.01,587.299 1462.53,569.175 \n",
       "  1482.04,551.506 1501.56,534.318 1521.08,517.629 1540.59,501.452 1560.11,485.797 1579.63,470.666 1599.14,456.059 1618.66,441.973 1638.18,428.401 1657.69,415.335 \n",
       "  1677.21,402.762 1696.73,390.67 1716.24,379.046 1735.76,367.873 1755.28,357.137 1774.79,346.822 1794.31,336.911 1813.83,327.388 1833.34,318.237 1852.86,309.443 \n",
       "  1872.38,300.99 1891.89,292.864 1911.41,285.048 1930.93,277.531 1950.44,270.297 1969.96,263.334 1989.48,256.63 2008.99,250.172 2028.51,243.95 2048.03,237.952 \n",
       "  2067.54,232.168 2087.06,226.589 2106.58,221.204 2126.09,216.005 2145.61,210.984 2165.13,206.133 2184.64,201.444 2204.16,196.909 2223.68,192.522 2243.19,188.277 \n",
       "  2262.71,184.167 \n",
       "  \"/>\n",
       "<polygon clip-path=\"url(#clip0801)\" points=\"\n",
       "324.496,312.204 634.001,312.204 634.001,130.764 324.496,130.764 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  324.496,312.204 634.001,312.204 634.001,130.764 324.496,130.764 324.496,312.204 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip0801)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"25\"/>\n",
       "<circle clip-path=\"url(#clip0801)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"21\"/>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 208.744)\" x=\"516.496\" y=\"208.744\">y</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip0801)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,251.724 492.496,251.724 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip0801)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 269.224)\" x=\"516.496\" y=\"269.224\">s(t)</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t0 = LinRange(0, 1, 101) # fine sampling for showing curve\n",
    "scatter(tm, y, color=:blue,\n",
    "    label=\"y\", xlabel=\"t\", ylabel=\"y\", ylim=[-1.3, 1.3])\n",
    "plot!(t0, s.(t0), color=:blue, label=\"s(t)\", legend=:topleft)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15×4 Array{Float64,2}:\n",
       " 1.0  0.00790928  6.25568e-5  4.94779e-7\n",
       " 1.0  0.210968    0.0445076   0.00938968\n",
       " 1.0  0.236033    0.0557117   0.0131498 \n",
       " 1.0  0.251662    0.0633339   0.0159387 \n",
       " 1.0  0.28119     0.0790679   0.0222331 \n",
       " 1.0  0.312707    0.0977856   0.0305783 \n",
       " 1.0  0.346517    0.120074    0.0416077 \n",
       " 1.0  0.424718    0.180385    0.0766128 \n",
       " 1.0  0.437108    0.191063    0.0835153 \n",
       " 1.0  0.488613    0.238742    0.116653  \n",
       " 1.0  0.555751    0.308859    0.171649  \n",
       " 1.0  0.773223    0.597874    0.46229   \n",
       " 1.0  0.951916    0.906145    0.862574  \n",
       " 1.0  0.986666    0.973511    0.96053   \n",
       " 1.0  0.999905    0.999809    0.999714  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deg = 3 # polynomial degree\n",
    "Afun = (tt) -> [t.^i for t in tt, i in 0:deg] # matrix of monomials\n",
    "A = Afun(tm) # M × 4 matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Float64,1}:\n",
       " -1.1217772852297472\n",
       " -0.7987637026605704\n",
       "  9.106395885988642 \n",
       " -6.099750221152859 "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xh = A \\ y # ordinary LS solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip1000\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2000\" height=\"2000\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1001\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip1001)\" points=\"\n",
       "0,1600 2400,1600 2400,0 0,0 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1002\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip1001)\" points=\"\n",
       "252.496,1440.48 2321.26,1440.48 2321.26,47.2441 252.496,47.2441 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1003\">\n",
       "    <rect x=\"252\" y=\"47\" width=\"2070\" height=\"1394\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,1249.39 2321.26,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,996.628 2321.26,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,743.863 2321.26,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,491.099 2321.26,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,238.334 2321.26,238.334 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 2321.26,1440.48 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 252.496,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1249.39 283.528,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,996.628 283.528,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,743.863 283.528,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,491.099 283.528,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,238.334 283.528,238.334 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 311.046, 1494.48)\" x=\"311.046\" y=\"1494.48\">0.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 798.962, 1494.48)\" x=\"798.962\" y=\"1494.48\">0.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1494.48)\" x=\"1286.88\" y=\"1494.48\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1774.79, 1494.48)\" x=\"1774.79\" y=\"1494.48\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2262.71, 1494.48)\" x=\"2262.71\" y=\"1494.48\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1266.89)\" x=\"228.496\" y=\"1266.89\">-1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1014.13)\" x=\"228.496\" y=\"1014.13\">-0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 761.363)\" x=\"228.496\" y=\"761.363\">0.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 508.599)\" x=\"228.496\" y=\"508.599\">0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 255.834)\" x=\"228.496\" y=\"255.834\">1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1590.4)\" x=\"1286.88\" y=\"1590.4\">t</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 57.6, 743.863)\" x=\"57.6\" y=\"743.863\">y</text>\n",
       "</g>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"327.602\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"327.602\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1003)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"14\"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1303.56 330.563,1299.45 350.079,1295.2 369.596,1290.82 389.113,1286.28 408.629,1281.59 428.146,1276.74 447.663,1271.72 467.179,1266.52 486.696,1261.14 \n",
       "  506.213,1255.56 525.729,1249.77 545.246,1243.78 564.762,1237.55 584.279,1231.1 603.796,1224.39 623.312,1217.43 642.829,1210.2 662.346,1202.68 681.862,1194.86 \n",
       "  701.379,1186.74 720.896,1178.28 740.412,1169.49 759.929,1160.34 779.445,1150.82 798.962,1140.91 818.479,1130.59 837.995,1119.85 857.512,1108.68 877.029,1097.06 \n",
       "  896.545,1084.96 916.062,1072.39 935.579,1059.33 955.095,1045.75 974.612,1031.67 994.128,1017.06 1013.65,1001.93 1033.16,986.274 1052.68,970.098 1072.2,953.409 \n",
       "  1091.71,936.22 1111.23,918.552 1130.74,900.427 1150.26,881.877 1169.78,862.938 1189.29,843.653 1208.81,824.068 1228.33,804.238 1247.84,784.22 1267.36,764.074 \n",
       "  1286.88,743.863 1306.39,723.653 1325.91,703.507 1345.43,683.489 1364.94,663.658 1384.46,644.074 1403.98,624.788 1423.49,605.849 1443.01,587.299 1462.53,569.175 \n",
       "  1482.04,551.506 1501.56,534.318 1521.08,517.629 1540.59,501.452 1560.11,485.797 1579.63,470.666 1599.14,456.059 1618.66,441.973 1638.18,428.401 1657.69,415.335 \n",
       "  1677.21,402.762 1696.73,390.67 1716.24,379.046 1735.76,367.873 1755.28,357.137 1774.79,346.822 1794.31,336.911 1813.83,327.388 1833.34,318.237 1852.86,309.443 \n",
       "  1872.38,300.99 1891.89,292.864 1911.41,285.048 1930.93,277.531 1950.44,270.297 1969.96,263.334 1989.48,256.63 2008.99,250.172 2028.51,243.95 2048.03,237.952 \n",
       "  2067.54,232.168 2087.06,226.589 2106.58,221.204 2126.09,216.005 2145.61,210.984 2165.13,206.133 2184.64,201.444 2204.16,196.909 2223.68,192.522 2243.19,188.277 \n",
       "  2262.71,184.167 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1003)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1310.95 330.563,1314.54 350.079,1317.21 369.596,1319.01 389.113,1319.94 408.629,1320.02 428.146,1319.28 447.663,1317.72 467.179,1315.37 486.696,1312.26 \n",
       "  506.213,1308.38 525.729,1303.77 545.246,1298.45 564.762,1292.42 584.279,1285.72 603.796,1278.35 623.312,1270.34 642.829,1261.71 662.346,1252.47 681.862,1242.64 \n",
       "  701.379,1232.24 720.896,1221.29 740.412,1209.81 759.929,1197.82 779.445,1185.33 798.962,1172.36 818.479,1158.94 837.995,1145.08 857.512,1130.79 877.029,1116.1 \n",
       "  896.545,1101.03 916.062,1085.59 935.579,1069.81 955.095,1053.7 974.612,1037.27 994.128,1020.56 1013.65,1003.57 1033.16,986.328 1052.68,968.849 1072.2,951.152 \n",
       "  1091.71,933.257 1111.23,915.18 1130.74,896.942 1150.26,878.56 1169.78,860.052 1189.29,841.438 1208.81,822.736 1228.33,803.965 1247.84,785.142 1267.36,766.286 \n",
       "  1286.88,747.417 1306.39,728.551 1325.91,709.709 1345.43,690.908 1364.94,672.167 1384.46,653.504 1403.98,634.938 1423.49,616.488 1443.01,598.171 1462.53,580.007 \n",
       "  1482.04,562.014 1501.56,544.21 1521.08,526.614 1540.59,509.244 1560.11,492.12 1579.63,475.258 1599.14,458.679 1618.66,442.4 1638.18,426.439 1657.69,410.817 \n",
       "  1677.21,395.55 1696.73,380.657 1716.24,366.158 1735.76,352.07 1755.28,338.411 1774.79,325.201 1794.31,312.459 1813.83,300.201 1833.34,288.447 1852.86,277.216 \n",
       "  1872.38,266.526 1891.89,256.395 1911.41,246.842 1930.93,237.886 1950.44,229.544 1969.96,221.836 1989.48,214.78 2008.99,208.394 2028.51,202.697 2048.03,197.708 \n",
       "  2067.54,193.444 2087.06,189.925 2106.58,187.169 2126.09,185.194 2145.61,184.019 2165.13,183.663 2184.64,184.144 2204.16,185.48 2223.68,187.69 2243.19,190.792 \n",
       "  2262.71,194.806 \n",
       "  \"/>\n",
       "<polygon clip-path=\"url(#clip1001)\" points=\"\n",
       "324.496,372.684 987.158,372.684 987.158,130.764 324.496,130.764 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  324.496,372.684 987.158,372.684 987.158,130.764 324.496,130.764 324.496,372.684 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip1001)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"25\"/>\n",
       "<circle clip-path=\"url(#clip1001)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"21\"/>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 208.744)\" x=\"516.496\" y=\"208.744\">y</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,251.724 492.496,251.724 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 269.224)\" x=\"516.496\" y=\"269.224\">s(t)</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1001)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,312.204 492.496,312.204 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1001)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 329.704)\" x=\"516.496\" y=\"329.704\">ordinary LS cubic fit</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot!(t0, Afun(t0)*xh, line=:red, label=\"ordinary LS cubic fit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Float64,1}:\n",
       " -1.3165917881067561\n",
       "  3.2310676853223086\n",
       " -3.94749649713934  \n",
       "  3.1398714413842437"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yc = copy(y)\n",
    "yc[12] = -0.5\n",
    "#seed!(8)\n",
    "#yc = s.(tm) + 0.2 * rand(Laplace(0,1), M)\n",
    "xc = A \\ yc # ordinary LS solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip1200\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2000\" height=\"2000\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1201\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip1201)\" points=\"\n",
       "0,1600 2400,1600 2400,0 0,0 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1202\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip1201)\" points=\"\n",
       "252.496,1440.48 2321.26,1440.48 2321.26,47.2441 252.496,47.2441 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1203\">\n",
       "    <rect x=\"252\" y=\"47\" width=\"2070\" height=\"1394\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,1249.39 2321.26,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,996.628 2321.26,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,743.863 2321.26,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,491.099 2321.26,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,238.334 2321.26,238.334 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 2321.26,1440.48 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 252.496,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1249.39 283.528,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,996.628 283.528,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,743.863 283.528,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,491.099 283.528,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,238.334 283.528,238.334 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 311.046, 1494.48)\" x=\"311.046\" y=\"1494.48\">0.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 798.962, 1494.48)\" x=\"798.962\" y=\"1494.48\">0.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1494.48)\" x=\"1286.88\" y=\"1494.48\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1774.79, 1494.48)\" x=\"1774.79\" y=\"1494.48\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2262.71, 1494.48)\" x=\"2262.71\" y=\"1494.48\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1266.89)\" x=\"228.496\" y=\"1266.89\">-1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1014.13)\" x=\"228.496\" y=\"1014.13\">-0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 761.363)\" x=\"228.496\" y=\"761.363\">0.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 508.599)\" x=\"228.496\" y=\"508.599\">0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 255.834)\" x=\"228.496\" y=\"255.834\">1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1590.4)\" x=\"1286.88\" y=\"1590.4\">t</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 57.6, 743.863)\" x=\"57.6\" y=\"743.863\">y</text>\n",
       "</g>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"996.628\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"996.628\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1203)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"14\"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1303.56 330.563,1299.45 350.079,1295.2 369.596,1290.82 389.113,1286.28 408.629,1281.59 428.146,1276.74 447.663,1271.72 467.179,1266.52 486.696,1261.14 \n",
       "  506.213,1255.56 525.729,1249.77 545.246,1243.78 564.762,1237.55 584.279,1231.1 603.796,1224.39 623.312,1217.43 642.829,1210.2 662.346,1202.68 681.862,1194.86 \n",
       "  701.379,1186.74 720.896,1178.28 740.412,1169.49 759.929,1160.34 779.445,1150.82 798.962,1140.91 818.479,1130.59 837.995,1119.85 857.512,1108.68 877.029,1097.06 \n",
       "  896.545,1084.96 916.062,1072.39 935.579,1059.33 955.095,1045.75 974.612,1031.67 994.128,1017.06 1013.65,1001.93 1033.16,986.274 1052.68,970.098 1072.2,953.409 \n",
       "  1091.71,936.22 1111.23,918.552 1130.74,900.427 1150.26,881.877 1169.78,862.938 1189.29,843.653 1208.81,824.068 1228.33,804.238 1247.84,784.22 1267.36,764.074 \n",
       "  1286.88,743.863 1306.39,723.653 1325.91,703.507 1345.43,683.489 1364.94,663.658 1384.46,644.074 1403.98,624.788 1423.49,605.849 1443.01,587.299 1462.53,569.175 \n",
       "  1482.04,551.506 1501.56,534.318 1521.08,517.629 1540.59,501.452 1560.11,485.797 1579.63,470.666 1599.14,456.059 1618.66,441.973 1638.18,428.401 1657.69,415.335 \n",
       "  1677.21,402.762 1696.73,390.67 1716.24,379.046 1735.76,367.873 1755.28,357.137 1774.79,346.822 1794.31,336.911 1813.83,327.388 1833.34,318.237 1852.86,309.443 \n",
       "  1872.38,300.99 1891.89,292.864 1911.41,285.048 1930.93,277.531 1950.44,270.297 1969.96,263.334 1989.48,256.63 2008.99,250.172 2028.51,243.95 2048.03,237.952 \n",
       "  2067.54,232.168 2087.06,226.589 2106.58,221.204 2126.09,216.005 2145.61,210.984 2165.13,206.133 2184.64,201.444 2204.16,196.909 2223.68,192.522 2243.19,188.277 \n",
       "  2262.71,184.167 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1203)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1409.44 330.563,1393.3 350.079,1377.56 369.596,1362.19 389.113,1347.19 408.629,1332.56 428.146,1318.28 447.663,1304.33 467.179,1290.73 486.696,1277.44 \n",
       "  506.213,1264.47 525.729,1251.8 545.246,1239.42 564.762,1227.33 584.279,1215.52 603.796,1203.97 623.312,1192.68 642.829,1181.63 662.346,1170.83 681.862,1160.25 \n",
       "  701.379,1149.88 720.896,1139.73 740.412,1129.78 759.929,1120.01 779.445,1110.43 798.962,1101.01 818.479,1091.76 837.995,1082.66 857.512,1073.7 877.029,1064.87 \n",
       "  896.545,1056.16 916.062,1047.57 935.579,1039.09 955.095,1030.69 974.612,1022.38 994.128,1014.15 1013.65,1005.98 1033.16,997.874 1052.68,989.81 1072.2,981.783 \n",
       "  1091.71,973.784 1111.23,965.803 1130.74,957.831 1150.26,949.858 1169.78,941.874 1189.29,933.871 1208.81,925.838 1228.33,917.766 1247.84,909.645 1267.36,901.467 \n",
       "  1286.88,893.221 1306.39,884.898 1325.91,876.488 1345.43,867.982 1364.94,859.371 1384.46,850.644 1403.98,841.793 1423.49,832.807 1443.01,823.678 1462.53,814.396 \n",
       "  1482.04,804.95 1501.56,795.333 1521.08,785.533 1540.59,775.542 1560.11,765.351 1579.63,754.949 1599.14,744.326 1618.66,733.475 1638.18,722.384 1657.69,711.045 \n",
       "  1677.21,699.448 1696.73,687.584 1716.24,675.442 1735.76,663.014 1755.28,650.29 1774.79,637.26 1794.31,623.914 1813.83,610.245 1833.34,596.241 1852.86,581.893 \n",
       "  1872.38,567.192 1891.89,552.128 1911.41,536.692 1930.93,520.874 1950.44,504.664 1969.96,488.054 1989.48,471.033 2008.99,453.593 2028.51,435.723 2048.03,417.414 \n",
       "  2067.54,398.656 2087.06,379.44 2106.58,359.757 2126.09,339.597 2145.61,318.95 2165.13,297.808 2184.64,276.159 2204.16,253.995 2223.68,231.307 2243.19,208.084 \n",
       "  2262.71,184.318 \n",
       "  \"/>\n",
       "<polygon clip-path=\"url(#clip1201)\" points=\"\n",
       "324.496,372.684 987.158,372.684 987.158,130.764 324.496,130.764 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  324.496,372.684 987.158,372.684 987.158,130.764 324.496,130.764 324.496,372.684 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip1201)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"25\"/>\n",
       "<circle clip-path=\"url(#clip1201)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"21\"/>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 208.744)\" x=\"516.496\" y=\"208.744\">y</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,251.724 492.496,251.724 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 269.224)\" x=\"516.496\" y=\"269.224\">s(t)</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1201)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,312.204 492.496,312.204 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1201)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 329.704)\" x=\"516.496\" y=\"329.704\">ordinary LS cubic fit</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scatter(tm, yc, color=:blue,\n",
    "    label=\"y\", xlabel=\"t\", ylabel=\"y\", ylim=[-1.3, 1.3])\n",
    "plot!(t0, s.(t0), color=:blue, label=\"s(t)\", legend=:topleft)\n",
    "plot!(t0, Afun(t0)*xc, line=:red, label=\"ordinary LS cubic fit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-1.14924, -0.517269, 8.83346, -6.09636], Any[0, 0, 0, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta = 0.01\n",
    "dpot = (z,delta) -> z / (1 + abs(z)/delta) # Fair potential\n",
    "xr,_ = ncg_inv([A], [v -> dpot.(v - yc,delta)], [1.], xc, niter=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip1400\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2000\" height=\"2000\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1401\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip1401)\" points=\"\n",
       "0,1600 2400,1600 2400,0 0,0 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1402\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polygon clip-path=\"url(#clip1401)\" points=\"\n",
       "252.496,1440.48 2321.26,1440.48 2321.26,47.2441 252.496,47.2441 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip1403\">\n",
       "    <rect x=\"252\" y=\"47\" width=\"2070\" height=\"1394\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,1249.39 2321.26,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,996.628 2321.26,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,743.863 2321.26,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,491.099 2321.26,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  252.496,238.334 2321.26,238.334 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 2321.26,1440.48 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1440.48 252.496,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1440.48 311.046,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  798.962,1440.48 798.962,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1286.88,1440.48 1286.88,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1774.79,1440.48 1774.79,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2262.71,1440.48 2262.71,1419.58 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,1249.39 283.528,1249.39 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,996.628 283.528,996.628 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,743.863 283.528,743.863 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,491.099 283.528,491.099 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  252.496,238.334 283.528,238.334 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 311.046, 1494.48)\" x=\"311.046\" y=\"1494.48\">0.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 798.962, 1494.48)\" x=\"798.962\" y=\"1494.48\">0.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1494.48)\" x=\"1286.88\" y=\"1494.48\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1774.79, 1494.48)\" x=\"1774.79\" y=\"1494.48\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2262.71, 1494.48)\" x=\"2262.71\" y=\"1494.48\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1266.89)\" x=\"228.496\" y=\"1266.89\">-1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 1014.13)\" x=\"228.496\" y=\"1014.13\">-0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 761.363)\" x=\"228.496\" y=\"761.363\">0.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 508.599)\" x=\"228.496\" y=\"508.599\">0.5</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 228.496, 255.834)\" x=\"228.496\" y=\"255.834\">1.0</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(0, 1286.88, 1590.4)\" x=\"1286.88\" y=\"1590.4\">t</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:66px; text-anchor:middle;\" transform=\"rotate(-90, 57.6, 743.863)\" x=\"57.6\" y=\"743.863\">y</text>\n",
       "</g>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"326.482\" cy=\"1326.73\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"722.785\" cy=\"1156.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"771.704\" cy=\"1157.19\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"802.206\" cy=\"1174.29\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"859.835\" cy=\"1196.99\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"921.345\" cy=\"1062.8\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"987.331\" cy=\"1060.51\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1139.95\" cy=\"979.171\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1164.13\" cy=\"828.216\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1264.65\" cy=\"733\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1395.69\" cy=\"605.096\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"996.628\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"1820.12\" cy=\"996.628\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2168.87\" cy=\"137.636\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2236.69\" cy=\"193.375\" r=\"14\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"18\"/>\n",
       "<circle clip-path=\"url(#clip1403)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"2262.52\" cy=\"221.891\" r=\"14\"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1303.56 330.563,1299.45 350.079,1295.2 369.596,1290.82 389.113,1286.28 408.629,1281.59 428.146,1276.74 447.663,1271.72 467.179,1266.52 486.696,1261.14 \n",
       "  506.213,1255.56 525.729,1249.77 545.246,1243.78 564.762,1237.55 584.279,1231.1 603.796,1224.39 623.312,1217.43 642.829,1210.2 662.346,1202.68 681.862,1194.86 \n",
       "  701.379,1186.74 720.896,1178.28 740.412,1169.49 759.929,1160.34 779.445,1150.82 798.962,1140.91 818.479,1130.59 837.995,1119.85 857.512,1108.68 877.029,1097.06 \n",
       "  896.545,1084.96 916.062,1072.39 935.579,1059.33 955.095,1045.75 974.612,1031.67 994.128,1017.06 1013.65,1001.93 1033.16,986.274 1052.68,970.098 1072.2,953.409 \n",
       "  1091.71,936.22 1111.23,918.552 1130.74,900.427 1150.26,881.877 1169.78,862.938 1189.29,843.653 1208.81,824.068 1228.33,804.238 1247.84,784.22 1267.36,764.074 \n",
       "  1286.88,743.863 1306.39,723.653 1325.91,703.507 1345.43,683.489 1364.94,663.658 1384.46,644.074 1403.98,624.788 1423.49,605.849 1443.01,587.299 1462.53,569.175 \n",
       "  1482.04,551.506 1501.56,534.318 1521.08,517.629 1540.59,501.452 1560.11,485.797 1579.63,470.666 1599.14,456.059 1618.66,441.973 1638.18,428.401 1657.69,415.335 \n",
       "  1677.21,402.762 1696.73,390.67 1716.24,379.046 1735.76,367.873 1755.28,357.137 1774.79,346.822 1794.31,336.911 1813.83,327.388 1833.34,318.237 1852.86,309.443 \n",
       "  1872.38,300.99 1891.89,292.864 1911.41,285.048 1930.93,277.531 1950.44,270.297 1969.96,263.334 1989.48,256.63 2008.99,250.172 2028.51,243.95 2048.03,237.952 \n",
       "  2067.54,232.168 2087.06,226.589 2106.58,221.204 2126.09,216.005 2145.61,210.984 2165.13,206.133 2184.64,201.444 2204.16,196.909 2223.68,192.522 2243.19,188.277 \n",
       "  2262.71,184.167 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1409.44 330.563,1393.3 350.079,1377.56 369.596,1362.19 389.113,1347.19 408.629,1332.56 428.146,1318.28 447.663,1304.33 467.179,1290.73 486.696,1277.44 \n",
       "  506.213,1264.47 525.729,1251.8 545.246,1239.42 564.762,1227.33 584.279,1215.52 603.796,1203.97 623.312,1192.68 642.829,1181.63 662.346,1170.83 681.862,1160.25 \n",
       "  701.379,1149.88 720.896,1139.73 740.412,1129.78 759.929,1120.01 779.445,1110.43 798.962,1101.01 818.479,1091.76 837.995,1082.66 857.512,1073.7 877.029,1064.87 \n",
       "  896.545,1056.16 916.062,1047.57 935.579,1039.09 955.095,1030.69 974.612,1022.38 994.128,1014.15 1013.65,1005.98 1033.16,997.874 1052.68,989.81 1072.2,981.783 \n",
       "  1091.71,973.784 1111.23,965.803 1130.74,957.831 1150.26,949.858 1169.78,941.874 1189.29,933.871 1208.81,925.838 1228.33,917.766 1247.84,909.645 1267.36,901.467 \n",
       "  1286.88,893.221 1306.39,884.898 1325.91,876.488 1345.43,867.982 1364.94,859.371 1384.46,850.644 1403.98,841.793 1423.49,832.807 1443.01,823.678 1462.53,814.396 \n",
       "  1482.04,804.95 1501.56,795.333 1521.08,785.533 1540.59,775.542 1560.11,765.351 1579.63,754.949 1599.14,744.326 1618.66,733.475 1638.18,722.384 1657.69,711.045 \n",
       "  1677.21,699.448 1696.73,687.584 1716.24,675.442 1735.76,663.014 1755.28,650.29 1774.79,637.26 1794.31,623.914 1813.83,610.245 1833.34,596.241 1852.86,581.893 \n",
       "  1872.38,567.192 1891.89,552.128 1911.41,536.692 1930.93,520.874 1950.44,504.664 1969.96,488.054 1989.48,471.033 2008.99,453.593 2028.51,435.723 2048.03,417.414 \n",
       "  2067.54,398.656 2087.06,379.44 2106.58,359.757 2126.09,339.597 2145.61,318.95 2165.13,297.808 2184.64,276.159 2204.16,253.995 2223.68,231.307 2243.19,208.084 \n",
       "  2262.71,184.318 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip1403)\" style=\"stroke:#ff00ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  311.046,1324.84 330.563,1327.01 350.079,1328.31 369.596,1328.75 389.113,1328.35 408.629,1327.13 428.146,1325.12 447.663,1322.32 467.179,1318.76 486.696,1314.45 \n",
       "  506.213,1309.41 525.729,1303.67 545.246,1297.24 564.762,1290.14 584.279,1282.38 603.796,1273.99 623.312,1264.98 642.829,1255.38 662.346,1245.2 681.862,1234.45 \n",
       "  701.379,1223.17 720.896,1211.36 740.412,1199.05 759.929,1186.25 779.445,1172.98 798.962,1159.27 818.479,1145.12 837.995,1130.56 857.512,1115.61 877.029,1100.28 \n",
       "  896.545,1084.6 916.062,1068.57 935.579,1052.23 955.095,1035.58 974.612,1018.66 994.128,1001.46 1013.65,984.027 1033.16,966.361 1052.68,948.487 1072.2,930.422 \n",
       "  1091.71,912.186 1111.23,893.795 1130.74,875.27 1150.26,856.628 1169.78,837.889 1189.29,819.07 1208.81,800.19 1228.33,781.267 1247.84,762.32 1267.36,743.368 \n",
       "  1286.88,724.429 1306.39,705.521 1325.91,686.663 1345.43,667.874 1364.94,649.171 1384.46,630.574 1403.98,612.101 1423.49,593.77 1443.01,575.6 1462.53,557.61 \n",
       "  1482.04,539.817 1501.56,522.241 1521.08,504.9 1540.59,487.812 1560.11,470.995 1579.63,454.47 1599.14,438.253 1618.66,422.363 1638.18,406.819 1657.69,391.639 \n",
       "  1677.21,376.842 1696.73,362.447 1716.24,348.471 1735.76,334.934 1755.28,321.853 1774.79,309.247 1794.31,297.135 1813.83,285.536 1833.34,274.467 1852.86,263.947 \n",
       "  1872.38,253.996 1891.89,244.63 1911.41,235.869 1930.93,227.731 1950.44,220.235 1969.96,213.399 1989.48,207.241 2008.99,201.781 2028.51,197.036 2048.03,193.026 \n",
       "  2067.54,189.768 2087.06,187.281 2106.58,185.584 2126.09,184.695 2145.61,184.632 2165.13,185.415 2184.64,187.061 2204.16,189.589 2223.68,193.018 2243.19,197.366 \n",
       "  2262.71,202.651 \n",
       "  \"/>\n",
       "<polygon clip-path=\"url(#clip1401)\" points=\"\n",
       "324.496,433.164 987.158,433.164 987.158,130.764 324.496,130.764 \n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  324.496,433.164 987.158,433.164 987.158,130.764 324.496,130.764 324.496,433.164 \n",
       "  \"/>\n",
       "<circle clip-path=\"url(#clip1401)\" style=\"fill:#000000; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"25\"/>\n",
       "<circle clip-path=\"url(#clip1401)\" style=\"fill:#0000ff; stroke:none; fill-opacity:1\" cx=\"432.496\" cy=\"191.244\" r=\"21\"/>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 208.744)\" x=\"516.496\" y=\"208.744\">y</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,251.724 492.496,251.724 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 269.224)\" x=\"516.496\" y=\"269.224\">s(t)</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,312.204 492.496,312.204 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 329.704)\" x=\"516.496\" y=\"329.704\">ordinary LS cubic fit</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip1401)\" style=\"stroke:#ff00ff; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  348.496,372.684 492.496,372.684 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip1401)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 516.496, 390.184)\" x=\"516.496\" y=\"390.184\">robust cubic fit</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot!(t0, Afun(t0)*xr, line=:magenta, label=\"robust cubic fit\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#savefig(\"tmp.pdf\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.1.0",
   "language": "julia",
   "name": "julia-1.1"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
