0
$\begingroup$

Situation

I have define a function in Mathematica like this:

phi[t_] := Subscript[ϕ, m] Cos[(2 π)/T t]

Then I specify some parameters in this function with a Rule:

flapDragon[t] = phi[t] /. {Subscript[ϕ, m] -> 1.204, T -> 0.0278}

Which output 1.204 Cos[226.014 t].

Problem

However when I plot the function flapDragon[t] using:

Plot[flapDragon[t], {t, 0, 0.02}] 

It plot nothing, but with:

Plot[Evaluate@flapDragon[t], {t, 0, 0.02}]

it can plot something.

Question

I wonder why I have to use Evaluate before flapDragon[t] with Plot function.

$\endgroup$
3
  • $\begingroup$ When you define flapDragon[t] = phi[t] /. etc you need to include an underscore after the t. i.e. You need flapDragon[t_]=phi[t] /. etc. Without the underscore you have only defined flapDragon for the specific symbol t, not anything that might take the place of t. $\endgroup$
    – Ymareth
    May 25, 2016 at 9:42
  • $\begingroup$ You've not defined the flapDragon function correctly, use fD[t_]:=... Also, avoid using sub and superscript variables. See mathematica.stackexchange.com/questions/18393/… $\endgroup$ May 25, 2016 at 9:42
  • $\begingroup$ I still wonder why Evaluate works ? $\endgroup$
    – PureLine
    May 25, 2016 at 9:53

1 Answer 1

1
$\begingroup$

As others pointed out, you have not defined flapDragon as a function of a variable t, you've defined it as a symbol with a single DownValue for the symbol t.

Notice that this does not work,

Plot[Evaluate@flapDragon[tt], {tt, 0, 0.02}]

Mathematica graphics

That's because flapDragon[tt] is not defined.

I still wonder why Evaluate works

The reason it works is that Plot uses Block to localize its variables. This means that the variable t is not fully localized like it is with Module. Compare these

Block[{t},
 flapDragon[t]
 ]
Module[{t},
 flapDragon[t]
 ]
(* 1.204 Cos[226.014 t] *)
(* flapDragon[t$502358] *)

So in this case, it works because you use the exact same variable when you defined flapDragon as when you plotted it. You can avoid this confusion by using a pattern to define your function flapDragon[t_] = .... or flapDragon[t_] := .... (both work in this situation)

$\endgroup$
4
  • $\begingroup$ En, I still could figure out the reason why Plot[flapDragon[t], {t, 0, 0.02}] don't works but Plot[Evaluate@flapDragon[t], {t, 0, 0.02}] works from your answer. $\endgroup$
    – PureLine
    May 25, 2016 at 14:56
  • $\begingroup$ But, The reason why Plot[Evaluate@flapDragon[t], {t, 0, 0.02}] works may be not because "Plot uses Block to localize its variables" as you figure out. "Plot has Attributes HoldAll, so one possibility to get what you expect is to do just SetOptions[Plot, Evaluated -> True];" Ref. mathematica.stackexchange.com/questions/19453/… $\endgroup$
    – PureLine
    May 25, 2016 at 15:01
  • $\begingroup$ That isn't the interesting question at hand, the question is why Plot[Evaluate@flapDragon[t], {t, 0, 0.02}] works at all, when flapDragon wasn't even defined as a function. Of course you need Evaluate when doing Plot, do a search and you'll find many posts about it. The reason it works in this case is most certainly a fluke related to what I wrote here. $\endgroup$
    – Jason B.
    May 25, 2016 at 15:03
  • $\begingroup$ thanks. it seems you understand my question better than I. I didn't make the essential of my problem clear. I wish to keep my original question as it used to be, for the purpose to remind me of detailed problem formulation $\endgroup$
    – PureLine
    May 26, 2016 at 6:39

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