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

kill(all)$

load(lrats)$
load(facexp)$

Dilute buffer solutions: given 'ratio = C_HA/C_A'
========================================
[H(+)] or [OH(-)] is not neglible compared to C_HA and C_A

Maxima : intro computation ,base and acid, 'part :7,version 1.0'

start :   'p6a' 
==========

p1:H=(K_a*(OH-H+C_HA))/(-OH+H+C_A);

p1a:factor(solve(p1,K_a))[1];

Solve : 'p1a' for C_A :
--------------------------------

p2:solve(p1a,C_A)[1];

p2a:expand(p2);

find Coef  from C_HA :
---------------------------------

p2b:ratcoef(rhs(p2a),C_HA);

p2c:p2a-C_HA*p2b;

find C_A with respect to ratio , pH ( = -log10(H))
-----------------------------------------------------------------------

lhs : p2c , x =  ?
====================

p3:C_A*x = lhs(p2c);

p3a:solve(p3,x)[1];

p3b:expand(p3a);

rhs : p2c 
====================

p4:factorout(rhs(p2c),H,OH);

p4a:factorsum(p4);

p4b : part(p4a,1);
p4b1:part(p4b,1);
p4b2:part(p4b,2);
p4c : part(p4a,2);

p4d:expand(p4b1/p4c)*p4b2;

combination 'lhs,rhs'
--------------------------------

p5:lhs(p3)= p4d;

p5a:p5/x;

p5b:subst([p3b],p5a);

remove : [OH(-)] = f(H)
---------------------------------

K_w =[OH]*[H]  = 10^(-14)
=====================
rem :    pH+pOH = 14

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

p6a:solve(p6,OH);

final result : C_A:
--------------------------

p5c:subst(p6a,p5b);

def : ratio :  y = C_HA/C_A
---------------------------------------

p7:C_HA/C_A=y;

lhs :
===

p5d:part(rhs(p5c),2);

p5e:lratsubst([p7],p5d);

p5f:expand(p5e);

final result : C_A with ratio
-----------------------------------------

p5g:lhs(p5c) = (part(rhs(p5c),1))/p5f;

We want to make plot : pH(-log10(C_A))
=================================
rem : y = pH , x=-log10(C_A)
-----------------------------------------------------------

acetate-acetic acid buffer (K_a : CH3COOH : 1.76 *10^(-5), y=1)

pH = f(H)

definition : log10(100) = 2

p8:log10(z):=log(z)/log(10.0);

p8a:log10(100.0);

 log10(100)  =2      ⇔ 100= 10^2
---------------------------------------------------
-log10(H)      = pH ⇔  H   = 10^(-pH)
----------------------------------------------------

equimolar : buffer
===============

p9:[K_a = 1.76*10^(-5),K_w=10^(-14),y=1,H=10^(-ph)];

p5h:subst(p9,p5g);

p5i:-log10(p5h);

p5j:rhs(p5i);

plot : pH(-log10(C_A))
==================

p10:wxplot2d([%,-log10(1.76*10^(-5))],[ph,0,10],[plot_format,xmaxima],grid2d)$

p10a: pH = subst([K_a=1.76*10^(-5)],-log10(K_a));

interpretation : 'red line' must be 'vertical'.

---------------------------------------------------------------------------------------
=>  Buffer :            K_a = [H(+)]*C_A/C_HA 
=>  equimolar        K_a = [H(+)]*1
=> pH                      pK_a= -log10(K_a)  = -log10([H(+)]) = pH
---------------------------------------------------------------------------------------
see : 'p10a'
---------------------------------------------------------------------------------------

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

interpretation : 'blue curve'

---------------------------------------------------------------------------------------
x value : ph
y value : p5j
--------------------------------------------------------------------------------------
================================================

list1:makelist(ph,ph,4,7,0.1);

p11:length(list1);

p11a:list1[1];

list2:[];

for i:1 thru p11 do(
list2:endcons(subst([ph=list1[i]],p5j),list2))$

list2;

we want to remove complex values from list:

p12:for i: 1 thru p11 do(
if is(list2[i]> 0) = true then
     return(i));

return : gives first positive value from list2.

list2a : ph
list2b : -log(C_A)

list2a:makelist(list1[i],i,p12,p11);
list2b:makelist(list2[i],i,p12,p11);

list3:[];

for i:1 thru length(list2a) do(
list3:endcons([list2b[i],list2a[i]],list3))$

list3;

wxplot2d([[discrete, list3],[discrete, list3],x],[x,0,10],[y,4,7],
        [style, points, lines,lines], [color, red, blue,black],
        [point_type, asterisk],grid2d,
        [legend, "buffer", "buffer","line"],
        [xlabel, "-log10(C_A)"],
        [ylabel, "pH"],[title,"pH (equimolar (acetic,acetate)),buffer"])$

'--------------------------------------------------------------------------------------------------------------------------------------'