Not enough input arguments for bisection method function.

1 view (last 30 days)
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;
end
for 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
end
end
Whenever I run, this error displays:
>> bisec
Set year: 1958
Not 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 condition
end
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:
>> bisec
pH is 7.000000 for iteration 1
pH is 4.500000 for iteration 2
pH is 5.750000 for iteration 3
pH is 5.125000 for iteration 4
pH is 5.437500 for iteration 5
pH is 5.593750 for iteration 6
pH is 5.671875 for iteration 7
pH is 5.632813 for iteration 8
pH is 5.613281 for iteration 9
pH is 5.623047 for iteration 10
pH 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.

Accepted Answer

Jan
Jan on 15 Apr 2021
You have defined f() with 2 input:
function y = f(pH,pCO2)
but you call it with 1 input only:
while f(xl)*f(xu)>0
I guess you want:
while f(xl, pCO2) * f(xu, pCO2) > 0

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!