kill(all)$

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

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

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

log plot for weak acid. 'HA'
----------------------------------------
x: ph , y: log(concentration)

reaction
=======

reaction : HA(-)+H(2)O      ⇔ H(3)O(+) + A(2-)           ....  K_a

definition : K_a
-----------------------
K_a = ([H]*[A(-)])/[HA]

p2a:K_a = ((H)*(A))/(HA);

concentration acid: C_HA
---------------------------------------
C_HA = [HA] + [A(-)]

p2b:C_HA = HA+A;
p2c:rhs(p2b) = lhs(p2b);

acid : region :  ph  <  7   ( No reaction)  below  pK_a
===================

p4:subst([A = 0],p2c);

p4a:subst([p4],p2a);

solve : [A(-)]
------------------

p4b:solve(p4a,A)[1];

operator : log or log10
---------------------------------

log : base 'e = 2.7... '

p5a:log(p4b);

log : base '10 '

p5b:log10(p4b);

p5c:log((C_HA*K_a)/H), logexpand=super;

from : definition ph
ph = -log10(H)

p6a:num(rhs(p5b)),logexpand = super;

p6b:denom(rhs(p5b));

p6c:subst([-log(H) =a],p6a)/p6b;

p6d:expand(p6c);

log10(A):
=======

p6e: subst([part(p6d,1)=ph],p6d);

p6f:expand(logcontract(p6e));

base : region :  ph  >  7   (  reaction) above  pK_a
===================
example : HAc + OH(-) ⇔ H2O + Ac (-)

p7:subst([HA = 0],p2c);

p7a:subst([p7],p2a);

solve : [HA]
------------------

p7b:solve(p7a,HA)[1];

operator : log or log10
---------------------------------

log : base 'e = 2.7... '

p8a:log(p7b);

log : base '10 '

p9b:log10(p7b);

p9c:log((C_HA*H)/K_a),logexpand = super;

from : definition ph
ph = -log10(H)

p10a:num(rhs(p9b)),logexpand = super;

p10b:denom(rhs(p9b));

p10c:lratsubst([-log(H) =a],p10a)/p10b;

p10d:expand(p10c);

log10(HA):
=========

p10e:lratsubst([a/p10b = ph],p10d);

p10f:expand(p10e);

p10g:expand(logcontract(p10f));

Draw : log10 plot for HAc: 
======================

given :  HAc, 'CH3COOH'
=====================

p11a:pK_a = 4.76;
p11b:C_HA =0.01;  /* M*/

solve :

pK_a = 4.76  => K_a ?
--------------------------------
-log10(K_a) = 4.76 => log10(K_a) = -4.76

p11c:K_a=10^(-4.76);                                                                                                  

log10(C_HA)  ?
-----------------------

p11d:log10(rhs(p11b)),numer;

acid : region :  ph  <  7   ( No reaction)  below  pK_a
==========================================

log10(conc=A)
-----------------------

f1(pH,K_a,C_HA):=''p6f;

base : region :  ph  >  7   (  reaction) above  pK_a
=========================================

log10(conc=HA)
------------------------

f2(pH,K_a,C_HA):=''p10g;

draw : log10 plot for HAc
=====================

part 1:
---------

x -axes : ph
y-axes  : log10 (concentration)

xmin : 0;
xmax :14;
ymin : -14;
ymax:0;

wxplot2d( [0],
[x,xmin,xmax],[y,ymin,ymax],[same_xy],grid2d,[xlabel,"ph"],[ylabel,"log(conc)"]);

part 2: log10(H),log10(OH)
---------------------------------------

definition : line trough two points
============================
-------------------------------------------------------
p1(x1,y1) , p2(x2,y2)
-------------------------------------------------------
m= (y2-y1)/(x2-x1)
y - y1 = m*(x-x1)  => y = y1+m*(x-x1)
-------------------------------------------------------
rem : y = m *x +n,first order equation
-------------------------------------------------------

twoptline(x1,y1,x2,y2):= y1+((y2-y1)/(x2-x1))*(x-x1);

1e)  log10(H) ,blue,points
-------------------

line : point (14,0) to (0,-14)

s1:twoptline(xmax,0,0,ymin);

2e)  log10(OH),red,points
---------------------

line : point (0,0) to (14,-14)

s2:twoptline(0,0,xmax,ymin);

wxplot2d( [s1,s2],
[x,xmin,xmax],[y,ymin,ymax],[same_xy],[style,[points,0.01,red],[points,0.01,blue]]
    ,grid2d,[xlabel,"ph"],[ylabel,"log(conc)"]);

x -range : ph
y : log(concentration)

s11:subst([x=ph],s1);
s22:subst([x=ph],s2);

draw : log10 [HA] : red line.'( ph > pka)'
================================

q1:f2(ph,rhs(p11c),rhs(p11b)),float,numer;

q1a:subst([ph=x],q1);

piecewise function :
------------------------------

fred(x,pK_a,C_HA,sm1) := if x > pK_a then sm1 else float(log10(C_HA)) $

check : below pk_a = 4.76
================

fred(2.0,4.76,rhs(p11b),s1);

check : above  pk_a = 4.76
================

fred(5.0,4.76,rhs(p11b),s1);

draw : log10 [A] : blue line. '( ph < pka)'
================================

q2:f1(ph,rhs(p11c),rhs(p11b)),float,numer;

q2a:subst([ph=x],q2);

piecewise function :
------------------------------

fblue(x,pK_a,C_HA,sm2) := if x > pK_a then float(log10(C_HA)) else sm2 $

combination graph
================
red   line : [HA]
blue line : [A(-)]
red dot line   :[OH-]
blue dot line :[H3O+]

wxplot2d( [s1,s2,fred(x,4.76,0.01,q1a),fblue(x,4.76,0.01,q2a)],
[x,xmin,xmax],
    [y,ymin,ymax],[same_xy],[style,[points,0.01,red],[points,0.01,blue],[lines,0.5,red],[lines,0.5,blue]]
    ,grid2d,[xlabel,"ph"],[ylabel,"log(conc)"]);

wxplot2d( [s1,s2,fred(x,4.76,0.01,q1a),fblue(x,4.76,0.01,q2a)],
[x,xmin,xmax], [y,ymin,ymax],
    [same_xy],[style,[points,0.01,red],[points,0.01,blue],[lines,0.5,red],[lines,0.5,blue]],
    [title," log10 plot  Conc"],
    grid2d,[xlabel,"ph"],[ylabel,"log(conc)"],
    [legend,false]);

wxplot2d( [s1,s2,fred(x,4.76,0.01,q1a),fblue(x,4.76,0.01,q2a)],
[x,xmin,xmax], [y,ymin,ymax],
[same_xy],[style,[points,0.01,red],[points,0.01,blue],[lines,0.5,red],[lines,0.5,blue]],
[title," log10 plot  Conc"],
grid2d,[xlabel,"ph"],[ylabel,"log(conc)"],
[legend,"OH- red dots","H3O+ blue dots","HA","A(-)"]);

How to use : log10 plot :  'above'
===========================

---------------------------------------------------------------------------------------------------------------
given : ph   ' draw vertical line at that position on the graph ' = 3.8
==============================================================
 find    : concentration of the species, 'intersection 
==============================================================

see : 
====
graphics : 
How to draw lines with parametricplot,vertical,horizontal lines,version 1.0.
==============================================================
rem : example  :  -2 =>  from  y axes  => 0.01 M of conc 
---------------------------------------------------------------------------------------------------------------

xx(t):= 3.8;
yy(t):= t;

v:[parametric,xx(t),yy(t),[t,ymin,ymax]]$

wxplot2d( [s1,s2,fred(x,4.76,0.01,q1a),fblue(x,4.76,0.01,q2a),v],
[x,xmin,xmax], [y,ymin,ymax],[same_xy],
[style,[points,0.01,red],[points,0.01,blue],[lines,0.6,red],[lines,0.6,blue],[lines,0.7,black]],
[title," log10 plot  Conc,example : 'ph=3.8'"],
grid2d,[xlabel,"ph"],[ylabel,"log(conc)"],
[legend,"H3O(+) blue dots","OH(-) red dots","HA","A(-)","ph=3.8"]);

read : intersection ( blackline,blue dot line)   , ph   = [H3O(+)], 3.8   =>    read:  -14 +3.8       = -10.2
            intersection ( blackline,red dot line)     , pOh = 10.2                  =>   read:  -14-(-10.2)   = - 3.8
------------------------------------------------------------------------------------------------------------------------------------------
rem : ph + pOh = 14

control : pH:
===========
x=[H(+)]
before : x^2+x*K_a-K_a*C_HA=0

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])$  

z1:1;
z2:subst([K_a=rhs(p11c)],K_a);
z3:subst([K_a=rhs(p11c),C_HA = 0.01],-K_a*C_HA);

sol:quadra2(z1,z2,z3,aa);

rem : only positive solutions have a physical meaning.

find : ph

q3:ph=float(pH(sol[1]));

rem :  ph from 0.01M HAc

see  : graph: check value  'q3' on the graph
==========

xx1(t):= 3.389;
yy1(t):= t;

vv:[parametric,xx1(t),yy1(t),[t,ymin,ymax]]$

wxplot2d( [s1,s2,fred(x,4.76,0.01,q1a),fblue(x,4.76,0.01,q2a),vv],
[x,xmin,xmax], [y,ymin,ymax],[same_xy],
[style,[points,0.01,red],[points,0.01,blue],[lines,0.6,red],[lines,0.6,blue],[lines,0.7,black]],
[title," log10 plot  Conc,example : 'ph=3.8'"],
grid2d,[xlabel,"ph"],[ylabel,"log(conc)"],
[legend,"H3O(+) blue dots","OH(-) red dots","HA","A(-)","ph=3.389"]);

result : intersection(blue dots,blue line)    =>       ph =3.389 for 0.01M HAc
=================================================================
Why : reaction ?
=================================================================
Answer : no
Ac(-),H(+)
=================================================================

How to find : 0.01M acetate
=================================================================
result :intersection(red dot line 'OH-',red line 'HA'), projection on the 'ph axes'
=> ph above 8.2 for the acetate solution. ( see : graph above.)
Why : reaction ?
=================================================================
OH(-) + HAc ⇔ Ac(-) + H2O
=================================================================

find system point
===============
point below  : -2 'example '
====================================
[HA] = [A-]
C_HA = [HA]+[A(-)]  ' example : 0.01M  C_HA'
=> 
[HA] = [A(-)] = C_HA/2
====================================

r1:float(log10(0.01/2));

value below the point : ph = pK_a
====================================

float(log10(2));

? point :below intersection (blue line, red line), vertical down.(not show here)

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

other method , distribution of  C_HA.
---------------------------------------------------------------------------------------------------------------------------------------------------------------