Search code examples
matlablimit

Error while calculating limit - Conversion to logic from syms is not possible - MATLAB -2014a


I want to calculate the limit of log(CR(r))/log(r) as r tends to 0. MATLAB code is written below.

function cd = CD(data)    
  syms r 
  cd =  limit(log(CR(r,data))/log(r),r,0) ;
end

function val = hf(xi,xj,r)
    dis = abs(xi-xj);

    if(dis <= r)
        val = 1;
    else
        val = 0 ;
    end
end

    function cr = CR(r,data)
       N = length(data);
      sum = 0;
       for i = 1 : N
        for j = i+1 : N

            sum = sum + hf(data(i),data(j),r);
        end
     end
      cr = sum/(N*(N-1));
end

Error:-
Output Snap


Solution

  • Well, the error message says it all really:

    You can't use a symbolic variable in an equality check. How can you know if dis <= r when r doesn't have a value?

    I'm obligated to say this:

    Don't use sum as a variable name! It's a very important and useful builtin function. You're making it useless when doing that.

    i and j are bad variable names in MATLAB, since they denote the imaginary unit (sqrt(-1)).

    Also, did I remember to say: Dont use sum as a variable name!

    PS! Your hf-function is equivalent to:

    function val = hf(xi,xj,r)
        var = abs(xi-xj) <= r;   % Still doesn't work since r is symbolic
    end