




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Year LAB No.
Casabuena, Jhonel D. Ignacio, Robbie S. Lazona, Princess Emee E. Navarra, Casey S. Vicente, Marielle Joi G.
CIEN 2221 Numerical Subject Date Topic Name Subject Code /
: Introduction to MATLAB Program Solutions to CE Problems : _______________
Laboratory 0 3 Student No. Colegio de Muntinlupa Department of Civil Engineering
1 unit – Computer or Laptop with installed MATLAB software for each student
function [root, fx, Re, Cd, ea, iter] = bisect(func, xl, xu, es, maxit, varargin) %% Find the root of function using Bisection Method % Input: % func = given function to determine the root % xl = lower initial guess % xu = upper initial guess % es = stopping criterion (%) % maxit = maximum number of iterations % Output: % root = approximate value of the root of function % fx = corresponding function value of the root % Re = Reynolds number % Cd = Drag Coefficient % ea = approximate relative error (%) % iter = number of iterations %% Constants: rho = 1000; % fluid density (kg/m^3) mu = 0.001; % dynamic viscosity (N*s/m^2) D = 0.001; % particle diameter (m) %% Initializations: if nargin < 3, error('At least 3 input required'), end if nargin < 4 || isempty(es), es = 0.05; end if nargin < 5 || isempty(maxit), maxit = 100; end fl = func(xl, varargin{:}); fu = func(xu, varargin{:}); sign = fl * fu; if sign <= 0, error('No Sign Change'), end iter = 1; ea = 100; xr = xl; fprintf('\n'); fprintf('root function value reynolds number Drag Coefficient approx. rel. error iteration\n'); %% Bisection Method Iteration: while (1) xrold = xr; xr = (xl + xu)/2; iter = iter + 1; if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end test = func(xl, varargin{:}) * func(xr, varargin{:}); if test < 0 xu = xr; elseif test > 0 xl = xr; else ea = 0;
if xr ~= 0 ea = abs((xr - xrold)/xr) * 100; end if func(xl, varargin{:}) * func(xr, varargin{:}) < 0 xu = xr; elseif func(xl, varargin{:}) * func(xr, varargin{:}) > 0 xl = xr; else ea = 0; end if ea <= es || iter >= maxit; break, end end %% Resulting Calculation: root = xr; fx = func(xr, varargin{:}); Re = (rho * D * root) / mu; Cd = 24/Re + 3/sqrt(Re) + 0.34; fprintf('%14.10f %14.10f %14.10f %14.10f %12.8f %2d\n',root, fx, Re, Cd, ea, iter); fprintf('\n');
function [vr, fv, Re, cd, ea, iter] = newtraph(vr, es, maxit, varargin) %% uses Newton-Raphson method to find the root of func % Input: % func = name of function % dfunc = name of derivative of function % vr = initial guess % es = stopping criterion (%) % maxit = maximum allowable iterations (default = 100) % g = gravity % D = the particle's diameter % mu = the fluid's dynamic viscosity % rho = the fluid's density % rhos = the particle's density % Output: % v = real root % ea = approximate relative error (%) % iter = number of iterations (%) % fv = settling velocity of particle % Re = Reynolds number % cd = drag coefficient %% Constants for the function g = 9.81; % gravity (m/s^2) D = 0.001; % particle's diameter (m) mu = 0.001; % fluid's dynamic viscosity (N*s/m^2) rho = 1000; % fluid's density (kg/m^3) rhos = 2650; % particle's density (kg/m^3) %% Initialization: if nargin < 3, error('at least 3 input arguments reguired'), end if nargin < 4 || isempty(es), es = 0.05; end if nargin < 5 || isempty(maxit), maxit = 100; end iter = 0;
fprintf('\n'); fprintf('root function value Reynolds number Drag coefficient approximate relative error iterations\n'); %% Applying Newton-Raphson method: v = vr; while (1) Re = (rho * D * v)/mu; if Re <= 0, error('invalid Reynolds number'); end func = (sqrt((4gRemu(rhos - rho) / ((72/Re + 9/sqrt(Re) + 1.02) ... Dv))) - v); dfunc = func(4gRe(rhos - rho)(72/Re + 9/sqrt(Re) + 1.02) ... D) / ((72/Re + 9/sqrt(Re) + 1.02)Dv)^2; vrold = vr; fval = func; dfval = dfunc; vr= vrold - (fval / dfval); iter = iter + 1; if vr ~= 0, ea = abs((vr - vrold)/vr) * 100; end if ea <= es || iter >= maxit, break, end end %% Resulting Calculation fv = func; Re = (rho * D * v)/mu; cd = 24/Re + 4/sqrt(Re) + 0.34; fprintf('%4.2f %5.2f %5.2f %5.2f %3.2f %2d\n', vr, fv, Re, cd, ea, iter); fprintf('\n');
function [root, fx, Re, Cd, ea, iter] = secant( xl, xu, es, maxit, varargin) % secant: root location zeroes using Secant Method % [root, fx, Re, Cd, ea, iter] = secant(func, xl, xu, es, maxit, ...): % uses secant method to find the root of func and computes Re and Cd % Input: % func = name of function % xl = lower initial guess % xu = upper initial guess % es = desired relative error % maxit = maximum allowable iterations % rho = fluid density (kg/m^3) % rhos = particle density (kg/m^3) % mu = dynamic viscosity (N·s/m^2) % D = particle diameter (m) % Output: % root = root estimate (velocity) % fx = function value at root % ea = approximate relative error (%)