What is wrong in script?

2 Ansichten (letzte 30 Tage)
Athira T Das
Athira T Das am 23 Jul. 2022
Bearbeitet: Torsten am 25 Jul. 2022
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 Kommentare
Torsten
Torsten am 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
Athira T Das am 24 Jul. 2022
Ok. But I am confused whether my for loops are working properly or not?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Torsten
Torsten am 24 Jul. 2022
Bearbeitet: Torsten am 24 Jul. 2022
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
X = 0
  4 Kommentare
Athira T Das
Athira T Das am 25 Jul. 2022
If any terms depends on m2, can I write the code like
for m1 = 0:M
faktor = nchoosek(M,m1);
for m2 = 0:M
faktor0 = nchoosek(M,m2);
.......................................
....................................
X = X + faktor0.*sum1.*sum2;
end
X = X*faktor
end
Torsten
Torsten am 25 Jul. 2022
Bearbeitet: Torsten am 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
X = 2048

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by