MATLAB Answers

quintuple summation using a for loop

조회 수: 1(최근 30일)
Wyatt Panaccione
Wyatt Panaccione 23 Jun 2020
Ive tried setting the sum to an empty cell and filling it and not declaring it and I do not know what to do or whats wrong and why it will not give me an answer different than the inital declaration.
syms i j k l p q
U = ((-1)^j)*a' - ((-1)^i)*a;
V = ((-1)^j)*a' - ((-1)^i)*a;
W = ((-1)^j)*a' - ((-1)^i)*a;
r = sqrt(U*U+V*V+W*W);
Phi = -U*W*log(r-U)-V*W*log(r-V)+U*V*atan((U*V)/(r*W))-r*W;
for i = 0:1
for j = 0:1
for k = 0:1
for l = 0:1
for p = 0:1
for q = 0:1
sum = sum + (((-1)^(i+j+k+l+p+q))*Phi);
disp(sum)
end
disp(sum)
end
disp(sum)
end
disp(sum)
end
disp(sum)
end
disp(sum)
end
disp(sum)

  댓글 수: 4

표시 이전 댓글 수: 1
Walter Roberson
Walter Roberson 23 Jun 2020
You do not initialize sum to sym(0)
Note: we recommend against naming a variable sum: it happens a lot that people want to use the function sum in the same code they used sum as a variable. And it confuses readers.
Note: we would need your a to test this.
Wyatt Panaccione
Wyatt Panaccione 23 Jun 2020
%% Inputs
a = 0.035;%length of up/low magnet
b = 0.014;%width of up/low magnet
c = 0.007;%height of up/low magnet
am = .03;%length of middle magnet
bm = .01;%width of middle magnet
cm = 0.01;%height of middle magnet
%% Negative K
syms iw jw k l p q
U = ((-1)^jw)*am - ((-1)^iw)*a;
V = ((-1)^l)*bm - ((-1)^k)*b;
W = ((-1)^q)*cm - ((-1)^p)*c;
r = sqrt(U*U+V*V+W*W);
Phi = -U*W*log(r-U)-V*W*log(r-V)+U*V*atan((U*V)/(r*W))-r*W;
sum =[0];
for iw = 0:1
for jw = 0:1
for k = 0:1
for l = 0:1
for p = 0:1
for q = 0:1
sum = sum + (((-1)^(iw+jw+k+l+p+q))*Phi);
disp(sum)
end
end
end
end
end
end
disp(sum)
Wyatt Panaccione
Wyatt Panaccione 23 Jun 2020
Here is the entire code

댓글을 달려면 로그인하십시오.

답변(1개)

David Goodmanson
David Goodmanson 23 Jun 2020
편집: David Goodmanson 23 Jun 2020
Hello Wyatt,
The sum comes out zero because it is, in fact, zero. Each term in the desired sum is
((-1)^(iw+jw+k+l+p+q))*constant;
Each of the indices is +-1. Their sum is either even or odd.
When it's even, the term contributes (-1)^even = +constant to the desired sum.
When it's odd, the term is contributes (-1)^odd = -constant to the desired sum.
There are 2^6 instances of (iw+jw+k+l+p+q), and half of those sums are even, half odd as you can check. So the desired sum ends up as zero.
It would be better if you went with Walter's recommendation and used something other than 'sum' for the sum variable. 'Sum' would work fine.

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by