MATLAB: Heaviside duty cycle symbolic function

duty cycleheavysidestep functionsymbolic

I'm trying to write a symbolic function to simulate an electrical device's electricity usage. Since the electricity is billed different price at different time of the day, I wanted to write a symbolic function with a duty cycle of 0.125 (on 15 minutes, off 1h45 minutes). I thought it would be easy with matlab.
syms x
fridge = 0;
for ii = 0:12
fridge = fridge + heaviside(x-(ii*2)) - heaviside(x-(0.25+(ii*2)));
end
fplot(fridge,[0, 24])
There are some parts of the functions not plotting correctly, like
heaviside(x-6)-heaviside(x-6.25)
Any ideas what could be wrong?
EDIT: When I plot my function from 0 to 12, it seems to work fine but when plotted from 0 to 18 it stops working.

Best Answer

The fplot appears to miss certain values for the range of values you have specified. The reason for this apparent ambiguity is that the number of function evaluations is set to the default value of the " MeshDensity " parameter (23) and hence certain critical points seem to have been missed for the range [0,24].
To obtain correct results I would suggest increasing the MeshDensity value to 60:
>> fplot(fridge,[0, 24], 'MeshDensity', 60)
Setting the value to 61, ensures that the value of 1/2 (at x == 6) is correctly represented as well:
>> fplot(fridge,[0, 24], 'MeshDensity', 61)