Your script compiles and runs fine on my end, without errors.

Error 041 is about "series declaration" (look under remarks for "series()" in the manual). So, try declaring series before if and for () statements.