Control of flow behavior in complex fluids using automatic differentiation