Abdulhameed
Abdulhameed

Reputation: 210

What is wrong in this short MATLAB code?

The code below attempts to plot the graph based on the equations shown in this figure. The initial parameters are given in the code. I get errors even with this small code. Where did I get it wrong? The F-function refers to hypergeometric function.

enter image description here

alpha = 4;
delta = 2/alpha;
P_c = 1;% 
P_d = 1;
T = 1;
%Tplot= linspace(-7.5, 10, 2.5); %referring to T % on the x-axis
Tplot=10^(-7.5/10):10^(2.5/10):10^(10/10); based on @Nakini answer
lambda_c = 0.01;
lambda_d = [0.002,0.01, 0.05]; 
mu = (delta/(1-delta)).*Tplot.* hypergeom([1, 1-delta], 2-delta, -Tplot);
v = (Tplot.^delta).*gamma(1-delta).*gamma(1+delta);
for k = 1:3 % from Dr Core's Edit
p_d(:,k) = lambda_d(k)./((lambda_d(k) .* (v + 1) + lambda_c.*(P_c/P_d).^delta).*v);
p_c(k,:) =  lambda_c./((lambda_c*(mu + 1) + lambda_d(k).*P_d/P_c).^delta).*v);
end
plot (Tplot, p_c);
hold on
plot (Tplot, p_d);

Edit: I have edited the code based on answers from @Doctor Core and @Nakini. The modifications made have been highlighted in the code. However, I seem not to get it yet.

Upvotes: 1

Views: 140

Answers (2)

Nakini
Nakini

Reputation: 772

  1. The use of linspace is not entirely correct. If you want something similar to the plot, you need to do: Tplot= linspace(-7.5, 10, 8); or Tplot= -7.5:2.5:10;.
  2. Are you sure that you need to use the value of Tplot as it is? It seems like T is in db.
  3. If option-2 is correct, then
    • hypergeom function causes a problem when Tplot == 0. Matlab throw a division by zero error. I am not sure what this function is doing, though. But I can guess that the function(s) has a value between 0 and 1 when T is 0db.
    • For all -ve values of Tplot, you are going to get complex numbers from hypergeom
    • You are also going to get complex numbers for v when Tplot is -ve
  4. As p_c and p_d are dependent on v, you will get complex numbers as long as v is complex.

In short, you need to check the values of T. And once you use positive values of T everything will fall into place, I guess. Of course, you need to take care of syntax while evaluating expressions like lambda_c(mu + 1).

EDIT

Here is the correct version of your code:

alpha = 4;
delta = 2/alpha;
P_c = 1;% 
P_d = 1;
T = -10:2.5:10;
%Tplot= linspace(-7.5, 10, 2.5); %referring to T % on the x-axis
% Tplot=10^(-7.5/10):10^(2.5/10):10^(10/10); %based on @Nakini answer
Tplot=db2pow(T); %based on @Nakini answer
lambda_c = 0.01;
lambda_d = [0.002,0.01, 0.05]; 
mu = (delta/(1-delta)).*Tplot.* hypergeom([1, 1-delta], 2-delta, -Tplot);
v = (Tplot.^delta).*gamma(1-delta).*gamma(1+delta);
col = ['-og'; '-or'; '-ob'];
figure;hold on; grid on;
for k = 1:3 % from Dr Core's Edit
    p_c =  lambda_c./(lambda_c*(mu + 1) + (lambda_d(k).*(P_d/P_c).^delta).*v);
    p_d = lambda_d(k)./(lambda_d(k) .* (v + 1) + lambda_c.*(P_c/P_d).^delta.*v);
    plot (T, p_c, col(k, :), 'MarkerFaceColor', col(k,3));
    plot (T, p_d, col(k, :), 'MarkerFaceColor', col(k,3));
end
set(gca, 'XTick',T);
xlabel('T(db)')
hold off;

And the output is: Output

Upvotes: 1

Doctor Core
Doctor Core

Reputation: 102

I spotted two kinds of mistakes:

  1. you left out the operator lambda_c(mu + 1) should be lambda_c*(mu + 1) this happened at least twice.

  2. seems you are trying to generate 3 curves for p_d using a 3x1 lambda_d with a 10x1 Tplot. Matlab vectorization doesn't work that way. You should either use a for loop or do something smart (and hard to read) with 2D arrays. Hint: make p_d a 10x3 array.

Try

for k = 1:3
    p_d(:,k) = lambda_d(k)./((lambda_d(k) .* (v + 1) + lambda_c.*(P_c/P_d).^delta).*v);
end

Upvotes: 1

Related Questions