# How to properly substitute values into variables?

I have an array of functions in terms of a through a[n] and I wish to replace the a[]'s with previously calculated values p through p[n], as in below:

p = Table[Chop[NIntegrate[f[t]*wt[t]*Tn[t], {t, 0, 1}]/(Pi/2)], {n, 0, k}];
p[] = p[]/2;

s = Table[Coefficient[pmult[j], tstar[i], 1], {j, 0, k}, {i, 1, k}]

q3=s/.p


What is the best way of doing this? I have tried the Replace[], ReplaceAll[], and /. commands but none seem to be giving me what I want. I just want a replaced with the number contained in p, a replaced with p, etc.

• perhaps just q3 = s / a :> p Jan 24, 2014 at 17:49
• @Belisarius I believe you have missed the point :-))) Jan 24, 2014 at 18:19
• @Peltio The point was left as an exercise to the reader .) Jan 24, 2014 at 18:25

To use /., you have to supply a replacement Rule rather than just a value. On the left hand side of the Rule (indicated by "->") you supply a pattern; on the right hand side you supply an expression to substitute for the pattern.

In your target vector s, each element depends on symbols like a[i]. For example,

s = {f[a], f[a,a]}


You want to replace every a[i] with the corresponding element of the list p, p[[i]]. You need a list of replacement Rules to get them all at once:

s /. Table[a[i] -> p[[i]], {i, 2}]


or

s /. Thread[{a, a} -> p]

• That works perfectly! Thanks! Jan 26, 2014 at 20:48