3
$\begingroup$

I have two time- and space-dependent vector fields, one which is:

$\mathbf A(x,y,t) = -y \, t \, \hat{\mathbf x} + x \, t \, \hat{\mathbf y}, \tag 1$

and another $\mathbf B(x,y,t)$ which is $\mathbf A$ translated $3/2$ units in the positive $\hat{\mathbf x}$ direction and $3/2$ units in the positive $\hat{\mathbf y}$ direction, so:

$\mathbf B(x,y,t) = - \left( y - \dfrac{3}{2} \right) \, t \, \hat{\mathbf x} + \left( x - \dfrac{3}{2} \right) \, t \, \hat{\mathbf y}. \tag 2$

I want to plot $\mathbf B$ evaluated at $t = 1$ in Mathematica. I tried two different ways but only one worked. The first one is directly from the expression (2):

$\mathbf B(x,y,1) = - \left( y - \dfrac{3}{2} \right) \, \hat{\mathbf x} + \left( x - \dfrac{3}{2} \right) \, \hat{\mathbf y}. \tag 3$

The resulting plot correctly shows the translation in both axes:

StreamPlot[{3/2 - y, -(3/2) + x}, {x, -5, 5}, {y, -5, 5}]

Figure 1

The second way I plot $\mathbf B(x,y,1)$ is by first defining $\mathbf A$ and then translating it. However, the resulting plot only shows $\mathbf A$ translated in the $\hat{\mathbf x}$ direction, even though B[1] gives the same expression as (3):

Clear[A, B];
A[t_] := {-y*t, x*t};
B[t_] := ReplaceAll[ReplaceAll[A[t], x -> x - 3/2], y -> y - 3/2];
B[1]
StreamPlot[B[1], {x, -5, 5}, {y, -5, 5}]

Figure 2

Note that if you use the function VectorPlot instead of StreamPlot, the error is still present in the second method.

Why the second method didn't work? I want to fix it because I'm also using Mathematica to automatically translate the field $\mathbf A$.

Given that B[1] is the same expression as (3), I think the problem is related with user-defining B.

$\endgroup$
1
  • 3
    $\begingroup$ replace B[1] with Evaluate@B[1]? $\endgroup$
    – kglr
    Commented Sep 7, 2021 at 20:54

1 Answer 1

5
$\begingroup$

The simplest solution is to use StreamPlot[Evaluate[B[1]],...].

At each point the plot is effectively evaluating

Block[{x = x0, y = y0}, B[1]]

to determine what the vector is. Suppose $x_0=1$ and $y_0=2$, then the evaluation of B[1] at that point amounts to:

In[34]:= ReplaceAll[ReplaceAll[{-2*1, 1*1}, 1 -> 1 - 3/2], 2 -> 2 - 3/2]

Out[34]= {-2, -1/2}

instead of the expected {-1/2, -1/2}. Using Evaluate forces the symbolic calculations to be computed before evaluating at numeric coordinates.

$\endgroup$
1
  • $\begingroup$ Thanks for explaining why using Evaluate solves the problem! The Wiki has very few examples. $\endgroup$
    – alejnavab
    Commented Sep 7, 2021 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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