1
$\begingroup$

I'm trying to use ParametricNDSolveValue for a system of equations. I want to have it create a function that takes in a single input and outputs a list. Here is my code thus far.

f[x_, y_] := y;
g[x_, y_] := -0.2 y - 9 Sin[x];

sol = ParametricNDSolveValue[{
x'[t] == f[x[t], y[t]],
y'[t] == g[x[t], y[t]],
x[0] ==  x0,
y[0] ==  y0},
{x, y},
{t, 0, tmax},
{x0, y0}];

From here, I'd like to create functions in terms of time, that outputs a list like so:

sol[x0, y0][t]
={1.234, 5.678}

However, the way I currently have the code structured, I need to do this:

sol[x0, y0][[1]][t]
=1.234
sol[x0, y0][[2]][t]
=5.678

I would like the sol[x0, y0] function output the list of numbers instead of having to run through each function inside of sol[x0, y0]. How can I reformat my code so that I can accomplish this?

$\endgroup$
1
$\begingroup$
tmax = 5;
sol = ParametricNDSolveValue[{x'[t] == f[x[t], y[t]], y'[t] == g[x[t], y[t]], 
  x[0] == x0, y[0] == y0}, {x, y}, {t, 0,  tmax}, {x0, y0}];

You can use Through:

Through @ sol[1, 2][4]

{-0.40324, 2.14714}

Or make function out of sol using Through:

ClearAll[soln]
soln[p0_, p1_][t_] := Through[sol[p0, p1]@t]
soln[1, 2][4]

{-0.40324, 2.14714}

Better yet, you can make tmax a parameter:

ClearAll[sol2, soln2, tmax]
sol2 = ParametricNDSolveValue[{x'[t] == f[x[t], y[t]], y'[t] == g[x[t], y[t]], 
  x[0] == x0, y[0] == y0}, {x, y}, {t, 0, tmax}, {x0, y0, tmax}]

soln2[p0_, p1_, tmax_][t_] := Through[sol2[p0, p1, tmax]@t]
soln2[1, 2, 5][4]

{-0.40324, 2.14714}

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