function [V, lambda, success, iterNum] = cpf_correctVoltage(baseMVA, bus, gen, Ybus, V_predicted, lambda_predicted, initQPratio, loadvarloc)
% function: do correction for predicted voltage in cpf 
% [INPUT PARAMETERS]
% loadvarloc: (in internal bus numbering)
% created by Rui Bo on 2007/11/12

%% define named indices into bus, gen, branch matrices
[PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
    VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;

%% get bus index lists of each type of bus
[ref, pv, pq] = bustypes(bus, gen);

%% set load as lambda indicates
lambda = lambda_predicted;
bus(loadvarloc, PD) = lambda*baseMVA;
bus(loadvarloc, QD) = lambda*baseMVA*initQPratio;

%% compute complex bus power injections (generation - load)
SbusInj = getSbusInj(baseMVA, bus, gen);

%% prepare initial guess
V0 = V_predicted; % use predicted voltage to set the initial guess

%% run power flow to get solution of the current point
mpopt = mpoption('VERBOSE', 0);
[V, success, iterNum] = newtonpf(Ybus, SbusInj, V0, ref, pv, pq, mpopt); %% run NR's power flow solver
