Is there more code than what you provided here? You defined a variable gamma at the top but then reference a variable γ in your f(M) definition.
Other than that, nothing jumps out at me as an obvious error. It might help if you showed us a formatted/written version of the f(M) equation you’re trying to code to make sure that code actually says what you intended. The for i in 1:length loop with by-index writes/accesses is not very “Julian” code, so there’s probably a more elegant implementation that could be made, but that’s probably not central to the problem you’re currently trying to solve; feel free to ask for follow-up if interested in those tips.