0
$\begingroup$

Consider the following MWE:

Ps = {P1, P2, P3}
f[betas_] := betas.betas

(*A) works as expected:*)
Plot[f[{P1, P2, P3}] /. {P1 -> 5, P2 -> 10, P3 -> 15}, {P1, 0, 10}]
Plot[f[{P1, P2, P3}] /. {P1 -> 5, P2 -> 10, P3 -> 15}, {P2, 0, 10}]
Plot[f[{P1, P2, P3}] /. {P1 -> 5, P2 -> 10, P3 -> 15}, {P3, 0, 10}]

(*B) does not work:*)
Table[Show[
  Plot[f[Ps] /. {P1 -> 5, P2 -> 10, P3 -> 15}, {Ps[[k]], 0, 10}]], {k,
   Length[Ps]}]

I try to get the three graphs in A) by looping through Ps in B), but can't get it running.

$\endgroup$
  • $\begingroup$ Essentially a duplicate. $\endgroup$ – Alan Mar 29 '18 at 20:37
3
$\begingroup$

The error indicates that Ps[[k]] cannot be used as an iterator. A workaround:

Table[With[{p = Ps[[k]]}, 
  Plot[f[Ps] /. {P1 -> 5, P2 -> 10, P3 -> 15}, {p, 0, 10}]], {k, Length[Ps]}]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Uh, did you redefine Pi? While this is perfectly valid, I have my doubts wether we should teach that as good programming practice ;) $\endgroup$ – Henrik Schumacher Mar 29 '18 at 22:32
  • 2
    $\begingroup$ @HenrikSchumacher Oh, I hadn't even realized! Thanks for pointing that out :) That's why I don't usually don't use upper-case names.. $\endgroup$ – anderstood Mar 29 '18 at 23:04

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.