Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 f(xu)*f(xl) is lower than zero or not. xu =xr; the true, sets upper limit as 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions