%w=400000;m=127;
l=6000;l0=18000;
%beta=[10^(-7.5) 10^(-8) 10^(-9) 10^(-9.5) 10^(-10.5)];
beta=[1.0*10^(-8.5)]
alfa=1.158*l/l0;
w=400;%m=133;
%d=w/m;
d=3;
f=1:d:w;
k=max(size(f));p=ones(1,k);

%l=linspace(.000985,.000872,127);
%c=(.083*10^(-6)).*ones(1,k);hc=ones(1,k);

%for t=1:k
%r(t)=438.29996+33.3*(.00001*f(t))+5.012*(.00001*f(t))^2-.42*(.00001*f(t))^3+.012*(.00001*f(t))^4-.001*(.00001*f(t))^5;

%gamma(t)=sqrt((r(t)+i*2*pi*f(t)*l(t))*(i*2*pi*f(t)*c(t)));
%end
%alfa=unwrap(real(gamma));
%for t=1:k

%hc(t)=exp(-alfa(t)*2*9000*(.3048/1609.3));
%hc1(t)=exp(-1.4*.5*f(t)^.5);

%end
du= [1.3403e-002;
  1.2344e-002;
  1.0546e-002;
  8.6512e-003;
  7.0061e-003;
  5.6988e-003;
  4.6969e-003;
  3.9363e-003;
  3.3558e-003;
  2.9068e-003;
  2.5540e-003;
  2.2719e-003;
  2.0423e-003;
  1.8524e-003;
  1.6928e-003;
  1.5568e-003;
  1.4396e-003;
  1.3376e-003;
  1.2479e-003;
  1.1684e-003;
  1.0975e-003;
  1.0337e-003;
  9.7600e-004;
  9.2353e-004;
  8.7554e-004;
  8.3143e-004;
  7.9069e-004;
  7.5291e-004;
  7.1774e-004;
  6.8489e-004;
  6.5410e-004;
  6.2517e-004;
  5.9791e-004;
  5.7218e-004;
  5.4781e-004;
  5.2468e-004;
  5.0266e-004;
  4.8163e-004;
  4.6151e-004;
  4.4221e-004;
  4.2367e-004;
  4.0584e-004;
  3.8865e-004;
  3.7207e-004;
  3.5605e-004;
  3.4055e-004;
  3.2551e-004;
  3.1091e-004;
  2.9671e-004;
  2.8289e-004;
  2.6942e-004;
  2.5632e-004;
  2.4359e-004;
  2.3124e-004;
  2.1927e-004;
  2.0770e-004;
  1.9652e-004;
  1.8573e-004;
  1.7532e-004;
  1.6528e-004;
  1.5559e-004;
  1.4626e-004;
  1.3727e-004;
  1.2865e-004;
  1.2037e-004;
  1.1246e-004;
  1.0491e-004;
  9.7722e-005;
  9.0882e-005;
  8.4381e-005;
  7.8208e-005;
  7.2348e-005;
  6.6790e-005;
  6.1527e-005;
  5.6551e-005;
  5.1858e-005;
  4.7445e-005;
  4.3306e-005;
  3.9436e-005;
  3.5826e-005;
  3.2468e-005;
  2.9350e-005;
  2.6460e-005;
  2.3787e-005;
  2.1322e-005;
  1.9054e-005;
  1.6975e-005;
  1.5077e-005;
  1.3351e-005;
  1.1789e-005;
  1.0380e-005;
  9.1149e-006;
  7.9831e-006;
  6.9744e-006;
  6.0788e-006;
  5.2868e-006;
  4.5893e-006;
  3.9781e-006;
  3.4453e-006;
  2.9832e-006;
  2.5848e-006;
  2.2433e-006;
  1.9521e-006;
  1.7054e-006;
  1.4975e-006;
  1.3236e-006;
  1.1792e-006;
  1.0605e-006;
  9.6403e-007;
  8.8709e-007;
  8.2722e-007;
  7.8243e-007;
  7.5108e-007;
  7.3184e-007;
  7.2365e-007;
  7.2569e-007;
  7.3732e-007;
  7.5808e-007;
  7.8760e-007;
  8.2565e-007;
  8.7206e-007;
  9.2669e-007;
  9.8947e-007;
  1.0603e-006;
  1.1390e-006;
  1.2256e-006;
  1.3196e-006;
  1.4210e-006;
  1.5292e-006;
  1.6438e-006;
  1.7645e-006;
  1.8905e-006;
  2.0214e-006;
  2.1565e-006];


duc=du';
for l=1:k
   duu(l)=duc(l)*((f(l)^2/(f(l)^2+1)))^2;
   end

for g=1:1
for n=1:k
%hc3(n)=exp(-alfa*f(n)^.5);
hc3(n)=duu(n);
hx(n)=beta(g)*f(n)^1.5;
end
p1=100;
la=w/p1
n0=10^(-12);


for n=1:k
a(n)=hx(n)*(hx(n)+hc3(n));n1=n0*d*10^3;
b(n)=n1*(2*hx(n)+hc3(n));c(n)=n1*(n1-d*(1/la)*hc3(n));
p(n)=(1/(2*a(n)))*(-b(n)+(b(n)^2-4*a(n)*c(n))^.5);
end

s=0;
for t=1:k
s=s+p(t);
end
dif=s-p1;
while dif<0
e=.02;
la=(1-e)*la;
for n=1:k
a(n)=hx(n)*(hx(n)+hc3(n));n1=n0*d;
b(n)=n1*(2*hx(n)+hc3(n));c(n)=n1*(n1-(1/la)*d*hc3(n));
p(n)=(1/(2*a(n)))*(-b(n)+(b(n)^2-4*a(n)*c(n))^.5);
end
q=zeros(1,k);y=q>=c;u=y.*p;
s=0;
for t=1:k
s=s+u(t);
end
dif=s-p1;
end
x=1:1:k;f1=d.*x;p2=(1/1).*u;
plot(f1,u)
%hold on
%semilogy(f1,p2)
cap(g)=0;
for n=1:k
cap(g)= cap(g)+d*log2(1+p(n)*hc3(n)/(n1+hx(n)*p(n)));
end
end
xlabel ('frecventa in khz');
ylabel ('densitatea spectrala per canal in mW')
title('Distributia puterii pentru o capacitate maxima')
%gtext('beta=10^(-7.5)') 
%gtext('beta=10^(-8)') 
gtext('beta=1.0*10^(-13)') 
%gtext('beta=10^(-9.5)') 
%gtext('beta=10^(-10.5)') 
for  n=1:k; ca(n)=log2(1+p(n)*hc3(n)/(n1+hx(n)*p(n))); end;
%plot(f,ca)