kill(all);

Maxima : intro computation ,base and acid, 'part 14,version 1.0'
peter.vlasschaert@gmail.com,18/12/2018

load packages :
------------------------

load(lrats)$
load(facexp)$

convert:
-----------

log10(x):=log(x)/log(10);

definition : pH,pOH
================

p1: pH(x)   :=-log10(x)$
p1a:pOH(x):=-log10(x)$

dissociation : aq :
===============

kw:10^(-14);

quadratic equation :  s1*x^2+s2*x+s3= 0
=================

quadra2(s1,s2,s3,aa):= block([x1,x2,disc], disc: sqrt(s2^2-4*s1*s3),
                                                     x1:(-s2+disc)/(2*s1),
                                                     x2:(-s2-disc)/(2*s1),
                                                     aa:[x1,x2])$  

rem:solution of quadratic equation ,only pos solution have a physical meaning.

Mixture :Weak acid (n=1): HB + Polyprotic Weak acid (n=2) : H2A
======================================================
rem : HnA => n=2 => H2A

example : HAc , A=Ac,acetic acid (ned : azijnzuur)
pka = -log10(ka)

reaction : H2A+H(2)O       ⇔ H(3)O(+) +HA(-)           ....  '1a'   ,K_a1
reaction : HA(-)+H(2)O      ⇔ H(3)O(+) + A(2-)           ....  '1b'  ,K_a2
reaction : HB +H(2)O         ⇔ H(3)O(+) + B(-)            ....   '2'    ,K_a
reaction : H(2)O+ H(2)O    ⇔ H(3)O(+) + OH(-)         ....   '3'    ,K_w
--------------------------------------------------------------------------------------------
rem:H(3)O(+) =H, A(2-) = A,HA(-) = HA, B(-) = B ,OH(-) = OH  
====================================================
rem : kw = [OH(-)]*[H(+)]
====================================================
---------------------------------------------------------------------------------------------
K_ij  = dissociation constant   , " i = symbol , j = number  "
---------------------------------------------------------------------------------------------
i = a, a=acid
i = b ,b=base
====================================================

equilibrium:  =>    '1a' and '1b' and '2'  and '3'

definition : pKa
============

p2:pKa(x)   :=-log10(x)$

Dibasic weak acid (n=2)
-----------------------------------

definition : K_a1, K_a2      .... '2a'
===================

p2a:K_a1 = ((H)*(HA))/(H2A);
p2b:K_a2 = ((H)*(A))/(HA);
p2c: p2a*p2b;

concentration : H2A     ...'2b'
=================

p2d:C_H2A=(H2A)+(HA)+(A);

distribution functions : α(0),α(1),α(2)
==============================
HnA  : α(0),α(1),α(2), ...α(n)
-------------------------------------------------------------------------------------------
see: Maxima : intro computation ,base and acid, 'appendix 2'
,(Distribution diagram,acid,poly) = > Formula
-------------------------------------------------------------------------------------------

distribution : part '1' (definition)
------------------------------------------------

p2e1:α(0) = (H2A)/lhs(p2d);
p2e2:α(1) = (HA)/lhs(p2d);
p2e3:α(2) = (A)/lhs(p2d);

distribution : part '2' (only function H,K_ij)
---------------------------------------------------------------

p2f:H^2+K_a1*H +K_a1*K_a2;
p2f1: α(0) = (part(p2f,3))/p2f;
p2f2: α(1) = (part(p2f,2))/p2f;
p2f3: α(2) = (part(p2f,1))/p2f;

info : H(n)A ( 'distribution function')
==============================

n=2 , H^2+K_a1*H+K_a1*K_a2 .
n=3 , H^3 +K_a1*H^2+K_a1*K_a2*H +K_a1*K_a2*K_a3 .

Monobasic weak acid(n=1)
----------------------------------------

definition : K_a       .... '3a'
=============

p3a:K_a = ((H)*(B))/(HB);

concentration : HB   .... '3b'
================

p3b:C_HB=(HB)+(B);

charge balance: all species in the solution
===================================
-----------------------------------------------------------------
[H(+)]  = [OH(-)] +1*[HA(-)] +2*[A(2-)] +1*[B(-)]
-----------------------------------------------------------------

p4:H = OH + HA+2*A+B;

find : {OH,HA,B,A}
===============
===============

rem : K_w : w = aq. 'see above'
==========================

p4a:K_w =(OH)*(H);

p4b:solve(p4a,OH);

 =================================================================

find  B: for 'p4'
============

p5:solve(p3b,HB);

p5a:subst(p5,p3a);

p5b:solve(p5a,B);

find HA,A : for 'p4'
===============

p6a:solve(p2e2,HA);

p6b:solve(p2e3,A);

result 1: charge balance
=====================

p7:subst([p4b[1],p5b[1],p6a[1],p6b[1]],p4);

result 2: charge balance
=====================

p7a:subst([p2f2,p2f3],p7);

find polynomial :{H,K_a1,K_a2,K_w,C_H2A,C_HB}.
===========================================

p8:lhs(p7a) -rhs(p7a) = 0;

p8a:ratsimp(p8)$

p8b:num(lhs(p8a)) = 0;

find coef polynomial : H^5,H^4,H^3,H^2,H,constant
===========================================

p9a:ratcoef(lhs(p8b),H^5);
p9b:ratcoef(lhs(p8b),H^4);
p9c:ratcoef(lhs(p8b),H^3);
p9d:ratcoef(lhs(p8b),H^2);
p9e:ratcoef(lhs(p8b),H);
p9f:subst([H=0],lhs(p8b));

p10:p9a*H^5+p9b*H^4+p9c*H^3+p9d*H^2+p9e*H+p9f=0;

=>  find zero's from polynomial 'p10'

solve : newton raphson method to find values H
=========================================

1e) first approximation  :  acid solution : [OH(-)]  <<  [H(+)]
===============================================

p11:ev(p7a,K_w=0);

p11a:lhs(p11) -rhs(p11) = 0;

p11b:num(ratsimp(lhs(p11a)))=rhs(p11a);

p11c:expand(p11b);

p11d:factorout(p11c,[K_a*K_a1*K_a2]);

find coef polynomial :H^4,H^3,H^2,H,constant
=======================================

p12a:ratcoef(lhs(p11d),H^4);
p12b:ratcoef(lhs(p11d),H^3);
p12c:ratcoef(lhs(p11d),H^2);
p12d:ratcoef(lhs(p11d),H);
p12e:subst([H=0],lhs(p11d));

rem :K_a*K_a1*K_a2  very small  = 0 , give a second approximation. (constant term =0)
========================================================================

p13:p12a*H^4+p12b*H^3+p12c*H^2+p12d*H = 0;

p13a:factor(p13);

p13b:part(lhs(p13a),2) = rhs(p13a);

find coef polynomial :H^3,H^2,H,constant
=======================================

p14a:ratcoef(lhs(p13b),H^3);
p14b:ratcoef(lhs(p13b),H^2);
p14c:ratcoef(lhs(p13b),H);
p14d:subst([H=0],lhs(p13b));

thrid order polynomial : start value , for solving 'p10' : fifth order polynomial.
================================================================

p15:p14a*H^3+p14b*H^2+p14c*H+p14d=rhs(p13b);

example : values ,try to draw the function .

or  :  Menu : Equations : roots ' real roots'.

internet:
=======

Acid Strength and the Acid Dissociation Constant (Ka) = K_a
===================================================

https://chem.libretexts.org/Textbook_Maps/General_Chemistry
/Map%3A_A_Molecular_Approach_(Tro)/
16%3A_Acids_and_Bases/
16.04%3A_Acid_Strength_and_the_Acid_Dissociation_Constant_(Ka)

Table  16.4.1 : Values of  Ka ,  pKa ,  Kb , and  pKb  for Selected Acids ( HA  and Their Conjugate Bases ( A− )

acid  : HA                               K_a              , pK_a     
----------------------------------------------------------------------------------------
nitric   acid  :HNO3              2.3×10^1       ,−1.37       => strong acid
acetic acid  : CH3COOH   1.7×10^(-5)    ,4.76         => weak acid

given : concentration,C_HA = 0.01 M 
rem : removes memory :  C_HA,K_a

------------------------------------------------------------------------------------------------