Max deviation calculation using arduino

Can someone give out how it can be implemented below equation.

How to calculate unbalance –
Unbalance is calculated in terms of maximum deviation of current in a phase from the mean of three phases. To calculate the percentage deviation- [1]

Unbalanced_Formula

Where- Im is mean of currents in three phases (i.e. Im= (Ir+Iy+Ib)/3

Ir, Iy, Ib are phase currents.

Besides, an unbalance can also be quantified by comparing the intensity of negative sequence currents in comparison to the positive sequence currents. The permissible limit in terms of percentage of negative phase sequence current over positive sequence current is 1.3% ideally but acceptable upto 2%.[2]

Based on above equation I have come up with below code. But I wanted to know in which two phase unbalance has occurred. can someone guide me here

unsigned int Max_Current=0;
unsigned int Min_Current=0;
unsigned int Unbalance_limit=10;
unsigned int Max_Deviation=0;
unsigned int Percentage_Unblance=0;



void Unbalance_Check()

{

Imean_Current=(Rph_Current+Yph_Current+Bph_Current)/3;

if((Rph_Current>Yph_Current)&&(Rph_Current>Bph_Current))
{
Max_Current=Rph_Current;

}else if((Yph_Current>Rph_Current)&&(Yph_Current>Bph_Current))
{
Max_Current=Yph_Current;

}else if((Bph_Current>Rph_Current)&&(Bph_Current>Yph_Current))
{
Max_Current=Bph_Current;

}else if((Yph_Current<Rph_Current)&&(Yph_Current<Bph_Current))
{
Min_Current=Yph_Current; 

}else if((Rph_Current<Yph_Current)&&(Rph_Current<Bph_Current))
{
Min_Current=Rph_Current; 
} 

else if((Bph_Current<Yph_Current)&&(Bph_Current<Rph_Current))
{
Min_Current=Bph_Current; 
}

Max_Deviation=Max_Current-Min_Current;

Percentage_Unblance=(int)((Max_Deviation/Imean_Current)*100);

if(Percentage_Unblance>Unbalance_limit)
{
CRUnblance_Flag=1;
}else
{
CRUnblance_Flag=0; 
}





}

I think this will not work. You should make 2 separate if sequences, one to determine Max_current and the other one for Min_current.

What is the criterion to know the phase in which the unbalance occured ?

Wouldn't something like this any better?

const uint16_t unbalance_limit_precent=10;
uint16_t RYB_arr[3];
uint16_t I_Average, I_unbalance, temp ;
uint_8t CRUnblance_Flag=0;

//put in the 3 phase currents  in whatever order you want
RYB_arr[0] = Rph_Current; //the actual readings here into the array. dont really need an extra variable
RYB_arr[1] = Yph_Current;
RYB_arr[2] = Bph_Current;

//sort the array in ascending order
for(uint8_t i=0;i<3;i++)
{
	for(uint8_t j=i+1;j<3;j++)
	{
	   if( RYB_arr[i] > RYB_arr[j])
	   {
		  tmp = RYB_arr[i];
		  RYB_arr[i] = RYB_arr[j];
		  RYB_arr[j] = tmp;
	   }
	}
}

I_Average = (RYB_arr[0]+RYB_arr[1]+RYB_arr[2])/3;
I_unbalance = (RYB_arr[2]-RYB_arr[0])/I_Average; //RYB_arr[2]: Max Phase Current, RYB_arr[0]: Min Phase Current

if((I_unbalance/100)>unbalance_limit_precent) //I_unbalance converted into percent
{
	CRUnblance_Flag=1;
}else
{
	CRUnblance_Flag=0;
}

AJITnayak:
But I wanted to know in which two phase unbalance has occurred. can someone guide me here

what should be the nominal current in a balanced situation? u can use that to then identify you unbalances phases

Strictly speaking in an unbalance situation (assuming all 3 phase currents can be adjusted), ALL 3 phases are unbalanced and to balance them would mean to make them shift toward the average calculated current.