For the following pseudocode; I think that the number of flops is 2n^3. However, I am unsure that this is correct as the for loops make me doubt it. (Note: aij and xij represent entries for matrices A and X respectively)
for =1:
for =1:
for =:
=+*
end
end
end
This isn't really a matlab question. Here is a brute force way of working it out.
The inner equation has two flops, so the
kloop has2(n-j+1)flops.To do the rest, it helps to know the sum of
qfrom1topisp(p+1)/2andq^2from1topisp(p+1)(2p+1)/6.The
jloop is of2(n-j+1)forjfrom1toi, so it has2i(n+1)-i(i+1)=2i(n+1/2)-i^2flops.The overall, or
i, loop is a sum of2i(n+1/2)-i^2, givingn(n+1)(n+1/2) - n(n+1)(2n+1)/6 = n(n+1)(2n+1)/3.You can see that this is the same as the sum from
1tonof2i^2.A way to check this, e.g. in matlab, is to set some
n, and putf=0at the start and replace the inner equation withf=f+2;, then the result will bef=n(n+1)(2n+1)/3.