# How to properly substitute values into variables?

I have an array of functions in terms of a[0] through a[n] and I wish to replace the a[]'s with previously calculated values p[0] 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[[1]] = p[[1]]/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[0] replaced with the number contained in p[0], a[1] replaced with p[1], etc.

-
perhaps just q3 = s / a :> p –  belisarius Jan 24 '14 at 17:49
@Belisarius I believe you have missed the point :-))) –  Peltio Jan 24 '14 at 18:19
@Peltio The point was left as an exercise to the reader .) –  belisarius Jan 24 '14 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[1][a[1]], f[2][a[1],a[2]]}


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[1], a[2]} -> p]

-
That works perfectly! Thanks! –  gKirkland Jan 26 '14 at 20:48