Summation expression for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
I have to write a "For loop" for the expression below and that what I wrote. But I am not sure if it is write or not. If someone would verify my work, I would appreciate it. Thank you in advance.
sum1 = 0;
for ii = 1:2:9;
sum2 = 0;
for jj = 2:2:10;
sum2+cos(ii.*jj.*pi./'12);
end
sum1 = sum1+(sum2.*ii);
end

댓글 수: 0
답변 (2개)
Walter Roberson
2018년 1월 18일
syms I J
symsum(symsum(cos((2*I-1)*(2*J)*sym('pi')/12), J, 1, 5), I, 1, 5)
%result is 0
I used I and J instead of i and j because symsum() needs consecutive values not values incrementing by 2
댓글 수: 1
Birdman
2018년 1월 18일
Firstly I used symsum but when I noticed what you said I decided not to use it.
Birdman
2018년 1월 18일
Well, I believe your nested for loop structure is wrong for your problem since it does require two separate for loops. I tried to show the solution in symbolic and for loop way. Please see the following approaches:
%symbolic way
syms i j
fun1=cos((i.*j.*pi)./12);
jj=subs(j,2:2:10);
fun2=sum(subs(fun1,j,jj))
ii=subs(i,1:2:9);
result=sum(subs(i.*fun2,i,ii))
%%result is zero.
%for loop
syms i j
sumj=0;
sumi=0;
for j=2:2:10
sumj=sumj+cos((i.*j.*pi)/12);
end
for i=1:2:9
sumi=sumi+i*subs(sumj,i);
end
%%sumi is zero.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!