Principal subspace analysis (PSA) and minor subspace analysis (MSA) are considered two robust instruments in many fields. The dual purpose algorithm is capable of solving the PSA and MSA by simply switching the sign of one algorithm. Until today, there have been few dual purpose algorithms that are able to find their corresponding cost functions. In this paper, a novel unified cost function (NUCF) is proposed that possesses a global maximum, which is achieved only in the case where the weight matrix encompasses the desired principal or minor subspace. With the use of the gradient ascent method in the NUCF, we propose a novel dual purpose algorithm, which possesses lower computational complexity when compared with some existing algorithms. Numerical simulations and real applications illustrate that the proposed dual purpose algorithm is capable of tracking the desired subspace, and it converges faster than some similar types of algorithms.