A new optimal power flow(OPF) algorithm is proposed. Using function transformation method to translate variable inequality equations to equality equations and constructing optimization equation with object function and its higher limit, the extended power flow equations with constraints of variable ranges are found. The equations are nonlinear and indefinite equations. By decreasing the higher limit of the object function step by step, applying method of the generalized inverse of matrices and Newton-Raphson method, the optimization process is turned into a series of one dimensional optimization of the nonlinear indefinite equations resolution. The new algorithm is illustrated through examples of minimizing generation cost in OPF and minimizing active loss in reactive power optimization. Case studies show the practicability of the algorithm.