[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

MATHLAB

The document contains MATLAB function files for a case study involving root-finding methods. It includes a function for calculating equilibrium equations related to pH and pCO2, as well as implementations for the Bisection, Regula Falsi, Newton-Raphson, and Secant methods. Each method is designed to find the root of a specified function with adjustable parameters for error tolerance and maximum iterations.

Uploaded by

Rheyann Paz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

MATHLAB

The document contains MATLAB function files for a case study involving root-finding methods. It includes a function for calculating equilibrium equations related to pH and pCO2, as well as implementations for the Bisection, Regula Falsi, Newton-Raphson, and Secant methods. Each method is designed to find the root of a specified function with adjustable parameters for error tolerance and maximum iterations.

Uploaded by

Rheyann Paz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Let's start by creating the function file for the case study:

% File name: fpH.m

function f = fpH(pH, pCO2)

% Constants

K = 10^-146;

K1 = 10^-6.3;

K2 = 10^-10.3;

Kw = 10^-14;

Ka = 10^-1;

% Equilibrium equations

H = 10^(-pH);

CO2 = 10^(-pCO2);

% Nonlinear system of equations

f(1) = H^2 / (H^2 + K * H + K * K2) - CO2;

f(2) = (K * K2 * H^2) / (H^2 + K * H + K * K2) - Ka * (Kw / H);

% Column vector

f = f';

end

Now, let's create MATLAB function files for each of the root-finding methods (bisection, regula falsi,
Newton-Raphson, and Secant):

Bisection Method (bisect.m):


% File name: bisect.m

function [root, iterations, ea] = bisect(func, xl, xu, es, maxit)

if nargin < 5

maxit = 100;

end

if nargin < 4

es = 0.0001;

end

iter = 0;

xr = xl;

ea = 100;

while (1)

xrold = xr;

xr = (xl + xu) / 2;

iter = iter + 1;

if xr ~= 0

ea = abs((xr - xrold) / xr) * 100;

end

test = func(xl) * func(xr);

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea <= es || iter >= maxit

break

end

end

root = xr;

iterations = iter;

end

Regula Falsi Method (falsepos.m):


% File name: falsepos.m

function [root, iterations, ea] = falsepos(func, xl, xu, es, maxit)

if nargin < 5

maxit = 100;

end

if nargin < 4

es = 0.0001;

end

iter = 0;

xr = xl;

ea = 100;

while (1)

xrold = xr;

xr = xu - (func(xu) * (xl - xu)) / (func(xl) - func(xu));

iter = iter + 1;

if xr ~= 0

ea = abs((xr - xrold) / xr) * 100;

end

test = func(xl) * func(xr);

if test < 0

xu = xr;

elseif test > 0

xl = xr;

else

ea = 0;

end

if ea <= es || iter >= maxit

break

end

end

root = xr;

iterations = iter;

end

Newton-Raphson Method (newtrap.m):


% File name: newtrap.m

function [root, iterations, ea] = newtrap(func, x0, es, maxit)

if nargin < 4

maxit = 100;

end

if nargin < 3

es = 0.0001;

end

iter = 0;

xr = x0;

ea = 100;

while (1)

xrold = xr;

xr = xrold - func(xrold) / (func(xrold + 0.0001) - func(xrold)) * 0.0001;

iter = iter + 1;

if xr ~= 0

ea = abs((xr - xrold) / xr) * 100;

end

if ea <= es || iter >= maxit

break

end

end

root = xr;

iterations = iter;

end

Secant Method (secantm.m):


% File name: secantm.m

function [root, iterations, ea] = secantm(func, x0, x1, es, maxit)

if nargin < 5

maxit = 100;

end

if nargin < 4

es = 0.0001;

end

iter = 0;

ea = 100;

while (1)

x2 = x1 - (func(x1) * (x1 - x0)) / (func(x1) - func(x0));

iter = iter + 1;

if x2 ~= 0

ea = abs((x2 - x1) / x2) * 100;

end

if ea <= es || iter >= maxit

break

end

x0 = x1;

x1 = x2;

end

root = x2;

iterations = iter;

end

You might also like