clear all; channel_n=10; %channel number M=[5,10,20,50]; %transmitted power N0=0.5; h_1= random('rayleigh',1,1,channel_n); %Rayleigh fading h_2=h_1.^2; %|h|.^2 h_2_sorted=sort(h_2); %sort the channel gain h=h_2_sorted/N0; syms lamda for m=1:length(M) fprintf('transmitted power is %d watt',M(m)); for k=1:channel_n p=zeros(1,channel_n); sum=0; for i=k:channel_n sum=sum+(1/lamda-1/h(i)); end f=sum-M(m); x=solve(f,lamda); %find lamda if k==1 if vpa(x)>0 && vpa(x)<h(k) for i=k:channel_n p(i)=1/x-1/h(i); %allocate power capacity=capacity+log2(1+p(i)*h(i));%compute the capacity end p(i)=vpa(p(i),3) capacity=vpa(capacity,3) end else if vpa(x)>h(k-1) && vpa(x)<h(k) capacity=0; for i=k:channel_n p(i)=1/x-1/h(i); %allocate power capacity=capacity+log2(1+p(i)*h(i)); %compute the capacity end p(i)=vpa(p(i),3) capacity=vpa(capacity,3) end end end end