0
$\begingroup$

So I have a very simple question related to a simple topic, but also directly related to a fitting problem. I see that my question is related to this question but I don't manage to make the bridge between them.

The basic question is how to make a function that would look like :

list = {{1, 2}, 2, {3, 4, 8, 12}, 4, {2, 2, 2, 2, 2}}
F[x_] = list[[x]]

And I want to use it in the following context :

err[Vinitphi_?NumberQ, l_?NumberQ, nc_?NumberQ, tauC_?NumberQ, 
  tauG_?NumberQ, D0_?NumberQ, k_] = 
 With[{model = (Rad /. pfun)[Vinitphi, l, nc, tauC, tauG, D0]}, 
  Norm[rad[[k]][[2 ;; Length[rad[[k]]], 2]] - 
    model /@ rad[[k]][[2 ;; Length[rad[[k]]], 1]]]]

fit = FindMinimum[{Sum[
    err[Vinitphi/2^(i - 1), l, nc, tauC, tauG, D0, i], {i, 1, 6, 1}], 
   3*10^7 < Vinitphi < 9*10^7, 15 < l < 30, 0.05 < tauC < 0.5, 
   0.2 < nc < 0.8, 
   4*10^(10) < D0 < 2*10^(11)}, {{Vinitphi, 4.5*10^7}, {l, 25}, {nc, 
    0.5}, {tauC, 0.2}, {tauG, 30}, {D0, 6*10^(10)}}]

where I get the error :

Part::pkspec1: The expression k cannot be used as a part specification.

Could you help please ?

I write in the following appendix the ParametricNDSolve I'm taking care of :

Appendix :

rad={{{0., 117.705}, {3., 148.255}, {6., 176.81}, {9., 183.561}, {12., 
   197.419}, {15., 210.672}, {18., 211.152}, {21., 209.889}, {24., 
   207.741}, {27., 204.352}, {30., 201.79}, {33., 199.976}, {36., 
   199.04}, {39., 197.151}, {42., 197.584}, {45., 196.198}, {48., 
   195.153}, {51., 195.711}, {54., 194.088}, {57., 193.304}, {60., 
   192.474}, {63., 192.13}, {66., 192.877}, {69., 192.371}, {72., 
   192.657}, {75., 190.984}, {78., 190.685}, {81., 190.449}, {84., 
   189.83}, {87., 189.625}, {90., 194.855}, {93., 186.581}, {96., 
   184.735}, {99., 184.586}, {102., 183.505}, {105., 181.531}, {108., 
   179.925}, {111., 178.428}, {114., 176.164}, {117., 175.375}, {120.,
    174.782}, {123., 172.649}, {126., 170.454}, {129., 
   168.357}, {132., 168.04}, {135., 167.26}, {138., 165.657}, {141., 
   164.797}, {144., 163.705}, {147., 161.214}, {150., 160.5}, {153., 
   159.353}, {156., 157.873}, {159., 157.225}}, {{0., 51.7792}, {3., 
   80.825}, {6., 108.913}, {9., 121.147}, {12., 130.805}, {15., 
   140.562}, {18., 143.615}, {21., 146.513}, {24., 147.63}, {27., 
   147.12}, {30., 146.693}, {33., 147.396}, {36., 148.256}, {39., 
   147.737}, {42., 148.685}, {45., 149.043}, {48., 147.814}, {51., 
   148.776}, {54., 147.959}, {57., 147.775}, {60., 148.031}, {63., 
   148.284}, {66., 148.334}, {69., 148.521}, {72., 148.974}, {75., 
   146.562}, {78., 145.734}, {81., 145.177}, {84., 145.588}, {87., 
   144.949}, {90., 147.035}, {93., 141.755}, {96., 140.841}, {99., 
   139.94}, {102., 138.25}, {105., 136.508}, {108., 135.52}, {111., 
   133.758}, {114., 132.694}, {117., 131.744}, {120., 131.208}, {123.,
    130.292}, {126., 127.612}, {129., 126.981}, {132., 
   127.035}, {135., 125.198}, {138., 123.557}, {141., 123.946}, {144.,
    120.738}, {147., 119.875}, {150., 118.828}, {153., 
   118.162}, {156., 117.363}, {159., 116.712}}, {{0., 29.62}, {3., 
   53.1414}, {6., 67.2233}, {9., 82.5676}, {12., 83.5019}, {15., 
   92.3142}, {18., 98.9869}, {21., 102.557}, {24., 106.481}, {27., 
   107.188}, {30., 107.637}, {33., 108.415}, {36., 109.622}, {39., 
   110.593}, {42., 111.205}, {45., 111.396}, {48., 111.668}, {51., 
   114.126}, {54., 113.3}, {57., 114.27}, {60., 114.849}, {63., 
   110.808}, {66., 116.51}, {69., 118.796}, {72., 119.636}, {75., 
   118.02}, {78., 116.026}, {81., 116.767}, {84., 116.994}, {87., 
   119.169}, {90., 121.246}, {93., 116.291}, {96., 117.296}, {99., 
   117.72}, {102., 115.814}, {105., 114.76}, {108., 114.853}, {111., 
   113.886}, {114., 112.522}, {117., 112.109}, {120., 112.376}, {123.,
    110.998}, {126., 109.708}, {129., 108.926}, {132., 
   108.075}, {135., 107.182}, {138., 106.723}, {141., 106.562}, {144.,
    103.807}, {147., 102.798}, {150., 102.333}, {153., 
   101.633}, {156., 100.395}, {159., 99.889}}, {{0., 79.5768}, {3., 
   86.0729}, {6., 101.334}, {9., 103.158}, {12., 104.818}, {15., 
   104.534}, {18., 104.361}, {21., 105.568}, {24., 107.109}, {27., 
   105.042}, {30., 105.165}, {33., 107.669}, {36., 108.182}, {39., 
   108.549}, {42., 109.208}, {45., 109.714}, {48., 110.098}, {51., 
   110.481}, {54., 110.373}, {57., 110.563}, {60., 111.115}, {63., 
   111.766}, {66., 112.415}, {69., 113.322}, {72., 113.272}, {75., 
   113.95}, {78., 113.98}, {81., 114.017}, {84., 111.879}, {87., 
   114.706}, {90., 112.125}, {93., 109.696}, {96., 112.481}, {99., 
   109.528}, {102., 108.22}, {105., 108.112}, {108., 107.387}, {111., 
   106.369}, {114., 106.522}, {117., 105.678}, {120., 111.234}, {123.,
    109.391}, {126., 104.95}, {129., 109.079}, {132., 109.363}, {135.,
    100.807}, {138., 99.9696}, {141., 100.622}, {144., 99.789}, {147.,
    98.5068}, {150., 99.6161}, {153., 97.4872}, {156., 
   101.554}, {159., 101.406}}, {{0., 30.4597}, {3., 35.889}, {6., 
   45.7724}, {9., 54.0641}, {12., 56.851}, {15., 59.1402}, {18., 
   61.0664}, {21., 63.1851}, {24., 65.2428}, {27., 66.6239}, {30., 
   67.5882}, {33., 68.5353}, {36., 69.885}, {39., 71.1742}, {42., 
   72.485}, {45., 73.2793}, {48., 74.2798}, {51., 74.7271}, {54., 
   74.8248}, {57., 75.83}, {60., 76.1228}, {63., 77.5324}, {66., 
   76.4005}, {69., 77.5578}, {72., 80.4519}, {75., 80.1548}, {78., 
   80.1533}, {81., 79.2626}, {84., 79.6456}, {87., 79.5882}, {90., 
   78.9125}, {93., 77.5023}, {96., 80.9046}, {99., 78.125}, {102., 
   78.1087}, {105., 82.0064}, {108., 80.6066}, {111., 82.9245}, {114.,
    83.6384}, {117., 82.0775}, {120., 81.1198}, {123., 75.248}, {126.,
    78.1893}, {129., 72.6991}, {132., 72.683}, {135., 80.5139}, {138.,
    83.442}, {141., 81.0871}, {144., 80.1472}, {147., 79.3543}, {150.,
    79.0979}, {153., 79.0636}, {156., 78.3953}, {159., 
   77.2895}}, {{0., 27.5731}, {3., 27.4456}, {6., 37.2589}, {9., 
   40.9683}, {12., 43.2509}, {15., 44.3384}, {18., 46.5891}, {21., 
   48.0219}, {24., 49.6954}, {27., 51.1536}, {30., 51.7754}, {33., 
   53.2019}, {36., 54.9082}, {39., 55.9732}, {42., 57.3092}, {45., 
   58.6397}, {48., 58.9803}, {51., 58.6734}, {54., 60.6691}, {57., 
   61.3107}, {60., 62.1459}, {63., 63.2534}, {66., 64.1169}, {69., 
   64.7201}, {72., 65.4561}, {75., 65.7958}, {78., 65.9518}, {81., 
   66.9163}, {84., 67.5038}, {87., 64.925}, {90., 69.8176}, {93., 
   65.8334}, {96., 69.2665}, {99., 66.6777}, {102., 65.9855}, {105., 
   69.4403}, {108., 70.2052}, {111., 69.9442}, {114., 70.7562}, {117.,
    70.3714}, {120., 63.5385}, {123., 62.8021}, {126., 
   67.0399}, {129., 61.5096}, {132., 63.1028}, {135., 70.3602}, {138.,
    70.2032}, {141., 69.4146}, {144., 68.2243}, {147., 
   67.9524}, {150., 67.8477}, {153., 68.2036}, {156., 68.2165}, {159.,
    67.5863}}}

eps = 1;
phic = 0.85;
v = 80
a = 0
phi0 = 0.63;
L = 5000;
nL0 = 1;

pfun = ParametricNDSolve[{Derivative[1][V][
     t] == -(( 
      D0 (E^(-((L + t v)^2/(4 D0 t)))) (-L + t v) Vinitphi )/((D0 t)^(
       3/2)*4*Sqrt[Pi]*phi0)) + (
     2 Vphi[t] (phic - Vphi[t]/V[t]) (-2 + phic + 
        2 (1 - Vphi[t]/V[t])) (1 - (1 - Vphi[t]/V[t])^2))/((1 - 
        phic)^2 tauC V[t]^(
      1/3)) - (-nc V[t] - (
        4 l L nL0 \[Pi] Csch[((E^(((a Vphi[t])/(
           2 V[t])))) ((3/\[Pi])^(1/3)) (V[t]^(1/3)) )/(
          2^(2/3) l)] (-E^(-((a Vphi[t])/V[t]))
              l^2 Sinh[((E^(((a Vphi[t])/(2 V[t])))) ((3/\[Pi])^(
              1/3)) (V[t]^(1/3)) )/(2^(2/3) l)] + (
           E^(-((a Vphi[t])/(2 V[t]))) l (3/\[Pi])^(1/3)
             Cosh[((E^(((a Vphi[t])/(2 V[t])))) ((3/\[Pi])^(1/3)) (
              V[t]^(1/3)) )/(2^(2/3) l)] V[t]^(1/3))/(2^(
           2/3)) ))/ (-L Coth[((E^(((a Vphi[t])/(
             2 V[t])))) ((3/\[Pi])^(1/3)) (V[t]^(1/3)) )/(
            2^(2/3) l)] + ((3/\[Pi])^(1/3)
            Coth[((E^(((a Vphi[t])/(2 V[t])))) ((3/\[Pi])^(1/3)) (
             V[t]^(1/3)) )/(2^(2/3) l)] V[t]^(1/3))/2^(2/3) - l))/
      tauG, V[eps] == 4*Pi/3*2^3, 
   Derivative[1][Vphi][
     t] == -((D0 (E^(-((L + t v)^2/(4 D0 t)))) (-L + t v) Vinitphi )/(
      4*Sqrt[Pi]*phi0 (D0 t)^(3/2))) + ((
      Vphi[t] (-nc V[t] - (
         4 l L nL0 \[Pi] Csch[((E^(((a Vphi[t])/(
            2 V[t])))) ((3/\[Pi])^(1/3)) (V[t]^(1/3)) )/(
           2^(2/3) l)] (-E^(-((a Vphi[t])/V[t]))
               l^2 Sinh[((E^(((a Vphi[t])/(2 V[t])))) ((3/\[Pi])^(
               1/3)) (V[t]^(1/3)) )/(2^(2/3) l)] + (
            E^(-((a Vphi[t])/(2 V[t]))) l (3/\[Pi])^(1/3)
              Cosh[((E^(((a Vphi[t])/(2 V[t])))) ((3/\[Pi])^(1/3)) (
               V[t]^(1/3)) )/(2^(2/3) l)] V[t]^(1/3))/(2^(
            2/3)) ))/ (-L Coth[((E^(((a Vphi[t])/(
              2 V[t])))) ((3/\[Pi])^(1/3)) (V[t]^(1/3)) )/(
             2^(2/3) l)] + ((3/\[Pi])^(1/3)
             Coth[((E^(((a Vphi[t])/(2 V[t])))) ((3/\[Pi])^(1/3)) (
              V[t]^(1/3)) )/(2^(2/3) l)] V[t]^(1/3))/2^(2/3) - l)))/ 
      V[t]) /tauG, Vphi[eps] ==4*Pi/3*2^3*phi0, 
   Rad'[t] == V'[t]/V[t]^(2/3)/(4*Pi/3)^(1/3), 
   Rad[eps] == 2 }, {V, Vphi, Rad}, {t, eps, 170}, {Vinitphi, l, 
   nc, tauC, tauG, D0}, 
  Method -> {"EquationSimplification" -> "Residual"}]

You can check my initial guess is not too bad :

enter image description here

$\endgroup$
  • $\begingroup$ try F[x_Integer] := list[[x]]? $\endgroup$ – kglr Jun 19 at 19:45

Your Answer

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

Browse other questions tagged or ask your own question.