5
$\begingroup$

This question already has an answer here:

I try to plot a function for special values of some parameters which I don't want to set globally:

a[t_] = Cos[ω0*t + α]*Sinc[Δω*t]

Module[ {
   ω0 = 1, 
   Δω = 0.3, 
   α = Pi/4
}, 
 Plot[a[t], {t, -25, 25}, 
   AxesLabel -> {"\!\(\*SubscriptBox[\(ω\), \(0\)]\)t", "a(t)"}
 ]
]

This only gives me an empty diagram. If I however replace a[t] by its definition from above it works just fine. In this case replacing the name of the function by the definition may not be a problem, but I also want to do this with functions where the definitions are very long, so I'd really like to use their names instead. What do I do wrong here?

If this is not possible with module, I'd also be glad about ways to do this with other constructs!

$\endgroup$

marked as duplicate by MarcoB, m_goldberg, Bob Hanlon, Edmund, Mr.Wizard Jul 27 '16 at 1:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Why not put the definition for a within Module[], then? (Or define a to be a multiparameter function.) $\endgroup$ – J. M. will be back soon Jul 26 '16 at 11:38
  • 2
    $\begingroup$ This is exactly what Module should do, and what you need is Block. $\endgroup$ – Kuba Jul 26 '16 at 11:38
5
$\begingroup$

Edit

Given that a is defined at top-level with free variables

a[t_] := Cos[ω0*t + α]*Sinc[Δω*t]

the behavior of a[t] in Plot or anywhere else is a matter of which scope the free variables of a are evaluated in.

Module[{ω0 = 1, Δω = 0.3, α = Pi/4}, a[1]]

gives

Cos[α + ω0] Sinc[Δω]

because the local variables specified in Module's 1st argument are renamed internally to avoid conflict with the names used for the free variables. Module is a lexical scoping construct so that is kind of behavior one should expect. However, you can override this; you can get the free variables renamed by writing

 Module[{ω0 = 1, Δω = 0.3, α = Pi/4}, Evaluate[a[1]]]

-0.209778

because this is equivalent to writing

Module[{ω0 = 1, Δω = 0.3, α = Pi/4}, Cos[ω0*1 + α]*Sinc[Δω*1]]

Block as Kuba has pointed it out is better designed for this sort of work because it is a dynamic scoping construct; it rebinds the free variables to local values for, and only for, the dynamic extent of the evaluation of the block.

Thus,

Block[{ω0 = 1, Δω = 0.3, α = Pi/4}, a[1]]

-0.209778

So, if you want to stay with Module, write

Plot[Module[{ω0 = 1, Δω = 0.3, α = Pi/4}, Evaluate[a[t]]], {t, -25, 25}, 
  AxesLabel -> {"\!\(\*SubscriptBox[\(ω\), \(0\)]\)t", "a(t)"}]

However, Block will give simpler code.

Plot[Block[{ω0 = 1, Δω = 0.3, α = Pi/4}, a[t]], {t, -25, 25}, 
  AxesLabel -> {"\!\(\*SubscriptBox[\(ω\), \(0\)]\)t", "a(t)"}]

Block[{ω0 = 1, Δω = 0.3, α = Pi/4},
  Plot[a[t], {t, -25, 25}, 
    AxesLabel -> {"\!\(\*SubscriptBox[\(ω\), \(0\)]\)t", "a(t)"}]]

Note that Block is less fussy about its placement because it scopes dynamically rather than lexically.

You could also write

Plot[a[t] /. {ω0 -> 1, Δω -> 0.3, α -> Pi/4}, {t, -25, 25}, 
  AxesLabel -> {"\!\(\*SubscriptBox[\(ω\), \(0\)]\)t", "a(t)"}]

which makes an end-run around the scoping issues because it replaces the free variables with values without introducing any new scoping.

All of these plot expressions produce

plot

$\endgroup$
  • $\begingroup$ @Kuba. I have reconsidered and expanded my answer. I hope I have got the scoping issues right and explained them clearly in the edit I have made. $\endgroup$ – m_goldberg Jul 26 '16 at 23:24
4
$\begingroup$

Another possibility is to create a second definition for a that is:

a[t_, ω0_, Δω_, α_] := Cos[ω0*t + α]*Sinc[Δω*t]

Then replace a[t] with a[t, ω0, Δω, α] in your Module

Module[
 {
  ω0 = 1,
  Δω = 0.3,
  α = Pi/4
  },
 Plot[a[t, ω0, Δω, α], {t, -25, 25},
  AxesLabel -> {"\!\(\*SubscriptBox[\(ω\), \(0\)]\)t", "a(t)"}]
 ]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Okay, you need define function a[t_] with one variable t ahead.

a[t_]:=Module[{ω0 = 1, Δω = 0.3,    α = Pi/4},
    Cos[ω0*t + α]*Sinc[Δω*t] (* return value *)
]

ps: Sinc unit is radians.

Plot[a[t], {t, -25, 25}, AxesLabel ->           
{"\!\(\*SubscriptBox[\(ω\),     \(0\)]\)t", "a(t)"}]

enter image description here

$\endgroup$

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