# Replace symbol with numerical value

How do I get the same result, i.e., how can I get Mathematica to replace a with 1?

b = a;

s1[a_?NumericQ] := Sin[a]

s2[a_?NumericQ] := Sin[b]

s1[1.]

s2[1.]


I appreciate any help !

The nearest real problem I want to esolver is as follows:

b = a;

sua[a_?NumericQ] := NDSolveValue[{
D[u[x, t], t] + D[u[x, t], x] == -b u[x, t],

u[x, 0] == 1,

u[0, t] == 1},

u,

{x, 0, 1},

{t, 0, 5}]

sua[1][1, 5]


I would like a solution without the replacement /. b -> a?

• s2[a_?NumericQ] := Sin[b] /. b -> a or just s2[a_?NumericQ] := Sin[a], can you explain what you need more? Aug 22 '15 at 20:09
• I do not want sibstituir directly "b" for "a". Thank you! Aug 22 '15 at 20:38

The following will do what you ask, but I really can't think how it can be useful in any practical situation.

a = 42.;
b := a;  (* note use of SetDelayed *)
s1[a_?NumericQ] := Sin[a]
s2[a_?NumericQ] := Sin[b]


Now b tracks a and has value 42. at top-level. Therefore, for any numerical argument given s2, say π,

Sin[42.] == s2[π]

True


To make s2 evaluate to value other than Sin[42.], a must be given a value different from 42. I will do this in a dynamically-scoped block, preventing b from being disturbed at top-level.

Table[{s1[u], Block[{a = u}, s2[π]]}, {u, .1, .3, .1}]

{{0.0998334, 0.0998334}, {0.198669, 0.198669}, {0.29552, 0.29552}}


b is, of course, unchanged at top-level.

b

42.


However, consider that

s3[___] := Sin[b]


has the same behavior as s2 as is shown by

Table[{s1[u], Block[{a = u}, s3[]]}, {u, .1, .3, .1}]

{{0.0998334, 0.0998334}, {0.198669, 0.198669}, {0.29552, 0.29552}}


so I don't see why you want to define something like s2.

has