Summation expression for loop
Show older comments
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

Answers (2)
Walter Roberson
on 18 Jan 2018
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 Comment
Birdman
on 18 Jan 2018
Firstly I used symsum but when I noticed what you said I decided not to use it.
Birdman
on 18 Jan 2018
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.
Categories
Find more on Common Operations 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!