2
$\begingroup$

I am very confused with how the Plot command evaluates arguments for vector valued numerical functions. For example, when I run the following commands

Clear[f]
f = Interpolation[{{0, {0, 0}}, {1, {1, 2}}}]
Plot[f[t][[2]], {t, 0, 1}]

I get the error: Part::partw: Part 2 of InterpolatingFunction[{{0,1}},{5,3,0,{2},{2},0,0,0,0,Automatic,{},{},False},{{0,1}},{{{0,0}},{{1,2}}},{Automatic}][t] does not exist..

It seems like the Plot command is trying to evaluate U[t][[2]] at some point instead of only calling U for specific numeric values. How can I force Plot to only plug evaluate U with actual numbers.

Thanks!

$\endgroup$
1
  • $\begingroup$ Plot[Evaluate[f[t]][[2]], {t, 0, 1}]? $\endgroup$ – kglr Sep 7 '16 at 0:35
2
$\begingroup$

One way is to Indexed instead of Part:

Clear[fff]
fff = Interpolation[{{0, {0, 0}}, {1, {1, 2}}, {2, {3, 2}}, {3, {1, 4}}}];
GraphicsRow@{
  Plot[Indexed[fff[t], 1], {t, 0, 1}], 
  Plot[Indexed[fff[t], 2], {t, 0, 1}]}

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks this is great! Indexed is basically exactly what I wanted: a delayed form of Part. If only it weren't so hard to find these things in the documentation! $\endgroup$ – nbren12 Sep 13 '16 at 3:34
2
$\begingroup$

I still want to see what other answers, but I have found an inelegant solution:

Clear[f, ff]
f = Interpolation[{{0, {0, 0}}, {1, {1, 2}}}]
ff[x_?NumericQ, i_] := f[x][[i]]
Plot[Table[ff[t, i], {i, 1, 2}], {t, 0, 1}, Evaluated -> True]

The key was to make ensure that the placeholder function ff[t,i] is not evaluated when t is anything but a number. I don't like having to define an extra pattern rule, and I hope this can be done using some combination of Hold, Defer, Evaluate, etc.

$\endgroup$
1
  • $\begingroup$ This is, in fact, a typical solution, explained here. See "Plotting" at the end of the answer. $\endgroup$ – Michael E2 Sep 7 '16 at 0:51

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.