I have problems for determining variable "t" for "pCO2" value.
This is the function of pH:
function y = f(pH,pCO2) % Function for calculating pH (Seperate f.m file)
k1= 10 ^(-6.3);k2= 10^(-10.3);kw= 10^(-14);kh= 10^(-1.46);H=10^(-pH);y= k1/(1e6*H)*kh*pCO2 + 2*k2*k1/ (1e6*H)*kh*pCO2 + kw/H - H;end
This is the bisection method function to determine the pH.
function bisec %Seperate bisec.m file
i=0; % Initial condition
xl=2; % Lower limit
xu=12; % Upper limit
Ead=0.005; % Relative percent error
n=log2((xu-xl)/Ead); % Number of max iterations
n=ceil(n); % Round up to closest number
prompt = 'Set year: ';t = input(prompt);pCO2 = 0.012226*(t-1983)^2+1.418542*(t-1983)+ 342.38309; % pCO2 equation with respect to years.
while f(xl)*f(xu)>0 % Setting fail condition
disp('Bisection method will not work.'\n) xl=2; xu=12;endfor i=1:n % Starting iterations
xmid =(xl+xu)/2; % Bisection method
if f(xmid)*f(xu)<0 % Setting limits
xl = xmid; else xu = xmid; end fprintf('pH is %f for iteration %d\n',xmid,i); % Display the result
endend
Whenever I run, this error displays:
>> bisecSet year: 1958Not enough input arguments.Error in f (line 7)y= k1/(1e6*H)*kh*pCO2 + 2*k2*k1/ (1e6*H)*kh*pCO2 + kw/H - H;Error in bisec (line 14)while f(xl)*f(xu)>0 % Setting fail conditionend
If I set a constant value for pCO2 in f file, like pCO2=315; and remove
prompt = 'Set year: ';t = input(prompt);pCO2 = 0.012226*(t-1983)^2+1.418542*(t-1983)+ 342.38309; % pCO2 equation with respect to years.
lines from bisection file (lines 9-11), it works:
>> bisecpH is 7.000000 for iteration 1pH is 4.500000 for iteration 2pH is 5.750000 for iteration 3pH is 5.125000 for iteration 4pH is 5.437500 for iteration 5pH is 5.593750 for iteration 6pH is 5.671875 for iteration 7pH is 5.632813 for iteration 8pH is 5.613281 for iteration 9pH is 5.623047 for iteration 10pH is 5.627930 for iteration 11
What should I do for this code to ask a year from the user to calculate pCO2 and determine the pH with bisection method?
Thanks.
Best Answer