1
$\begingroup$

I am trying to carry out list operation but running into a following error I have large data sets(more than 0.2 million data points) and desired output will be a best fit (numerical data points ) to my experimental obtained data points. Please suggest any solution for following errors.

Thread::tdlen: Objects of unequal length in {0.0164708,0.0164689,0.0164671,0.0164652,0.0164634,0.0164616,0.0164597,0.0164579,0.0164561,0.0164542,0.0164524,0.0164506,<<27>>,0.0163994,0.0163976,0.0163957,0.0163939,0.0163921,0.0163903,0.0163885,0.0163866,0.0163848,0.016383,0.0163812,<<224930>>}+{<<1>>} cannot be combined. Total::tllen: Lists of unequal length in ({-0.0164708,-0.016471,-0.0164712,-0.0164714,-0.0164715,-0.0164715,<<39>>,-0.0164303,-0.0164282,-0.016426,-0.0164238,-0.0164215,<<224929>>}+{<<1>>})^2 cannot be added. How could I avoid it and what could be the reason of its manifestation? code i m getting error in:

(* Final version *)

data = Import["F:\\text csv-160615\\6mw-2\\6mw-2.csv"];
Dimensions@data

data = Drop[data, {-1}];
datamean = Mean[data[[All, 2]]];

ti = 99.48;
tf = 1599.34;
dt = 2/300.;
ndsol1 = ParametricNDSolve[{y'[x] == -k1*y[x] - k2*y[x]*y[x] - 
     k3*y[x]*y[x]*y[x], y[ti] == 0.016470755}, 
  y, {x, ti, tf}, {k1, k2, k3}]

output = ParallelTable[
    ndsol2 = y[k1, k2, k3] /. ndsol1;
    mat = 
     Flatten@Table[Evaluate[ndsol2[x] /. ndsol1], {x, ti, tf, dt}];
    gendata = Transpose[{Range[ti, tf, dt], mat}];
    (*  Fitness test *)
    ssres = Total[(gendata[[All, 2]] - data[[All, 2]])^2];
    sstot = Total[(data[[All, 2]] - datamean)^2];
    r2 = 1 - (ssres/sstot);
    {k1, k2, k3, r2}, {k1, 0, 0, 0}, {k2, 1, 1.1, 0.1}, {k3, 1, 1.1, 
     0.1}]; // AbsoluteTiming
output = Flatten[output, 2];

pos = Ordering[output[[All, 4]], -1]
selvalues = Flatten@output[[pos]]

ndsol21 = y[selvalues[[1]], selvalues[[2]], selvalues[[3]]] /. ndsol1;
mat1 = Flatten@Table[Evaluate[ndsol21[x] /. ndsol1], {x, ti, tf, dt}];
gendata1 = Transpose[{Range[ti, tf, dt], mat1}];

output = Export["F:\\text csv-160615\\6mw-2\\6mw-2-op.xlsx", 
  gendata1[[1 ;; -1 ;; 10, All]]]
(*Plot[Evaluate[ndsol2[x]/.ndsol1],{x,80,1500}]*)

ListPlot[{gendata1, data}, PlotStyle -> PointSize[Small], 
 PlotRange -> All, AxesOrigin -> {0, -.002}]
$\endgroup$
  • 2
    $\begingroup$ This may be only a speculation, I think the problem manifests because you are trying to do something with two data sets of unequal lengths where the function you are applying requires them to be of equal lengths. Please post your code so people can try and help you out $\endgroup$ – e.doroskevic Jul 16 '16 at 9:37
  • $\begingroup$ this error occurred for 5 data set out of 11.when i m changing t_f (last data point of x-axis ) to second last t_f-1, error is not coming for 3 out of 5 files.and completely clueless why is happening. change in x data points are same 2/300, but y data sets are experimental obtained data point. $\endgroup$ – avinash Jul 16 '16 at 9:57
  • $\begingroup$ so what is exactly meant by unequal legth? $\endgroup$ – avinash Jul 16 '16 at 9:58
  • $\begingroup$ i will post a code soon. $\endgroup$ – avinash Jul 16 '16 at 9:58
  • 1
    $\begingroup$ If you do Length[a] you get the length of the list. For certain operations the lengths of the lists need to be equal. For multiple dimension lists use Dimensions[] $\endgroup$ – Feyre Jul 16 '16 at 9:59
1
$\begingroup$

If the data has the dimensions {n, 2} where n = Length[Range[ti, tf, dt]] things should work fine.

Below is a minimal data set that has a length of 501.

ti = 100;
tf = 1600;
dt = 3.;

ndsol1 = ParametricNDSolve[{y'[x] == -k1*y[x] - k2*y[x]*y[x] - 
     k3*y[x]*y[x]*y[x], y[ti] == 0.016470755}, 
     y, {x, ti, tf}, {k1, k2, k3}]

junk = y[0, 1.05, 1.05] /. ndsol1;
junk2 = Flatten@Table[Evaluate[junk[x] /. ndsol1], {x, ti, tf, dt}];
data = Transpose[{Range[ti, tf, dt], junk2}];

Length[Range[ti, tf, dt]]
(* 501 *)

Dimensions@data
(* {501, 2} *)

Now invoke ParallelTable

output = ParallelTable[

  ndsol2 = y[k1, k2, k3] /. ndsol1;
  mat = Flatten@Table[Evaluate[ndsol2[x] /. ndsol1], {x, ti, tf, dt}];
  gendata = Transpose[{Range[ti, tf, dt], mat}];

  (*Fitness test*)
  ssres = Total[(gendata[[All, 2]] - data[[All, 2]])^2];
  sstot = Total[(data[[All, 2]] - datamean)^2];
  r2 = 1 - (ssres/sstot);

  {k1, k2, k3, r2},
  {k1, 0, 0, 0},
  {k2, 1, 1.1, 0.1},
  {k3, 1, 1.1, 0.1}
  ]

(* {{{{0, 1., 1., 0.998722}, {0, 1., 1.1, 0.998761}}, {{0, 1.1, 
    1., 0.998918}, {0, 1.1, 1.1, 0.998886}}}} *)
$\endgroup$
0
$\begingroup$

This is an extended comment as @JackLaVigne and @e.doroskevic have already provided the answer. Below is a process I use when I mess up and attempt to combine lists of unequal length.

The error shown is actually two errors:

Thread::tdlen: Objects of unequal length in {0.0164708,0.0164689,0.0164671,0.0164652,0.0164634,0.0164616,0.0164597,0.0164579,0.0164561,0.0164542,0.0164524,0.0164506,<<27>>,0.0163994,0.0163976,0.0163957,0.0163939,0.0163921,0.0163903,0.0163885,0.0163866,0.0163848,0.016383,0.0163812,<<224930>>}+{<<1>>} cannot be combined.

Total::tllen: Lists of unequal length in ({-0.0164708,-0.016471,-0.0164712,-0.0164714,-0.0164715,-0.0164715,<<39>>,-0.0164303,-0.0164282,-0.016426,-0.0164238,-0.0164215,<<224929>>}+{<<1>>})^2 cannot be added.

Each error is about the combining or adding of two lists and a compact version of the first list in each "bad" statement is shown. The number of elements of that first list can be determined by counting the numbers shown and the "counts" given between << and >>:

23 + 27 + 224930 = 224980

and

11 + 39 + 224929 = 224929 

We know that

ti = 99.48;
tf = 1599.34;
dt = 2/300.;
r = Range[ti, tf, dt], mat}];
Length[r]

results in a list with 224930 elements. The first error must belong to

ssres = Total[(gendata[[All, 2]] - data[[All, 2]])^2];

and the second error is from

sstot = Total[(data[[All, 2]] - datamean)^2];

Therefore gendata[[All,2]] has 224930 elements and data[[All,2]] has just 224929 elements.

Maybe the last row of "F:\text csv-160615\6mw-2\6mw-2.csv" is not read in?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.