MATLAB: How to put constraints to an equation[constant]

equationSymbolic Math Toolboxsyms

I want to put constraints to this uniform distribution function,
f(x)=1/2 if 2<=x<=4,
0 otherwise
I am asked to write my own uniform distribution function, so I can't use the in built function.
I'm using syms so as to plot, but when I use the if statement it says "conversion of syms to logical form is not possible". I want to make a pdf but the only result I need is the graph.
Thank You, all the three functions are right. I do not know which one to choose as the best answer.

Best Answer

My suggestion:
clear; clc;
syms x
f(x)=piecewise((2<=x & x<=4), 1/2, 0)
fplot(f(x), 'LineWidth', 3);
xlabel('x'); ylabel('f(x)');
zoom on; grid on;
If you run the script (provided that you have symbolic math toolbox), you will receive: