kill(all)$
load(lrats)$

Maxima:How to use : eliminate ' chemical equations solving : titration'
peter.vlasschaert@gmail.com,16/01/2019

load packages
============

load(lrats)$

How to use : Eliminate ?

eq1:x+2=y;
eq2:8*x-y=2;

eqns1:[eq1,eq2];

p1:eliminate(eqns1,[y]);

rem : eq1-eq2 = (x+2)-(8*x-2) =(-7*x+4)
===============================
?  1e)  solve ,y,eq1
?  2e)  solve ,y,eq2
?  3e)   eq1-eq2

eqns2:[eq2,eq1];

p2:eliminate(eqns2,[y]);

rem : eq2-eq1=(8*x-2)-(x+2)=(7*x-4)
===============================
?  1e)  solve ,y,eq2
?  2e)  solve ,y,eq1
?  3e)   eq2-eq1

Theory
======

change , the order , gives different result.
--------------------------------------------------------------
polyprotic acid  : hna  ( n=2 , diprotic acid)
reaction : h2A    ⇔ ha(-) + h(+)    =>  ka_1         '1'
                   ha(-)   ⇔ a(2-) + h(+)    =>  ka_2         '2'
                   h2O    ⇔ oh(-) + h(+)   =>  k_w           '3'
practical
=======
==============================
added : buret  (strong base),          volume = vb,concentration = cb
                  MOH   =>  M(+) + oh(-)                         '4'
               vessel : (weak acid : h2a),volume = va ,concentration = ca
                  h2a :  '1','2','3'
total volume :  v = va+vb (during titration)     'constraint' 
M= Na,K,Li   
 from : 'i' i=1,2,3,4 : result  ( => charge balance)
----------------------------------------------------------------------
 sum(pos:charge)  = sum(neg : charge)
 [M(+)]  +[h(+)] = [oh(-)] + [ha(-)] +2* [a(2-)]
=======================================
=>  cas : (c)omputer (a)lgebra (s)ystem
=======================================
M+ h  = oh +ha +a
-----------------------------------------------------------------------
M = >  cb*vb/v
-----------------------------------------------------------------------
concentration (h2A)
-----------------------------------------------------------------------
CA = [h2a] + [ha(-)]+[a(2-)]
========================================
=>  cas : computer algebra system
========================================
CA = h2a + ha + a
-----------------------------------------------------------------------
CA => ca*va/v
-----------------------------------------------------------------------

k1:ka[1] =h*ha/h2a;
k2:ka[2] = h*a/ha;
kw:k[w] = h*oh;
mass : ca*va/v = h2a+ha+a;
charge :cb*vb/v +1*h = 1*ha+2*a+1*oh;

math : 5 equations and 1 constraint

use : command eliminate  'see above'
===============================

Heqn : eliminate([k1,k2,kw,mass,charge],[h2a,ha,a,oh])[1];

Heqn1:ratsimp(Heqn);

p1:expand(Heqn1);

p2:collectterms(p1,h);

use : constraint   'from above'
=============

p3:subst([v=va+vb],p2);

vb :solve(p3,vb);

rem :  vb = -va , no physical meaning
=> result : first part list 

p4:vb[1];

definition :
=========
ph = -log10(h)    ⇔ h= 10^(-ph)

p5:lratsubst([h =10^(-ph)],rhs(p4));

p6:factor(p5);

definition : vb   
============
input : va,ka_1,ka_2,k_w,ph,cb,ca
---------------------------------------------------
unknown :  ph 
---------------------------------------------------
result        :  vb
============================
curve : ph = ph(vb)

p7:vb : p6;

math :
=====

rem : other calculation ,subst back  10^(-ph)  =>  h , polynomial.