Message Boards Message Boards


A function defined outside Manipulate does not produce Plot

Posted 9 years ago
4 Replies
8 Total Likes
Dear Users,

I really just started with Mathematica and I'm already stuck on something that looks pretty silly... I first do:
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:
F[x_] := a (UnitStep[x] - UnitStep[x - b])

and then
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,
POSTED BY: Luigi Bagolini
4 Replies
Posted 9 years ago

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.


POSTED BY: Luigi Bagolini
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:
...and here's the correct version:
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:
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
POSTED BY: Bianca Eifert
Posted 9 years ago
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.
POSTED BY: Bill Simpson
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:
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}]
POSTED BY: Bianca Eifert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract