You are missing the dp
term in the sum
dp = (x(2)-x(1));
area = sum(p)*dp;
H = -sum( (p*dp) * log2(p) );
This should bring you close enough...
PS,
be careful when you take log2(p)
for sometimes you might have empty bins. You might find nansum
useful.