1
$\begingroup$

I am trying to solve a differential equation numerically. The code is

H1[z_] = (3 (0.3) (1 + z)^(4) + 
  Exp[a1 u[z]]) (3 - (1/2) (1 + z)^2 (u'[z])^2 )^(-1)
s1 = ParametricNDSolve[{(H1[z])^2 (1 + z)^2 u''[z] - 
   3 (H1[z])^2 (1 + z) u'[z] + a1 Exp[(a1 u[z])] == 0, u'[0] == 1, 
 u[0] == 1}, u, {z, 0, 5}, {a}]

It doesn't plot and evaluate u[z]. I want to plot u[z] for different values of the parameter a1.

$\endgroup$
2
  • $\begingroup$ Please post a1. Or Block[{a1 = 8, a = 2}, Plot[u[a][z] /. s1, {z, 0, 5}]] $\endgroup$
    – cvgmt
    Commented Apr 3 at 8:31
  • $\begingroup$ Hi, seems not to work. $\endgroup$
    – yousee
    Commented Apr 3 at 13:52

2 Answers 2

3
$\begingroup$

First, the parameter in ParametricNDSolve is called a1, not a. Then, ParametricNDSolve returns a rule not a function.

To plot the solutions for different a1, you may write:

H1[z_] = (3  (0.3)  (1 + z)^(4) + 
     Exp[a1  u[z]])  (3 - (1/2)  (1 + z)^2  (u'[z])^2)^(-1);
sol =  u /. 
  ParametricNDSolve[{(H1[z])^2  (1 + z)^2  u''[z] - 
      3  (H1[z])^2  (1 + z)  u'[z] + a1  Exp[(a1  u[z])] == 0, 
    u'[0] == 1, u[0] == 1}, u, {z, 0, 5}, {a1}]

and then e.g.:

funs = Table[so[i][x], {i, 3}];
Plot[funs, {x, 0, 3}]

enter image description here

$\endgroup$
2
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

H1[z_] = (3   (0.3)   (1 + z)^(4) + 
     Exp[a1   u[z]])   (3 - (1/2)   (1 + z)^2   (u'[z])^2)^(-1);

sol = ParametricNDSolveValue[{(H1[z])^2   (1 + z)^2   u''[z] - 
     3   (H1[z])^2   (1 + z)   u'[z] + a1   Exp[(a1   u[z])] == 0, u'[0] == 1,
    u[0] == 1}, u, {z, 0, 5}, a1]

enter image description here

If sol is used directly in a plot, use Evaluate

Manipulate[
 plt[
  Evaluate[sol[#][z] & /@ {3, 1.5, 1}],
  {z, 0, 5},
  AxesLabel -> (Style[#, 14] & /@ {z, u}),
  PlotLegends ->
   Placed[{3, 1.5, 1}, {.3, .8}]],
 {{plt, Plot, "plot type"}, {Plot, LogPlot}}]

enter image description here

$\endgroup$

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.