why does it take forever to solve a function

4 views (last 30 days)
im trying to compute value of "pd" for various values of "lnd"..... but it takes too much time without any results and when i terminate i get this message :
Operation terminated by user during sym/symsum (line 65)
In Untitled5 (line 12)
pd=(symsum(nchoosek(M,i)*(pd1)^(i)*(1-pd1)^(M-i), i, s, M));
equations i'm trying to implement are attached
M=8;
N=32;
K=14;
c=2;
s=1;
T=18.623390280430364437245197384232;
lnd=30*rand;
pd1=2*K*nchoosek(N/2,K)*((gamma(K+i)*gamma(N-K+i+1+T/(1+lnd)))/(gamma(N+1+T/(1+lnd)))-...
(symsum(nchoosek(N/2,i)*(gamma(K+i)*gamma(N-K-i+1+T/(1+lnd)))/(gamma(N+1+T/(1+lnd))), i, K, (N/2))));
pd=(symsum(nchoosek(M,i)*(pd1)^(i)*(1-pd1)^(M-i), i, s, M));
plot(lnd,pd,'b.')

Accepted Answer

Walter Roberson
Walter Roberson on 31 May 2017
symsum is often the wrong function to call when you want to calculate a definite sum (that is, the upper and lower bound are numeric.) symsum() works by trying to calculate the general formula for the indefinite summation, and then substituting in the upper and lower bounds. Calculating the general formula can take a long time.
Instead, when you have numeric upper and lower bounds, you should typically generate all of the individual entries; vectorize if possible and otherwise use a for loop or arrayfun(). Then with those in hand, sum() the result.
I corrected some errors in your calculations of pd1
M = 8;
N = 32;
K = 14;
c = 2;
s = 1;
T = 18.623390280430364437245197384232;
nlambda = 50;
lambdavals = linspace(0,30,nlambda);
pd = zeros(1, nlambda);
for L = 1 : nlambda
lambda = lambdavals(L);
T1lambda = T./(1+lambda); %constant sub-expression factored out
pd1temp = arrayfun(@(i) nchoosek(N/2,i) * gamma(K+i) * gamma(N-K-i+1+T1lambda), K : N/2, 'Uniform', 0); %constant denominator removed from terms
pd1sum = sum( [pd1temp{:}] ) ./ gamma(N+1+T1lambda); %this is the first symsum
pd1 = 2 * K * nchoosek(N/2,K) * gamma(K) * gamma(N/2-K+1+T1lambda) / gamma(N/2+1+T1lambda) - pd1sum;
pdtemp = arrayfun(@(i) nchoosek(M,i)*(pd1)^(i)*(1-pd1)^(M-i), s:M, 'Uniform' ,0);
pdsum = sum( [pdtemp{:}] ); %this is the second symsum
pd(L) = pdsum;
end
plot(lambdavals, pd)
You will find that this will take less than a second to compute.
  3 Comments
Walter Roberson
Walter Roberson on 31 May 2017
Do not use symsum() for adding definite symbolic terms. Calculate the individual terms and sum() them.
pd1=(2*K*nchoosek(N/2,K)*((gamma(K+i)*gamma(N-K+i+1+T./(1+lnd)))./(gamma(N+1+T./(1+lnd)))-...
If pd1 is related to the image that you posted, then you have coded it incorrectly. I detected multiple errors in your coding and fixed them in my version. In particular, your gamma(N references should be gamma(N/2 references in the first line of your pd1 code.
Fatma Abdullah
Fatma Abdullah on 31 May 2017
thank you so much,,,i will try to do that and fix the code

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!