The helical flagella that are attached to the cell body of bacteria such as Escherichia coli and Salmonella typhimurium allow the cell to swim in a fluid environment. These flagella are capable of polymorphic transformation in that they take on various helical shapes that differ in helical pitch, radius, and chirality. We present a mathematical model of a single flagellum described by Kirchhoff rod theory that is immersed in a fluid governed by Stokes equations. We perform numerical simulations to demonstrate two mechanisms by which polymorphic transformation can occur, as observed in experiments. First, we consider a flagellar filament attached to a rotary motor in which transformations are triggered by a reversal of the direction of motor rotation [L. Turner et al., J. Bacteriol. 182, 2793 (2000)]. We then consider a filament that is fixed on one end and immersed in an external fluid flow [H. Hotani, J. Mol. Biol. 156, 791 (1982)]. The detailed dynamics of the helical flagellum interacting with a viscous fluid is discussed and comparisons with experimental and theoretical results are provided.