4
$\begingroup$

Suppose I have the given code:

usol[t_, x_]:= NDSolveValue[{D[u[t, x], x] == Sin[x], u[t, 0] == 0}, u[t, x], {x, 0, 10}]

and I need to find the value of usol[1,1]. For that I tried this method:

answer = Through@*{usol[1, x]}

answer[1]

Normally Through@* works for interpolating functions, then why is it not evaluating here? Or in other words how can I remove the [x] dependence from the interpolating function to make it work?

Furthermore, how can I make a table with x=1 and varying t from 0 to 5 using the interpolating function?

$\endgroup$

3 Answers 3

4
$\begingroup$

To answer your first question:

usol[t_, x_] :=
 NDSolveValue[{D[u[t, x], x] == Sin[x], u[t, 0] == 0}, u[t, x], {x, 0, 10}]

usol[1, x][[0]][1]

0.459698

usol[1, x][[0]][#] & /@ Range[0, 10, 0.1] // ListLinePlot

enter image description here

$\endgroup$
1
  • $\begingroup$ For the second part of my question, I will try to use your approach. Thanks! $\endgroup$
    – codebpr
    Commented Jan 17 at 12:52
3
$\begingroup$

That's not exactly what you're asking for, but why not just use ParametricNDSolveValue?

usol = ParametricNDSolveValue[{D[u[x], x] == t Sin[x], u[0] == 0}, u, {x, 0, 10}, {t}]

Your example doesn't really depend on t, so I modified it slightly to make a point.

usol[1][1]
(* 0.459698 *)
Table[usol[t][1], {t, 0, 5}]
(* {0., 0.459698, 0.919396, 1.37909, 1.83879, 2.29849} *)
$\endgroup$
2
$\begingroup$

usol[1,1] does not work because the second argument of usol is used, inside usol, as a variable. Therefore, you need to specify it as a variable. But then you can replace it by a value like e.g.:

usol[1, x] /. x -> 1

0.459698

"Through@{usol[1, x]}" does not make sense. "@" stands for composition. Therefore what you write in full form is: "Through[List[ usol[1, x] ]]". Now "Through" acts on the head of its argument, in this case "List". But as" List" is an atom, not composite, "Through" does nothing.

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