Search code examples
matlabincrementinfinity

Inf value after incrementation


Here's my code:

x=0; 
for i=1:100
    x=x+sqrt(((1/((((2*i)-2)^2)*(((2*i)+2)^2)))*16)+8);
end

For some reason I keep getting the infinity value (inf) for x after this operation???

Can someone help explain why?


Solution

  • That is because you divide by zero at i==1 : Take a look at (2*i)-2 where i==1.

    I can only guess that you actually meant to use i as the imaginary square root of -1. In that case, change the loop to:

    x=0;
    for k=1:100 x=x+sqrt(((1/((((2*i)-2)^2)*(((2*i)+2)^2)))*16)+8); end
    

    And in that case the loop is not necessary at all.