Given Information:
The system offivesimultaneousnonlinear equations,
K1=106[H+][HCO3−]KHpCO2K2=[H+][CO32−][HCO3−]
cT=KHpCO2106+[HCO3−]+[CO32−]Kw=[H+][OH−]0=[HCO3−]+2[CO32−]+[OH−]−[H+]
Here, KH is Henry’s constant, cT is total inorganic carbon, [HCO3−] is bicarbonate, Kw,K1, and K2 are equilibrium constants, [H+] is hydrogen ion, [CO32−] is carbonate, and [OH−] is hydroxyl ion.
The values of constants KH,K1,K2, and Kw are as follows:
KH=10−1,46K1=10−6.3K2=10−10.3Kw=10−14
Consider, the value of partial pressure of CO2 in 1958 as,
pCO2=315 ppm
Formula used:
For system of n simultaneous nonlinear equations formulated as,
f1(x1,x2,...,xn)=0f2(x1,x2,...,xn)=0...fn−1(x1,x2,...,xn)=0fn(x1,x2,...,xn)=0
From Newton-Raphson method, for mth equation,
fm,i+1=fm,i+(x1,i+1−x1,i)∂fm,i∂x1+(x2,i+1−x2,i)∂fm,i∂x2+...+(xn,i+1−xn,i)∂fm,i∂xn
Here, m represents the equation and the second subscript represents the value of the function at current value i or at next value i+1.
Further, the above equation is rewritten as,
−fm,i+x1,i∂fm,i∂x1+x2,i∂fm,i∂x2+...+xn,i∂fm,i∂xn=x1,i+1∂fm,i∂x1+x2,i+1∂fm,i∂x2+...+xn,i+1∂fm,i∂xn
Write the matrix equation for partial derivatives.
[Z]=[∂f1,i∂x1∂f1,i∂x2...∂f1,i∂xn∂f2,i∂x1∂f2,i∂x2...∂f2,i∂xn.........∂fn,i∂x1∂fn,i∂x2...∂fn,i∂xn]
Initial and the final values are expressed as,
{Xi}T=[x1,ix2,i...xn,i]
And,
{Xi+1}T=[x1,i+1x2,i+1...xn,i+1]
Write the function values at i as,
{Fi}T=|f1,if2,i...fn,i|
Thus, simplify the partial derivative equation as,
[Z]{Xi+1}=−{Fi}+[Z]{Xi}
The above equation can then be solved iteratively to obtain the solutions to the system of nonlinear equations.
Calculation:
Apply a technique based on Newton-Raphson method to solve the system of nonlinear equations.
Use the following MATLAB code toimplement Newton Raphson method and solve the given system of simultaneous nonlinear equations.
function Code_9_21P()
format short g
x0=[10^-7;10^-7;1e-5;2e-6;5e-11];
KH=10^-1.46;K1=10^-6.3;K2=10^-10.3;Kw=10^-14;pco2=315;
[x,f,e_a,itr]=newtmult(@jfrain,x0,[],[],KH,K1,K2,Kw,pco2);
fprintf('H: %2.4e\n',x(1))
fprintf('OH: %2.4e\n',x(2))
fprintf('cT: %2.4e\n',x(3))
fprintf('HCO3: %2.4e\n',x(4))
fprintf('CO3: %2.4e\n\n',x(5))
fprintf('Maximum relative error = %2.4g percent\n',e_a)
fprintf('Number of iterations = %d',itr)
pH=-log10(x(1));
fprintf('\npH: %2.4f\n',pH)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[x,f,e_a,itr]=newtmult(func,x0,es,maxit,varargin)
% check for minimum input required
ifnargin<2,error('Provide at least 2 arguments for input'), end
ifnargin<3||isempty(es),es=0.0001;end
ifnargin<4||isempty(maxit),maxit=50;end
% initialize
itr=0; x=x0;
while(1)
[J,f]=func(x,varargin{:});
dx=J\f;
x=x-dx;
% increase counter in every run of the loop
itr=itr+1;
e_a=100*max(abs(dx./x));
% stopping criteria
ifitr>=maxit||e_a<=es, break, end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[J,f]=jfrain(x,varargin)
del=0.00001;
% determine the elements of J matrix
df1dx1=(f1(x(1)+del*x(1),x(2),x(3),x(4),x(5),varargin{:})-...
f1(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(1));
%
df1dx2=(f1(x(1),x(2)+del*x(2),x(3),x(4),x(5),varargin{:})-...
f1(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(2));
%
df1dx3=(f1(x(1),x(2),x(3)+del*x(3),x(4),x(5),varargin{:})-...
f1(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(3));
%
df1dx4=(f1(x(1),x(2),x(3),x(4)+del*x(4),x(5),varargin{:})-...
f1(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(4));
%
df1dx5=(f1(x(1),x(2),x(3),x(4),x(5)+del*x(5),varargin{:})-...
f1(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(5));
%
df2dx1=(f2(x(1)+del*x(1),x(2),x(3),x(4),x(5),varargin{:})-...
f2(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(1));
%
df2dx2=(f2(x(1),x(2)+del*x(2),x(3),x(4),x(5),varargin{:})-...
f2(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(2));
%
df2dx3=(f2(x(1),x(2),x(3)+del*x(3),x(4),x(5),varargin{:})-...
f2(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(3));
%
df2dx4=(f2(x(1),x(2),x(3),x(4)+del*x(4),x(5),varargin{:})-...
f2(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(4));
%
df2dx5=(f2(x(1),x(2),x(3),x(4),x(5)+del*x(5),varargin{:})-...
f2(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(5));
%
df3dx1=(f3(x(1)+del*x(1),x(2),x(3),x(4),x(5),varargin{:})-...
f3(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(1));
%
df3dx2=(f3(x(1),x(2)+del*x(2),x(3),x(4),x(5),varargin{:})-...
f3(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(2));
%
df3dx3=(f3(x(1),x(2),x(3)+del*x(3),x(4),x(5),varargin{:})-...
f3(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(3));
%
df3dx4=(f3(x(1),x(2),x(3),x(4)+del*x(4),x(5),varargin{:})-...
f3(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(4));
%
df3dx5=(f3(x(1),x(2),x(3),x(4),x(5)+del*x(5),varargin{:})-...
f3(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(5));
%
df4dx1=(f4(x(1)+del*x(1),x(2),x(3),x(4),x(5),varargin{:})-...
f4(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(1));
%
df4dx2=(f4(x(1),x(2)+del*x(2),x(3),x(4),x(5),varargin{:})-...
f4(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(2));
%
df4dx3=(f4(x(1),x(2),x(3)+del*x(3),x(4),x(5),varargin{:})-...
f4(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(3));
%
df4dx4=(f4(x(1),x(2),x(3),x(4)+del*x(4),x(5),varargin{:})-...
f4(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(4));
%
df4dx5=(f4(x(1),x(2),x(3),x(4),x(5)+del*x(5),varargin{:})-...
f4(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(5));
%
df5dx1=(f5(x(1)+del*x(1),x(2),x(3),x(4),x(5),varargin{:})-...
f5(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(1));
%
df5dx2=(f5(x(1),x(2)+del*x(2),x(3),x(4),x(5),varargin{:})-...
f5(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(2));
%
df5dx3=(f5(x(1),x(2),x(3)+del*x(3),x(4),x(5),varargin{:})-...
f5(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(3));
%
df5dx4=(f5(x(1),x(2),x(3),x(4)+del*x(4),x(5),varargin{:})-...
f5(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(4));
%
df5dx5=(f5(x(1),x(2),x(3),x(4),x(5)+del*x(5),varargin{:})-...
f5(x(1),x(2),x(3),x(4),x(5),varargin{:}))/(del*x(5));
% define the J matrix
J=[df1dx1 df1dx2 df1dx3 df1dx4 df1dx5
df2dx1 df2dx2 df2dx3 df2dx4 df2dx5
df3dx1 df3dx2 df3dx3 df3dx4 df3dx5
df4dx1 df4dx2 df4dx3 df4dx4 df4dx5
df5dx1 df5dx2 df5dx3 df5dx4 df5dx5];
% determine the elements of f vector.
f11=f1(x(1),x(2),x(3),x(4),x(5),varargin{:});
f22=f2(x(1),x(2),x(3),x(4),x(5),varargin{:});
f33=f3(x(1),x(2),x(3),x(4),x(5),varargin{:});
f44=f4(x(1),x(2),x(3),x(4),x(5),varargin{:});
f55=f5(x(1),x(2),x(3),x(4),x(5),varargin{:});
% define the elements of f vector.
f=[f11;f22;f33;f44;f55];
end
% define the five non-linear equations
% first equation
function f=f1(H,OH,cT,HCO3,CO3,KH,K1,K2,Kw,pco2)
f =1e6*H*HCO3/KH/pco2-K1;
end
% second equation
function f=f2(H,OH,cT,HCO3,CO3,KH,K1,K2,Kw,pco2)
f = H*CO3/HCO3-K2;
end
% third equation
function f=f3(H,OH,cT,HCO3,CO3,KH,K1,K2,Kw,pco2)
f = H*OH-Kw;
end
% fourth equation
function f=f4(H,OH,cT,HCO3,CO3,KH,K1,K2,Kw,pco2)
f = KH*pco2/1e6+HCO3+CO3-cT;
end
% fifth equation
function f=f5(H,OH,cT,HCO3,CO3,KH,K1,K2,Kw,pco2)
f = HCO3+2*CO3+OH-H;
end
Execute the above code to obtain the solutions as,
H: 2.3419e-06
OH: 4.2701e-09
cT: 1.3260e-05
HCO3: 2.3375e-06
CO3: 5.0025e-11
Maximum relative error = 1.265e-12 percent
Number of iterations = 6
pH: 5.6304
Hence, the solution to the system of five nonlinear equations is [H+]=2.3419×10−6, [OH−]=4.2701×10−9, cT=1.3260×10−5, [HCO3−]=2.3375×10−6, [CO32−]=5.0025×10−11, and the pH of the rainwater is 5.6304.