kill(all)$

Maxima : intro computation ,base and acid, 'part 17a,version 1.0'
peter.vlasschaert@gmail.com,29/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)$

rem : loga(x) = y ⇔ x = 10^a

p1c:float(log10(100));

p1c: H = 10^(-ph)$

distribution function  : function H, Ka(i),Kw  i=1,2,..,n
=============================================
Ka(i) , dissociation constant  of  (a)cid.
---------------------------------------------------------------------------------
[H3O+] = [H+] = H
[OH(-)]  = [OH-] = OH
Kw = 10^(-14)        ,  dissociation constant  water ( = aq)
---------------------------------------------------------------------------------

find definition for : α(i)  , i = 0,1,2,..,n
------------------------------------------------------

HA  , mono , acid   => monoprotic acid    (n=1)
===

p2a:α(0) = H/(H+Ka(1));
p2b:α(1) =Ka(1)/(H+Ka(1));

p3c:C_HA =HA+A;

rem : [HA] = HA , [A] = A(-)

H2A  , di   , acid   => diprotic acid     (n=2)
====

p3:H^2+Ka(1)*H + Ka(1)*Ka(2);

p3a:α(0) =H^2/p3;
p3b:α(1)=Ka(1)*H/p3;
p3c:α(2)=(Ka(1)*Ka(2))/p3;

p3d:C_H2A =H2A + HA+A;

rem : [H2A] = H2A,[HA(-)] = HA,[A(2-)] = A

p4:s(n) :=sum(H^(n-k)*product(Ka(j),j,1,k),k,1,n);

rem : relation between sum and product connect with 'k'.
-------------------------------------------------------------------------------------
k : control of H  control of Ka.

p4a:s(1);

p4b:s(2);

result : highst power is missing.
===========================

denominator :HA,H2A
---------------------------------

HA :  n=1
===

p5a:H+s(1);

H2A: n=2
====

p5b:H^2+s(2);

 How construct : general equation for  : αn(i,n,H)
========================================

numerator : HA,H2A, ......,HnA
---------------------------------------------

connect factor and product with variable. 'i'
-----------------------------------------------------------------
rem : factor : powers of H

ss(n,i):= H^(n-i)*product(Ka(j),j,1,i);

ratio : numerator/denominator => αn(i,n,H)
----------------------------------------------------------------

αn(i,n,H) :=ss(n,i)/(H^n+s(n));

We want to construct : dissociation curve 'phosphoric acid'
==================================================
HnA ,  example :  'A= PO4' , n=3
------------------------------------------------------------------------------------------
given : Ka(1) = 5.9*10^(-3)
              Ka(2) = 6.15*10^(-8)
              Ka(3) = 4.8*10^(-13)
------------------------------------------------------------------------------------------

1e) Distribution values : αn(i,n,H), i=0,1,..n
===================================

α(0 ) : 
-------

p6a:H3A/C_H3A = αn(0,3,H) ;

α(1) : [H2A(-)] = H2A
------

p6b:H2A/C_H3A =αn(1,3,H);

α(2) :[HA(2-)] = HA
-------

p6c:HA/C_H3A =αn(2,3,H);

α(3) :[A(3-)] = A
-------

p6d:A/C_H3A =αn(3,3,H);

check for : sum(αn(i,n,H)) =1
=========

example : n=3 , H3A
============

p6:'sum(αn(i,3,H),i,0,3) =ratsimp(sum(αn(i,3,H),i,0,3));

2e) Degree of formation.
====================

definition : n_bar (  literature )
==============

The average of proton bounded by 'A' ( = ion form).

rem :  example : A = PO4(3-)
rem : n_bar = f(HA,H2A,H3A)

p7:n_bar = (HA+2*H2A+3*H3A)/C_H3A;

n:3;

p7a:lhs(p7) = sum((n-i)*'αn(i,n,H),i,0,n-1);

p7b:expand(p7a);

relation : αn(i,3,H)/C_H3A   ⇔ α(i) ;  i=0,1,..,n
----------------------------------------------------------------

l:makelist('αn(i,3,H) = α(i),i,0,n);

p7c:lratsubst(l,p7b);

general relation : sum (α(i),i,0,n-1) = 1
-------------------------------------------------------

p7d:sum(l[i],i,1,n+1);

p7e: ratsimp(ev(lhs(p7d),nouns)) = rhs(p7d);

relation between n_bar and general relation above.
-----------------------------------------------------------------------------

p7f:solve(p7e,α(0));

p7g:ratsimp(subst(p7f,p7c));

p7h:p7g-n;

p7i:p7h*(-1);

3e) Degree of formation and αn(i,n,H), i=0,1,..,n
=======================================

p8:expand(p7);

p8a:[p6a,p6b,p6c,p6d]$

p8b:lratsubst(p8a,p8);

4e) Dissociation curve for HnA  relation : n-n_bar = g(ph) 
================================================
ph = -log10(H)  ⇔  H = 10^(-ph)
--------------------------------------------------------------------------------------

ll:makelist( α(i) = 'αn(i,3,H) ,i,0,n);

ll:append(ll,[H=10^(-ph)]);

p9: ev(subst(ll,rhs(p7i)),nouns);

definition , H3A , dissociation curve. (n=3)
--------------------------------------------------------------

disscurv(Ka(1),Ka(2),Ka(3),ph):='p9;  error ? 
How to solve :
============
=> disscurv(Ka1,Ka2,Ka3,ph):='p9a

lll:[Ka(1) = Ka1,Ka(2)=Ka2,Ka(3)=Ka3];

p9a:subst(lll,p9);

p9b:disscurv(Ka1,Ka2,Ka3,ph):= '' p9a;

5e) Draw dissociation curve  H3A.
=============================

Ka1,Ka2,Ka3: phosphoric acid
----------------------------------------------

Ka1 :5.9*10^(-3);
Ka2: 6.15*10^(-8);
Ka3:4.8*10^(-13);

wxplot2d([disscurv(Ka1,Ka2,Ka3,ph),1,2,3,0],[ph,1,14],[y,-1,3.1],[ylabel,"3-nbar"],[title,"dissociation curve  H3PO4 "],
[legend,"3-nbar","[H2PO4(-)]","[HPO4(2-)]","[PO4(3-)]","[H3PO4] "]);

info graph
=========

values : y values : 
===============
-------------------------------------------------------------------------------------------------------------------------
                                    0 => black   line         =       [H3PO4]                    
                                    1 => red       line         =       [H2PO4(-)]   => NaH2PO4 (ampholyt)
                                    2 => green  line         =       [HPO4(2-)]   =>  Na2HPO4 (ampholyt)                
                                    3 => purple line         =       [PO4(3-)]
-------------------------------------------------------------------------------------------------------------------------
three successive dissociations: H3PO4 ( = blue line)
values : x values :
===============
ph : [H(+)] = 10^(-ph)

see:
Maxima : intro computation ,base and acid, 'part :17,version 1.0',(ampholyt,polyprotic acid)
----------------------------------------------------------------------------------------------------------------------------------------
result : 0.1M NaH2PO4  => ph = 4.73 ( value intersection (blue,red))
               0.1M Na2HPO4  => ph = 9.72 ( value intersection (blue,green))
-----------------------------------------------------------------------------------------------------------------------------------------

rem:
===

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

relation : K_a(i) = Ka(i), i=1,2,.,n
                  n_bar = nbar
                  3-nbar,n_bar : see definition above

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