# A function defined  outside Manipulate does not produce Plot

**URL:** <https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301>\
**Category:** Questions\
**Tags:** mathematica, graphics-and-visualization, interface-construction\
**Created:** [May 31, 2014, 4:11pm UTC](https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301 "2014-05-31T16:11:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluigi74](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bluigi74](https://community.wolfram.com/u/bluigi74)\
**Post date:** [May 31, 2014, 4:11pm UTC](https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301/1 "2014-05-31T16:11:05Z")

</div>

Dear Users,

I really just started with Mathematica and I’m already stuck on something that looks pretty silly… I first do:

```auto
Manipulate[Plot[a (UnitStep[x] - UnitStep[x - b]), {x, -1, 10}, PlotStyle -> Thick], {a, 0.5, 1}, {b, 1, 8}]

```

And it works fine: I have a rectangular function with hight and width I can change. Now if I whant to define a function and then plot it I try with:

```auto
F[x_] := a (UnitStep[x] - UnitStep[x - b])

```

and then

```auto
Manipulate[Plot[F[x], {x, -1, 10}, PlotStyle -> Thick], {a, 0.5, 1}, {b, 1, 8}]

```

No lines appear in the plot. I tried to define the function with F[x\_,a\_,b\_] and all kinds of combinations but nothing works. I think I am doing something conceptually wrong, can somebody help?

Thanks very much in advance,  
L.

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [May 31, 2014, 4:39pm UTC](https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301/2 "2014-05-31T16:39:07Z")

</div>

Maybe

```auto
Manipulate[
 F[x_] := a (UnitStep[x - a] - UnitStep[x - b]);
 Plot[F[x], {x, -1, 10}, PlotStyle -> Thick], {a, 0.5, 1}, {b, 1, 8}
]

```

but I am just guessing because if I look closely at your post I see “a (UnitStep -…” and I’m assuming something got corrupted in that.

Sometimes helpful hint: If you are trying to post code then hitting Enter a few times and positioning the cursor in the middle of those  
blank lines and then clicking on the red “spikey ball” at the right end of the second row of buttons above the box you are typing in will  
create a thin little outlined box. Paste code inside that box and the forum software will do less mangling of code inside the box than  
if you put code in with the text of your message. Having more than one “spikey ball box” is more advanced and needs more instructions.

---

<div class="post-metadata">

**Author:** ![biancaeifert](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/biancaeifert/32/1245_2.png) [@biancaeifert](https://community.wolfram.com/u/biancaeifert)\
**Post date:** [June 1, 2014, 11:43am UTC](https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301/3 "2014-06-01T11:43:54Z")

</div>

Hi Luigi,

there’s actually a simple reason why your code doesn’t work: when you plot a function, you have to specify its arguments. Which means that this is wrong:

```auto
Plot[F,{x,0,1}]

```

…and here’s the correct version:

```auto
Plot[F[x],{x,0,1}]

```

The same goes for the UnitStep function in your first example, which also misses its argument and therefore won’t work.

As for your specific problem, try this:

```auto
F[x_, a_, b_] := a (UnitStep[x] - UnitStep[x - b]);
Manipulate[Plot[F[x, a, b], {x, -1, 10}, PlotStyle -> Thick], {a, .5, 1}, {b, 1, 8}]

```

~ Bianca

---

<div class="post-metadata">

**Author:** ![biancaeifert](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/biancaeifert/32/1245_2.png) [@biancaeifert](https://community.wolfram.com/u/biancaeifert)\
**Post date:** [June 1, 2014, 11:46am UTC](https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301/4 "2014-06-01T11:46:09Z")

</div>

Hi Bill,

that works, but keeps re-evaluating the function definition of F all the time even when a and b don’t change. The following trick avoids that, in case the OP wants to use your version:

```auto
Manipulate[F[x_] := a (UnitStep[x] - UnitStep[x - b]);
 Plot[F[x], {x, -1, 10}, PlotStyle -> Thick], {a, 0.5, 1}, {b, 1, 8}, 
 TrackedSymbols :> {a, b}]

```

---

<div class="post-metadata">

**Author:** ![bluigi74](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@bluigi74](https://community.wolfram.com/u/bluigi74)\
**Post date:** [June 4, 2014, 10:40am UTC](https://community.wolfram.com/t/a-function-defined-outside-manipulate-does-not-produce-plot/2301/5 "2014-06-04T10:40:22Z")

</div>

Thank you very much for thorough expalanation.  
So even if for me a and b are just parameters I have to define them as variables, very instructive… I will have to check on the documentation what TrackedSymbols is 🙂  
Thanks.

L.
