# Creating a function that can be used to plot a triangle with an area of 1

The function should take an input x, which is the absolute value of the bounds of the triangle and should be centered at 0.

I'd like to use UnitTriangle, but if there's a better way, please let me know.

I'm not sure how to translate the width to the height.

Triangle[x_] := (2*(1/x))*UnitTriangle[x];
Plot[Triangle[x], {x, -2, 2} , PlotRange -> All]


I would do it with Piecewise. Like so:

unitTriangle[x_][u_] :=
Piecewise[{{(u + x)/x^2, -x <= u <= 0}, {(x - u)/x^2, 0 <= u <= x}}, 0]


### Demonstration

Manipulate[
Plot[unitTriangle[x][u], {u, -2, 2},
PlotRange -> All, Ticks -> {Automatic, Subdivide[0, 1/x, 5]}],
{{x, 1}, 1/10, 2, 1/10, Appearance -> "Labeled"}]


### Update

unitTriangle as defined above should be integrable, and it should integrate to 1 over the interval $$[-x, x]$$ for all $$x > 0$$.

Assuming[x > 0, Integrate[unitTriangle[x][u], {u, -x, x}]]


1

Q.E.D.

I think you're on the right track. One problem is that Triangle is a built-in function, so when you try to define your own Triangle there is the possibility that the definitions will overlap. In fact, you might have seen SetDelayed: Tag Triangle in Triangle[x_] is Protected. This is Mathematica telling you that it won't allow you to assign anything to Triangle. All built-in Mathematica functions (including things like the imaginary number I and Euler's number E) start with a capital. The best practice is to always start your own custom definitions and variable names with lower case letters.

The next thing that we can look at is the triangle itself. UnitTriangle is a good start, and you're multiplying by 2/x out front, I assume to try to rescale the height which is on the right track. The problem is that when you plot it, x can take on the value of zero, and then you have 2/0. So we can define a new variable which is the width. You have some options as to how to use this horizontal expansion, which I'll call a. You can either set it as a global variable a = 5, or you can fold it into the definition like this:

triangle[x_, a_] := UnitTriangle[a x]


Now we have something we can plot and will let us change the width by specifying different values of a when we call the function. But we also want to control the height. Larger values of a are going to actually decrease the width of the triangle. So a = 1 gets us a triangle from -1 to 1, a = 5 gets us a triangle from -0.2 to 0.2, and a = 1/4 gets us a triangle from -4 to 4. Thus, the larger the a value, the narrower the triangle, the taller we need the triangle to be.

Since:

$$area = \frac{1}{2} b h$$

and $$b$$ is actually $$2/a$$, we can replace it in the formula.

$$area = \frac{1}{2} \frac{2}{a} h$$

$$h = area \cdot a$$

$$h = 1 \cdot a$$

So, now we can put that out front of our definition to multiply the height.

triangle[x_, a_] := a UnitTriangle[a x]


One last thing we might want to do is be able to translate the triangle left or right.

triangle[x_, a_, b_] := a UnitTriangle[a(x-b)]
Manipulate[
Plot[
triangle[x, a, b],
{x, -10, 10},
PlotPoints -> 200,
PlotRange -> Full
],
{{a, 1}, 0.1, 4},
{{b, 0}, -5, 5}
]


UPDATE:

@m_goldberg had a fantastic idea that I didn't even know you could do, so I learned something neat. I figured I would add this to my answer as well since I feel it makes it more complete, despite me plagiarizing their excellent answer.

Assuming[a > 0, Integrate[triangle[x, a, b], {x, -Infinity, Infinity}]


1

• Apparently my gif broke and doesn't play automatically, but if you click on the image, you should be able to see it play. May 5 '19 at 5:15

You can also use the PDF of TriangularDistribution the area under which is 1:

Plot[Evaluate@Table[PDF[TriangularDistribution[{-a, a}], x], {a, 1, 3, 1/2}], {x, -4, 4},
Filling -> Axis, Exclusions -> None, PlotRange -> All,
PlotLabel -> Row[{"f[a][x] = ",
ToString[PDF[TriangularDistribution[{-a, a}], x], StandardForm]}],
PlotLegends -> (Row[{"a = ", ToString[#, StandardForm]}] & /@ Range[1, 3, 1/2])]