OK, I tried to do this accounting for K1,k2,k3 and the other ks but there must be a mistake somewhere since I am not getting the correct pH with my algorithm. I am not including the activity coefficient so maybe there lies the reason for discrepancy. It is quite different though pH=4 predicted...