I'm trying to get a piecewise linear best-fit for the closing price of one of the stocks I'm interested in. The logic seems ok, and the workflow works for a straight-line (ie 2 pts, ie 1 segment) regression (actually NMinimize)... but if I increase the the number of variables to solve for, it breaks and complains about "abscissa"... the code is below with the comments. Also here is the [data file](http://csv.pub/wakanda/data_out.txt) . Can you help me figure out whats wrong? (also here is the [nb file](http://csv.pub/wakanda/skeleton2.nb) in case you need it).

[![data plot superimposed with the excel piecewise linear fit](https://i.stack.imgur.com/WPU1E.png)](https://i.stack.imgur.com/WPU1E.png)

```

In[120]:= (d = 
   Import["data_out.txt", "CSV"]);

In[121]:= d = 
 d[[4533 ;; 4922, 
   5]]; (*get close price data for 14-may from the dataset*)

In[122]:= d = 
 Flatten[{Table[i, {i, 390}], 
   d}, {2}]; (*add an index for the price ie 1-390 data pts*)

In[123]:= (* create objective function to minimize:
1. piecewise linear interpoltion function takes a set of points "p" 
2. applies function to the index
3. subtracts the close price 
4. squares the diffrenence
5. sum
6. root *)

In[124]:= 
e[p_] := Total[(Interpolation[p, InterpolationOrder -> 1]@
       d[[All, 1]] - d[[All, 2]])^2]^0.5

In[125]:= (*this is the solution i got using excel solver*)

In[126]:= 
excelsolution = {{1, 32.69967765}, {28.16280834, 
    31.37817608}, {108.0001043, 32.75429029}, {135.5658831, 
    31.7584233}, {299.8762066, 32.76192525}, {390, 32.88427106}};

In[127]:= e[excelsolution]

Out[127]= 2.13146

In[128]:= ListPlot[{d, excelsolution}, Joined -> {False, True}, 
 PlotMarkers -> {{Automatic, Tiny}, {Automatic, Small}}] (* run to see it *)

Out[128]= (*graphics pasted above*)

In[129]:= (*this works*)

In[130]:= NMinimize[
 {
  e[{{1, y0}, {390, y390}}],
  31.5 <= y0 <= 33 && 31.5 <= y390 <= 33
  },
 {y0, y390},
 Method -> "DifferentialEvolution"
 ]

Out[130]= {5.51241, {y0 -> 31.7968, y390 -> 32.8737}}

In[131]:= (*but this doesn't work when i increase the number of \
arguments*)

In[132]:= NMinimize[
 {
  e[{{1, y0}, {x1, y1}, {390, y390}}], 
  31.5` <= y0 <= 33 && 1 <= x1 <= 390 && 31.5` <= y1 <= 33 && 
   31.5` <= y390 <= 33
  },
 {y0, x1, y1, y390},
 Method -> "DifferentialEvolution"
 ]

During evaluation of In[132]:= Interpolation::indat: Data point {x1,y1} contains abscissa x1, which is not a real number.

During evaluation of In[132]:= Interpolation::indat: Data point {x1,y1} contains abscissa x1, which is not a real number.

During evaluation of In[132]:= Interpolation::indat: Data point {x1,y1} contains abscissa x1, which is not a real number.

During evaluation of In[132]:= General::stop: Further output of Interpolation::indat will be suppressed during this calculation.

During evaluation of In[132]:= NMinimize::nnum: The function value {10.8271,10.8107,10.7944,10.7781,10.7618,10.7456,10.7294,10.7133,<<35>>,10.157,10.1423,10.1276,10.113,10.0984,10.0839,10.0694,<<340>>} is not a number at {x1,y0,y1,y390} = {285.476,32.6896,32.3246,32.9648}.

```