1
$\begingroup$

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?

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

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

$\endgroup$

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.