2
$\begingroup$

I have a very unpleasent problem with Mathematica, precisely with Table and/or Evaluate. After solving a parametric system with ParametricNDSolve, I'd like to collect the values of the solutions $f_3(x)$ and $v_3(x)$ using Table. However this works only for one of the two functions. The code is the following

system = {
   f'[x] == g[x], 
   g'[x] == -2 g[x]/x + 2*f[x]*v[x]^2/(x^2 - 1/x),
   v'[x] == a[x], 
   a'[x] == -((2x + 1/x^2)/(x^2 - 1/x))*a[x] - 2*a[x]/x - (f[x]/(x^2 - 1/x))^2*v[x] + (-2/(x^2 -1/x))*v[x],
   (*initial conditions below*)
   f[1.01] == 0 , 
   f[1000000] == 1, 
   v[1.01] == k, 
   a[1.01] == (-2/3)*k};

sol = ParametricNDSolve[system, {f, v}, {x, 1.01, 1000000}, {k}];

datapotential = 
  Table[{i, Evaluate[f[3][x] /. sol[[1]]] /. x -> i}, {i, 10, 20}];

datascalar = 
  Table[{i, Evaluate[v[3][x] /. sol[[1]]] /. x -> i}, {i, 10, 20}];

the first Table correctly gives me

{{{10, 0.552959}, {11, 0.583077}, {12, 0.6095}, {13, 0.632846}, {14, 
   0.653609}, {15, 0.672188}, {16, 0.688902}, {17, 0.704015}, {18, 
   0.717743}, {19, 0.730266}, {20, 0.741735}}}

while the second one gives

{{{10, v[3][10]}, {11, v[3][11]}, {12, v[3][12]}, {13, v[3][13]}, {14,
    v[3][14]}, {15, v[3][15]}, {16, v[3][16]}, {17, v[3][17]}, {18, 
   v[3][18]}, {19, v[3][19]}, {20, v[3][20]}}}

like it isn't able to evaluate $v_3(x)$ at the desired points! The wierd fact is that I can evaluate and plot both the functions $f$ and $v$ for every value of the parameter! For instance I can compute the value of $v_3$ in $x=4$ and get

Evaluate[v[3][4] /. sol]
(*0.997863620075214*).

I tried with many values of the parameter (not only 3) and many intervals but the problem is always there. I've also tried to literally copy and paste the line, and it works if I substitute $v$ with $f$! I didn't find anyone with this problem, but any suggestion to similar problems is appreciated. Thank you very much for your help!

$\endgroup$
1

1 Answer 1

2
$\begingroup$

You need to use v on the correct part of the solution as follows

sol = ParametricNDSolve[system, {f, v}, {x, 1.01, 1000000}, {k}];

datapotential = 
 Table[{i, Evaluate[f[3][x] /. sol[[1]]] /. x -> i}, {i, 10, 20}]

datascalar = 
 Table[{i, Evaluate[v[3][x] /. sol[[2]]] /. x -> i}, {i, 10, 20}]

Now both Tables will generate the data

Mathematica graphics

You just had the wrong part number in the second case and that is why it was not evaluating.

A better way is to just use /.sol for both and not having to worry about the part number. Like this

datapotential = 
 Table[{i, Evaluate[f[3][x] /. sol] /. x -> i}, {i, 10, 20}]
datascalar = 
 Table[{i, Evaluate[v[3][x] /. sol] /. x -> i}, {i, 10, 20}]

Mathematica graphics

Note: Thanks to comment below. You actually do not Evaluate in this case. So you just do

datapotential = Table[{i, f[3][x] /. sol /. x -> i}, {i, 10, 20}]

datascalar = Table[{i, v[3][x] /. sol /. x -> i}, {i, 10, 20}]
$\endgroup$
3
  • 2
    $\begingroup$ Might be worth noting that the Evaluate isn't doing anything here, it could just be removed $\endgroup$
    – Lukas Lang
    May 16, 2022 at 6:12
  • $\begingroup$ @LukasLang You are right. It is not needed here. I guess help on NDSolve uses Evaluate all over the place, and so it becomes just a habit to add it whenever one uses NDSolve. I'll remove it. $\endgroup$
    – Nasser
    May 16, 2022 at 6:29
  • $\begingroup$ Thank you so much! This was really helpful and solved my problem. $\endgroup$ May 16, 2022 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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