MATLAB: Maximum value of a normalized power changes

normnormalizationpower

I have a function (looks like gaussian) and I want to plot it such that the area under the plot is always 1 (power normalization). I know it is done by dividing the function by its L2-norm. What is interesting here is, when the sample size increases, maximum value of the function decreases. here is the code:
al=-pi/2: pi/10: pi/2;
cf=((1+cos(al))/2).^1;
plot(al,cf/norm(cf));
hold on;
al=-pi/2: pi/100: pi/2; %increase the sample size
cf=((1+cos(al))/2).^1;
plot(al,cf/norm(cf));
It must be independent of the sample size. It seems I need to multiply cf with length(cf) or length(cf)^2 to get a fixed maximum value for a normalized power but couldn't find out a takeaway from that.
Thanks in advance.

Best Answer

Hi pos,
First of all, if you want to approximate the area, that's done with intervals and not with points. Just summing the squares of the points overweights the two end points. However, you can average the each interval over adjacent points (simple trapezoidal rule) with
cfave = (cf(1:end-1)+cf(2:end))/2
The integral is approximated with
sum(cfave.^2)*h
where h is the width of the intervals, assuming they are all the same. That is the factor you were missing.
Let C be the normalization factor for cfave such that
sum((C*cfave).^2)*h = 1
this works out to
C = 1/(norm(cfave)*sqrt(h))
The code below gives almost exactly the same plot for different values of the interval like you were doing before.
a=-pi/2: pi/10: pi/2;
cf=((1+cos(a))/2).^1;
cfave = (cf(1:end-1)+cf(2:end))/2;
h = pi/length(cfave);
C = 1/(norm(cfave)*sqrt(h))
cfnorm = C*cf; % back to points
figure(1)
plot(a,cfnorm);