MATLAB: Easy question from a newbie

matrixnewbirdvector

I want to store the function outputs into a vector or matrix using the for loop Here is my code
for n = [10, 100, 1000]
m(i) = abs(compMidpoint(f,a, b,n)-val)
z(i) = abs(compTrapez(f,a, b,n)-val);
s(i) = abs(compSimpsons(f,a, b,n)-val);
i = i+1
end
the matlab reported me an error:
Subscript indices must either be real positive integers or logicals.
m(i) = abs(compMidpoint(f,a, b,n)-val);
Anyone knows what's going on here

Best Answer

There are three possibilities.
1. You have defined a variable named abs
2. You have defined a variable named compMidpoint.
3. You never defined i to start, before the loop.
In the last case, i will have the value sqrt(-1) by default. My guess is this is what you did. What value does i have on the first pass through the loop?
It is generally a bad idea to use i as a variable, although I admit I do tend to use it, coming from my old days with Fortran. So lets say that unless you know what you are doing, use of i as a variable can be dangerous. :)
Related Question