0
$\begingroup$
arec = 1/1100;
aeq = 1/(2.4*10^4*0.13);
alpha = (arec/aeq)^1/2;
y = (alpha*x)^2 + 2*alpha*x;
yb = y*(0.02/0.13);
yc = y*(0.11/0.13);
xi = 10^-3;
xrec = (((alpha^2 + 1)^1/2) - 1)/alpha;
n = 2*alpha*(alpha*x + 1)/((alpha*x)^2 + 2*alpha*x);
n0 = 2*alpha*(alpha*xi + 1)/((alpha*xi)^2 + 2*alpha*xi);

c = 
  ParametricNDSolve[
    {phi'[x] == -n*phi[x] + ((3*n^2/2*k)*(vg[x] (4/3 + y + yc) + vc[x]*yc[x])/(1 + y)), 
     dc'[x] == -k*vc[x] + 3*phi'[x], vc'[x] == -n*vc[x] + k*phi[x], 
     dg'[x] == -(3/4)*k*vg[x] + 4*phi'[x], 
     vg'[x] == ((1 + (3/4)*yb)^-1)*(-(3/4)*yb*n*vg[x] + (1/4)*k*dg[x]) +k*phi[x], 
     phi[xi] == 1, 
     dc[xi] == (3/4)*dg[xi], 
     vc[xi] == vg[xi], 
     dg[xi] == -2*phi[xi], 
     vg[xi] == -(1/4)*(k/n0)*dg[xi]}, 
    {phi, dc, vc, dg, vg}, {x, xi, xrec}, {k}]
Plot[Evaluate[Table[phi[k][x] /. c, {k, 0.01, 1}]], {x, xi, xrec}]

When I run this code, the NDSolve part gives a parametric function, and it seems OK, but when I plot, I get the following error:

ParametricNDSolve::ndnum: Encountered non-numerical value for a derivative at x == 0.001`.

What is the problem with my code? Is there anyway to fix it?

$\endgroup$
1
  • 1
    $\begingroup$ I think your initial condition are bad. The reason the problem doesn't show up until you try to plot is because ParametricNDSolveValue doesn't do any solving, it just set things up for NDSolve to solve when a numerical value for the parameter has been given. $\endgroup$
    – m_goldberg
    Mar 30 '19 at 22:05
3
$\begingroup$

After correcting a couple of typos, the code works.

arec = 1/1100;
aeq = 1/(2.4*10^4*0.13);
alpha = (arec/aeq)^1/2;
y = (alpha*x)^2 + 2*alpha*x;
yb = y*(0.02/0.13);
yc = y*(0.11/0.13);
xi = 10^-3;
xrec = (((alpha^2 + 1)^1/2) - 1)/alpha;
n = 2*alpha*(alpha*x + 1)/((alpha*x)^2 + 2*alpha*x);
n0 = 2*alpha*(alpha*xi + 1)/((alpha*xi)^2 + 2*alpha*xi);

c = ParametricNDSolve[{phi'[
     x] == -n*
      phi[x] + ((3*n^2/2*
         k)*(vg[x] (4/3 + y + yc) + vc[x]*yc)/(1 + y)), 
   dc'[x] == -k*vc[x] + 3*phi'[x], vc'[x] == -n*vc[x] + k*phi[x], 
   dg'[x] == -(3/4)*k*vg[x] + 4*phi'[x], 
   vg'[x] == ((1 + (3/4)*yb)^-1)*(-(3/4)*yb*n*vg[x] + (1/4)*k*dg[x]) +
      k*phi[x], phi[xi] == 1, dc[xi] == (3/4)*dg[xi], 
   vc[xi] == vg[xi], dg[xi] == -2*phi[xi], 
   vg[xi] == -(1/4)*(k/n0)*dg[xi]}, {phi, dc, vc, dg, vg}, {x, xi, 
   xrec}, {k}];
Plot[Evaluate[Table[phi[k][x] /. c, {k, 0.01, 1, .3}]], {x, xi, xrec},
  PlotLegends -> Automatic, AxesLabel -> {"x", "phi"}]

fig1

$\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.