Most simple approach would be using a MATLAB Function Block in Simulink. For example you can call the optimization routine usign a function call to Simulink Function - in case this is all Simulink. When simulation time is relevant for your optimization, model the loops in a Stateflow Chart. More elegant as well.
A third option is using For and While iterator subsystem (ports and subsystem library). As you played with them, you probably do not want to go this route.