A month ago, I used Mathematica to solve my experiment of the Test Technology of Mechanics Engineering. The exeriment is mainly about the Fourier Transform and verify the validity of the theory of Fourier. My code is below:

 originwave[t_] := 
  If[-5 <= t <= 5, t, If[t > 0, originwave[t - 10], originwave[t + 10]]];
(*the definition of SawtoothWave function*)

four periodic originwave

      {Plot[originwave[t], {t, -20, 20}, PlotRange -> {-5, 5}, 
           AspectRatio -> 1, ImageSize -> 300, AxesStyle -> Arrowheads[0.03], 
           AxesLabel -> {"t", "four periodic originwave"}

four periodic originwave

The seven order Fourier series expansion

      {Plot[Evaluate[FourierTrigSeries[originwave[t], t, 7]], {t, -20, 20},
           PlotRange -> {-5, 5}, AspectRatio -> 1, ImageSize -> 300, 
           AxesStyle -> Arrowheads[0.03], 
           AxesLabel -> {"t", "The seven order Fourier series expansion"}]},

The seven order Fourier series expansion

Furthermore,I encounter a problem about Taylor Series that we can use the approximate value of Maclaurin Series to replace a function. I can see the degree of closeness between them by changing the variable n. That's my trial:

              Plot[E^x, {x, 0, 5}, PlotStyle -> Pink],
              Plot[Evaluate@Normal@Series[E^x, {x, 0, n}], {x, 0, 5}, 
                   PlotRange -> {0, 150}]],
         {{n, 6}, 0, 10, 1, Appearance -> "Labeled"}]

enter image description here

Lastly ,the problem of plotting 2-D graph of |x|+|2x+y|=1, below is the solution:

Plot[y /. Solve[Abs@x + Abs@(x + y) == 1, y, Reals] // 
     Evaluate, {x, -2, 2}, AspectRatio -> Automatic]

enter image description here

Summary and my confusion:

In three tasks, I am alway using the Evaluate command. Firstly,I know this command and I will remember it when the MMA cannot give the result that I want. However, it is confusing to me that I don't know when to use it or not use it. Can someone help me?


[Too long for comment]

Here's a simple example of what is going on:

f is a slow to evaluate function:

f[x_] := (Pause[0.01]; x^2)
AbsoluteTiming[Plot[f[x], {x, 0, 1}];]            (* 1.6s *)
AbsoluteTiming[Plot[Evaluate[f[x]], {x, 0, 1}];]  (* 0.012s *)

The speed comes from calculating the symbolic result and substituting values into that, since as far as Plot is concerned the second version is the same as Plot[x^2, ...]. In the first case it will call f[x] for each point. Without to the HoldAll attribute Plot would not even have been aware of f[x]

In your case with FourierTrigSeries[originwave[t], t, 7] without Evaluate Plot would replace all occurances of t with numerical values doing things like:

FourierTrigSeries[originwave[0.3], 0.3, 7]
(* FourierTrigSeries[0.3, 0.3, 7] *)

Leading to expressions like above that don't make any sense. When you wrap it in Evaluate Plot will instead see the evaluated form of the original expression:

FourierTrigSeries[originwave[t], t, 7]
(* 2 Sin[t] - Sin[2 t] + 2/3 Sin[3 t] - 1/2 Sin[4 t] +
   2/5 Sin[5 t] - 1/3 Sin[6 t] + 2/7 Sin[7 t] *)

It is all about the attributes of the function you are considering. In particular it is about Hold-Attributes. In your case the function Plot has attribute HoldAll. You can see this by using the function Attributes:


{HoldAll, Protected, ReadProtected}

Evaluate is then used to evaluate the symbolic expression first (nullifying the HoldAll-Attribute) and afterwards evaluating the obtained expression at certain numeric values for the plotting-variable x. If you do not use Evaluate then x is first replaced by a numerical value and then the expression is evaluated. This is done for every plotpoint.


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.