Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a matlab code that calculate error and root by using false position metho

ID: 3732143 • Letter: I

Question

I need a matlab code that calculate error and root by using false position method like below code.
Can you write a matlab code by using below code with the same function (f = fpH(pH,pCO2) ) that is defined below code . Yet, you must change below code by using different matlab functions since new code must be different and it give the same output. Thanks.

falsepos(@fpH,2,12,1e-8,11,315)
function f = fpH(pH,pCO2) %We defined our function.
K1=10^-6.3;K2=10^-10.3;Kw=10^-14;
KH=10^-1.46;
H=10^-pH;
f=K1/(1e6*H)*KH*pCO2+2*K2*K1/(1e6*H)*KH*pCO2+Kw/H-H;
end
function [root,fx,ea,iter]=falsepos(func,xl,xu,es,maxit,varargin)
%{
func argument is the function we want to work on.
xl is the lower limit.
xu is the upper limit.
es is the desired relative error.
maxit is the maximum number of iterations that you want program to take.
varargin is a cell of arguments. You can put pretty much any number of
arguments and after the first five defined inputs, program will take all
the other variables and put them in a cell for the purpose of using them
later on. In this example we can use this cell to calculate function value
if it has three variables such as y axis and z axis.
%}
if nargin <5|isempty(maxit), maxit=50; end % If the number of arguments are lower than 5, it sets the maximum iteration to 50.
if nargin <4|isempty(es), es=0.0001; end % If the number of arguments are lower than 5, it sets the desired relative error to 50.
if nargin <3, error(sprintf('Please enter at least 3 arguments. Preferably, 1- A function 2- Lower x limit 3- Upper x limit')), end % If the user...
%uses less than 3 arguments, an error message will display indicating that inputs are insufficient and advise player to use certain variables.
iter=0; % Sets the iteration number to zero so that we can use it later to calculate the number of iterations
xr=xl; % Assigns the first root as the lower limit value.
ea=100; % Initial approxiamte relative error is 100.
if func(xl,varargin{:})*func(xu,varargin{:})>0, error('The limits you entered either don''t have the root or is root.'),end % Investiges if the limits are...
%roots or a function contain a root between the numbers.
while(1) % Opens a unlimited loop.
iter=iter+1; % Iteration number
xrold=xr; % Previous estimated root.
xr=xu-func(xu,varargin{:})*(xl-xu)/(func(xl,varargin{:})-func(xu,varargin{:})); % Current estimated root.
if xr~=0,ea=abs((xr-xrold)/xr)*100;end % If the current estimated root isn't zero it calculates the approximate relative error.
if func(xl,varargin{:})*func(xr,varargin{:})<0 % Made a if statement about if f(xu)*f(xl) is lower than zero or not.
xu=xr; % If the if statement is true, sets the upper limit as the current estimated root.
elseif func(xl,varargin{:})*func(xr,varargin{:})>0 % Made a if statement about if f(xu)*f(xl) is higher than zero or not.
xl=xr; % If the if statement is true, sets the lower limit as the current estimated root.
elseif func(xl,varargin{:})*func(xr,varargin{:})==0 % If the current estimated root is zero, the error is zero.
ea=0; % Sets error to zero.
end
if ea<=es|iter>=maxit,break,end % If desired conditions, approximate relative error is lower than desired relative error or we exceeded maximum iteration number, are satisfied it breaks the loop.
end
sprintf('The estimated root is: %f Approximation error is: %f Number of iterations: %i',xr,ea,iter)%Prints a string with defined parameters.
end

Explanation / Answer

%if there is any problem,please ask in comments

falsepos1(@fpH,2,12,1e-8,11,315);
function f = fpH(pH,pCO2) %We defined our function.

K1=10^-6.3;K2=10^-10.3;Kw=10^-14;

KH=10^-1.46;

H=10^-pH;

f=K1/(1e6*H)*KH*pCO2+2*K2*K1/(1e6*H)*KH*pCO2+Kw/H-H;
end
function [root,i,ea]=falsepos1(f,xL,xR,es,maxit,varargin)
%f is the function whose root is to be found
%xL is the lower limit of range of solution and xR is upper limit
xOld=0;
xNew=100;
i=0;
ea=abs((xNew-xOld)/xNew)*100;
while ea>es
i=i+1;
if i>maxit
i=i-1;
break
end
xOld=xNew;%xOld holds solution calculated in previous iteration
xNew=(xL*f(xR,varargin{:})-xR*f(xL,varargin{:}))/(f(xR,varargin{:})-f(xL,varargin{:}));%new solution calculated by false position formula
if f(xL,varargin{:})*f(xNew,varargin{:})<0
xR=xNew;
else
xL=xNew;
end
  
ea=abs((xNew-xOld)/xNew)*100;
end
root=xNew;
fprintf('Root calculated: %f Number of iterations: %i Approximation Error: %f ',root,i,ea)
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote