0
$\begingroup$

I am solving a system consisted of 3 ODEs using ParametricNDSolveValue. Everytime when I want to obtain each solution at certain parameters, I would use:

solutions = ParametricNDSolveValue[{ODEs}, {a, b, phi}, {x, 0, 1}, {TM, miumaxA, miumaxB}];
asol[x_]=solutions[para1val,para2val,para3val][[1]][x];
bsol[x_]=solutions[para1val,para2val,para3val][[2]][x];
phisol[x_]=solutions[para1val,para2val,para3val][[3]][x];

However, this might waste computational resource, as the solution is computed 3 times, in each line. Then, I try :

{asol,bsol,phisol}=solutions[para1val,para2val,para3val][x];

However, this does not work. solutions[para1val,para2val,para3val][x] would give a list of three interpolating functions. Is there any way to extract interpolating function of each solution once solutions[para1val,para2val,para3val][x] is evaluated?

$\endgroup$
5
  • $\begingroup$ It would be better to provide code that is self-contained. As it stands, everyone has to invent their own ode to run your code. In any event, try dropping [x] and write {asol,bsol,phisol} = solutions[para1val,para2val,para3val] instead, does that do it? For example, try evaluating asol[0.5] afterwards. $\endgroup$
    – user293787
    Commented Jul 9, 2022 at 11:07
  • $\begingroup$ Try to write asol[x_]:=solutions... instead of asol[x_]=solutions.... You may also define sol[x_, n_Integer]:=solutions[para1val,para2val,para3val][[n]][x], which yields you the solution definition for all values n=1,2,3. In principle, this makes what you want: the function definition persisting through the whole Mma session. $\endgroup$ Commented Jul 9, 2022 at 12:31
  • $\begingroup$ @user293787 The complete code is available in the link provided in the question. $\endgroup$
    – Johnson
    Commented Jul 9, 2022 at 12:53
  • 1
    $\begingroup$ @user293787 By dropping [x] , I am able to get asol[x], bsol[x], and phisol[x] as separated interpolating function. Your help is very much appreciated. @AlexeiBoulbitch By changing to :=, I would get error message "dSetDelayed::shape". $\endgroup$
    – Johnson
    Commented Jul 9, 2022 at 13:09
  • 3
    $\begingroup$ ParametricNDSolveValue caches results by default, so the ODEs won't be repeatedly solved and re-solved. It was designed with your sort of use-case in mind. $\endgroup$
    – Michael E2
    Commented Jul 9, 2022 at 17:07

1 Answer 1

3
$\begingroup$

Turning the comment to an answer:

ParametricNDSolveValue caches results by default, so the ODEs won't be repeatedly solved and re-solved. It was designed with your sort of use-case in mind.

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