function [R_rect,z_rect]=convert_polar2rect(R_polar,z_polar,slack)

%To convert from polar to rectangular measurements as well as their
%variance
%R_polar: variance for polar form=[R_ang R_abs]
%z_polar: vector of polar meas. =[z_abs z_ang]
%slack: index for slack bus

%R_rect: variance in rectangular form=[Rreal;Rimag]
%z_rect: vector of rectangular meas. (zreal;z_imag)

zreal=[];zimag=[];Rreal=[];Rimag=[];
for i=1:length(R_polar(:,1))
    if i==slack
        [zreal]=[zreal;z_polar(i,1)];
        [Rreal]=[Rreal;R_polar(i,2)];
    else
        VM=z_polar(i,1);VA=z_polar(i,2);
        RM=R_polar(i,2);RA=R_polar(i,1);
        [zreal]=[zreal;VM*cos(VA)];
        [zimag]=[zimag;VM*sin(VA)];
        [Rreal]=[Rreal;(cos(VA)^2)*RM+RA*(VM*sin(VA))^2];
        [Rimag]=[Rimag;(sin(VA)^2)*RM+RA*(VM*cos(VA))^2];
    end
end

R_rect=[Rreal;Rimag];
z_rect=[zreal;zimag];