How can I replace a variable with a list of numbers?


Suppose I have the following list of values

V = Table[i, {i, 1, 6.8, 0.19}];

and I want to replace v^2 in the expression PrC = (v^2 + 1)/(v^2 - 5) with the list of V values.


closed as off-topic by Anton Antonov, MarcoB, user9660, Bob Hanlon, Yves Klett May 17 '16 at 4:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Anton Antonov, MarcoB, Community, Bob Hanlon, Yves Klett
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ PrC = (v^2 + 1)/(v^2 - 5) /. v^2 -> V $\endgroup$ – Hubble07 May 14 '16 at 11:35
  • 1
    $\begingroup$ Your expression (v^2 + 1)/v^2 - 5) is missing a left parenthesis. Do you mean (v^2 + 1)/(v^2 - 5)? Also, you ask about replacing a variable, but your in your example you say you want to replace the expression v^2 with the numbers in a list, which is not the same thing. Which do you really mean? $\endgroup$ – m_goldberg May 14 '16 at 13:46
  • $\begingroup$ YES its (v^2 + 1)/(v^2 - 5) what i want to do is squared all the value of v from 1 to 6.8 $\endgroup$ – sara May 15 '16 at 10:44

There are many ways to accomplish what you want to do in Mathematica. This answer will discus just one -- defining prc as a function of v. Function is one of core concepts of Mathematica, so this approach has wide application beyond this specific case.

v = Table[i, {i, 1, 6.8, 0.19}];
prc[v_] := (v^2 + 1)/(v^2 - 5)

Then to get the values of prc over v just write

{-0.5, -0.674154, -0.938235, -1.36677, -2.15391, -4.01044, -13.2721, 14.9893, 
 5.44313, 3.55962, 2.75953, 2.31923, 2.04196, 1.85216, 1.71466, 1.61084, 1.52996, 
 1.46537, 1.41276, 1.36918, 1.33259, 1.30151, 1.27482, 1.25171, 1.23154, 1.21381, 
 1.19813, 1.18418, 1.17171, 1.16051, 1.15041}

That this works may surprise you. It works because prc is a the composition of purely numeric functions which have the function attribute called Listable, so it has the ability to thread over lists like v. I suggest you study [Listable](http://reference.wolfram.com/language/ref/Listable.html) (follow the link) because you will find it very useful to keep it mind.

Sometimes you will not want to take advantage of the Listable attribute, but the function prc will still be your friend. As an example, suppose you wanted to plot prc directly from the values of v. Then you could simply write

 ListPlot[Table[{i, prc[i]}, {i, v}]]

which gives



There is an alternative way of producing the plot, that I think is worth mentioning. This 2nd formulation is more purely functional, a little more concise, and maybe a little more efficient.

ListPlot[Transpose[{v, prc[v]}]]

It produces exactly the same plot as shown above.


Not the answer you're looking for? Browse other questions tagged or ask your own question.