1
$\begingroup$

I'm trying to draw this function: $$f(x) = x(\theta(x-1)-\theta(x-2))$$ Where $\theta(x)$ denotes the Heaviside theta function, in mathematica I wrote the function this way:

f[x_] = xHeavisideTheta[x - 1] - xHeavisideTheta[x - 2]
Plot[f[x], {x, 0, 10}]

But then this is the plot that I get in Mathematica: Mathematica

Whereas in wolfram alpha I get this following plot: wolfram alpha

What am I doing wrong in mathematica?

$\endgroup$
3
  • 4
    $\begingroup$ just add space after the x like this !Mathematica graphics i.e. change xHeavisideTheta[x - 1] to x *HeavisideTheta[x - 1] $\endgroup$
    – Nasser
    Dec 26, 2022 at 17:09
  • $\begingroup$ @Nasser Thank you I just had to add the * without the space to get it to work, I thought it would automatically multiply without the * like when I multiply a variable by a constant $\endgroup$
    – flex_
    Dec 26, 2022 at 17:14
  • 1
    $\begingroup$ Without either a space or an asterisk, xHeavisideTheta is a single symbol. Mathematica will not try to guess what you mean. $\endgroup$
    – Bob Hanlon
    Dec 26, 2022 at 17:27

1 Answer 1

1
$\begingroup$

In addition to spaces, I also use := when I define a function,

this should work:

f[x_] := x HeavisideTheta[x - 1] - x HeavisideTheta[x - 2]
Plot[f[x], {x, 0, 10}]
$\endgroup$

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