A new moving horizon model inversion method based on numerical optimization is presented to calculate the system input for a given system model and system output. It uses the moving horizon idea from Model Predictive Control (MPC) by using a moving and fixed-size window to inverse the data points in the current window using the initial states information (for the first window) or the last updated control point’s states information in the previous window. The approach can handle both linear and nonlinear systems, and also the sudden changes in system dynamics. Another advantage of this approach is its high computational efficiency when you have a long data record. The model inversion approach can be used in iterative learning control to refine the input to make the response of the system match the reference trajectory as close as possible.