What is wrong in script?
Show older comments

for m1 =0:M
T2=0;
for m2=0:M
T3=0;
for s1=0:m1/2
T4=0
for j1=0:m1-2*s1
T5=0
for s2=0:(M-m1)/2
T6=0;
for j2=0:(M-m1-2*s2)
T6=T6+(1/(po^2))^j2;
end
T5=T5+T6*((2*1i)/(delta^(0.5)))^(M-m1-2*s2);
end
T4=T4+T5*(1/(po^2))^j1;
end
T3=T3+T4*((2*1i)/(delta^(0.5)))^(m1-2*s1);
end
T2=T2+T3*(factorial(M)/(factorial(m2)*factorial(M-m2)));
end
T1=T1+T2*(factorial(M)/(factorial(m1)*factorial(M-m1)));
end
10 Comments
Just out of interest: Where do all these horrible sums stem from that you posted during the last days and weeks ?
What is meant if the sum goes to m1/2 or (M-m1)/2 ? Is it the usual treatment that summing up to 5/2, e.g., means summing for 0,1 and 2 ?
Further, you didn't give values for variables and loop limits. So nobody can tell what you mean by "What is wrong in Script ?" because nobody can test it.
Athira T Das
on 23 Jul 2022
Torsten
on 23 Jul 2022
And what about the loop limits ?
Jan
on 24 Jul 2022
@Athira T Das: Please share the important information with the readers: Why do you assume, that the script is wrong?
Is this the code you try to run ?
M = 3;
w = 0.2;
z = 0:10000;
k = 5.9275e+06;
Po = (k^2*z).^(-(3/5));
Delta = 1i*k./(2*z) + 1/w^2 + 1./Po.^2;
po = Po(1);
delta = Delta(1);
T1 = 0;
for m1 =0:M
T2=0;
for m2=0:M
T3=0;
for s1=0:m1/2
T4=0;
for j1=0:m1-2*s1
T5=0;
for s2=0:(M-m1)/2
T6=0;
for j2=0:(M-m1-2*s2)
T6=T6+(1/(po^2))^j2;
end
T5=T5+T6*((2*1i)/(delta^(0.5)))^(M-m1-2*s2);
end
T4=T4+T5*(1/(po^2))^j1;
end
T3=T3+T4*((2*1i)/(delta^(0.5)))^(m1-2*s1);
end
T2=T2+T3*(factorial(M)/(factorial(m2)*factorial(M-m2)));
end
T1=T1+T2*(factorial(M)/(factorial(m1)*factorial(M-m1)));
end
T1
Athira T Das
on 24 Jul 2022
Edited: Athira T Das
on 24 Jul 2022
What about the value for k ?
sqrt(0.545) or 5.9275e6 ?
What about the upper looplimit ? Like this: loop over integers up until 2 ?
k = 0;
for j = 0:5/2
k = k + 1;
end
k
Athira T Das
on 24 Jul 2022
Edited: Athira T Das
on 24 Jul 2022
Torsten
on 24 Jul 2022
k = 5.9275e+06
And why do you write
rho = (0.545*z)^(-3/5)
instead of
rho = (5.9275e+06^2*z)^(-3/5)
in your formula ?
Athira T Das
on 24 Jul 2022
Answers (1)
I would have progrmmed it this way, but the result seems to be the same.
My guess is that there is a mathematical trick to extremely simplify the expression (something like sum_{i=0}^(i=n} nchoosek(n,i) * p^i * (1-p)^(n-i) = 1), but I don't see it at the moment.
M = 3;
w = 0.2;
z = 0:10000;
k = 5.9275e+06;
Po = (k^2*z).^(-(3/5));
Delta = 1i*k./(2*z) + 1/w^2 + 1./Po.^2;
po = Po(1);
delta = Delta(1);
X = 0;
for m1 = 0:M
faktor = nchoosek(M,m1);
sum1 = 0.0;
for s1 = 0:m1/2
faktor1 = (2*1i/sqrt(delta)).^(m1-2*s1);
sum_inner = 0.0;
for j1 = 0:m1-2*s1
sum_inner = sum_inner + (1/po^2)^(j1);
end
sum1 = sum1 + faktor1*sum_inner;
end
sum2 = 0.0;
for s2 = 0:(M-m1)/2
faktor2 = (2*1i/sqrt(delta)).^(M-m1-2*s2);
sum_inner = 0.0;
for j2 = 0:M-m1-2*s2
sum_inner = sum_inner + (1/po^2)^(j2);
end
sum2 = sum2 + faktor2*sum_inner;
end
X = X + faktor*sum1*sum2;
end
X = X*2^M
4 Comments
Athira T Das
on 24 Jul 2022
Edited: Athira T Das
on 24 Jul 2022
Torsten
on 24 Jul 2022
Since all the expressions don't depend on m2, you can take the sum out and it gives
sum_{k=0}^{k=M} nchoosek(M,k) = 2^M
Athira T Das
on 25 Jul 2022
I think something like this:
M = 6;
w = 0.2;
z = 0:10000;
k = 5.9275e+06;
Po = (k^2*z).^(-(3/5));
Delta = 1i*k./(2*z) + 1/w^2 + 1./Po.^2;
po = Po(1);
delta = Delta(1);
X = 0;
for m1 = 0:M
faktor_m1 = nchoosek(M,m1);
sum_m2 = 0.0;
for m2 = 0:M
faktor_m2 = nchoosek(M,m2);
sum1 = 0.0;
for s1 = 0:m1/2
faktor1 = (2*1i/sqrt(delta)).^(m1-2*s1);
sum_inner = 0.0;
for j1 = 0:m1-2*s1
sum_inner = sum_inner + (1/po^2)^(j1);
end
sum1 = sum1 + faktor1*sum_inner;
end
sum2 = 0.0;
for s2 = 0:(M-m1)/2
faktor2 = (2*1i/sqrt(delta)).^(M-m1-2*s2);
sum_inner = 0.0;
for j2 = 0:M-m1-2*s2
sum_inner = sum_inner + (1/po^2)^(j2);
end
sum2 = sum2 + faktor2*sum_inner;
end
sum_m2 = sum_m2 + faktor_m2*sum1*sum2;
end
X = X + faktor_m1*sum_m2;
end
X
Categories
Find more on Structured Data and XML Documents in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!