0
$\begingroup$

When solving the following system:

g[a_,b_] := c /. FindRoot[a + b == c, {c, 0}]

pf = 
  ParametricNDSolveValue[{y''[x] == g[a,b] y[x] Cos[x + y[x]], 
    y[0] == a, y'[0] == 1}, Integrate[y[s]^2, {s, 0., b}], {x, 0, b}, {a, b}];

Mathematica gives me two error messages, which say:

FindRoot::nlnum: The function value {0. +a+b}is not a list of numbers with dimensions {1} at {c} = {0.}. >>

ReplaceAll::reps: {FindRoot[a+b==c,{c,0}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

How can I fix this issue?

$\endgroup$
3
$\begingroup$

You'd need to preempt symbolic evaluation:

g[a_?NumericQ, b_?NumericQ] := c /. FindRoot[a + b == c, {c, 0}]

pf = ParametricNDSolveValue[{y''[x] == g[a, b] y[x] Cos[x + y[x]], 
   y[0] == a, y'[0] == 1}, 
  Integrate[y[s]^2, {s, 0., b}], {x, 0, b}, {a, b}]
$\endgroup$
  • $\begingroup$ it works, thanks a lot. But I do not understand why g[a_?NumericQ, b_?NumericQ] works $\endgroup$ – 3c. Jul 24 '13 at 16:33
  • $\begingroup$ @user8583, when the command evaluates, before it gets to ParametricNDSolve it evaluates g[a,b] which you do not want. Try your ParametricNDSolveValue[...]//Trace to see what happens. $\endgroup$ – user21 Jul 24 '13 at 16:36
  • $\begingroup$ I get it. thanks $\endgroup$ – 3c. Jul 24 '13 at 17:39

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.