2

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!

nbren12
  • 191
  • 5

2 Answers2

2

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.

nbren12
  • 191
  • 5
2

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

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 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! – nbren12 Sep 13 '16 at 03:34