% MassCal.m

pkg load symbolic
%calculate solution to reciprocity calibration with syms

syms Zm Ze ToSq ZinL1 ZinL2 ZinL3 ZL1 ZL2 ZL3

ZinL1_Alg = Ze + ToSq/(ZL1 + Zm);
ZinL2_Alg = Ze + ToSq/(ZL2 + Zm);
ZinL3_Alg = Ze + ToSq/(ZL3 + Zm);

% no Ze
EQ1 = simplify(ZinL1_Alg -ZinL2_Alg);
EQ2 = simplify(ZinL2_Alg - ZinL3_Alg);

ToSq_Zm = simplify(solve(EQ1 == (ZinL1-ZinL2),ToSq));
EQ2_Zm = simplify(subs(EQ2,ToSq,ToSq_Zm));
Zm_sol = (solve(EQ2_Zm==(ZinL2-ZinL3),Zm));
ToSq_sol = simplify(subs(ToSq_Zm,Zm,Zm_sol));

ZinL1_Alg2 = subs(ZinL1_Alg,Zm,Zm_sol);
ZinL1_Alg3 = subs(ZinL1_Alg2,ToSq,ToSq_sol);
Ze_sol = simplify(solve(ZinL1_Alg3==ZinL1,Ze));

[nToSq_sol,dToSq_sol] = numden(ToSq_sol);
ToSq_sol = factor(nToSq_sol)/factor(dToSq_sol);
[nZe_sol,dZe_sol] = numden(Ze_sol);
Ze_sol = factor(nZe_sol)/factor(dZe_sol);
[nZm_sol,dZm_sol] = numden(Zm_sol);
Zm_sol = factor(nZm_sol)/factor(dZm_sol);
