You need to compare the absolute value of the term to your tolerance. Remember, some of the terms are negative.
while abs(term(i-1)) >= 0.0001
And yes, you would break if your counter (i or a) reached a predetermined limit.
You will also need to double check and fix up the indexing you are using for your calculations ...