With distributed energy increasing, the grid become weak. Grid impedance leads to interaction between the inverter and grid, which will affect inverter stability and threaten the system safety. The influence of grid impedance to the system stability was analyzed from the view of phase margin. Phase margin will decrease with grid impedance increasing, which may lead the system to be unstable. In order to enhance the stability of the system with grid impedance variation a variable gain control strategy to compensate phase margin on real time is proposed. After using the lead network to compensate the phase, in order to maximize the phase margin with grid impedance varying, a nonlinear model based on radial basis function neural network was built to regulate the cutoff frequency. The result of simulation validated the theoretical analysis in the paper.