1406577354
BISECTION
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% fx = function value at root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0,error('no sign change'),end
if nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;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,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr; fx = func(xr, varargin{:});
Musa Maulana
1406577354
function
[root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;
Musa Maulana
1406577354
function [q,ea,iter]=romberg(func,a,b,es,maxit,varargin)
% romberg: Romberg integration quadrature
% q = romberg(func,a,b,es,maxit,p1,p2,...):
% Romberg integration.
% input:
% func = name of function to be integrated
% a, b = integration limits
% es = desired relative error (default = 0.000001%)
% maxit = maximum allowable iterations (default = 30)
% pl,p2,... = additional parameters used by func
% output:
% q = integral estimate
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4||isempty(es), es=0.000001;end
if nargin<5|isempty(maxit), maxit=50;end
n = 1;
I(1,1) = trap(func,a,b,n,varargin{:});
iter = 0;
while iter<maxit
iter = iter+l;
n = 2^iter;
I(iter+l,l) = trap(func,a,b,n,varargin{:});
for k = 2:iter+l
j = 2+iter-k;
I(j,k) = (4^(k-1)*I(j+1,k-1)-I(j,k-1))/(4^(k-1)-1);
end
ea = abs((I(1,iter+l)-I(2,iter))/I(1,iter+l))*100;
if ea<=es, break; end
end
q = I(1,iter+l);
Musa Maulana
1406577354
function I = trap(func,a,b,n,varargin)
% trap: composite trapezoidal rule quadrature
% I = trap(func,a,b,n,pl,p2,...):
% composite trapezoidal rule
% input:
% func = name of function to be integrated
% a, b = integration limits
% n = number of segments (default = 100)
% pl,p2,... = additional parameters used by func
% output:
% I = integral estimate
if nargin<3,error('at least 3 input arguments required'),end
if ~(b>a),error('upper bound must be greater than lower'),end
if nargin<4|isempty(n),n=100;end
x = a; h = (b - a)/n;
s=func(a,varargin{:});
for i = 1 : n-1
x = x + h;
s = s + 2*func(x,varargin{:});
end
s = s + func(b,varargin{:});
I = (b - a) * s/(2*n);
function x = Tridiag(e,f,g,r)
% Tridiag: Tridiagonal equation solver banded system
% x = Tridiag(e,f,g,r): Tridiagonal system solver.
% input:
% e = subdiagonal vector
% f = diagonal vector
% g = superdiagonal vector
% r = right hand side vector
% output:
% x = solution vector
n=length(f);
% forward elimination
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
% back substitution
x(n) = r(n)/f(n);
for k = n-1:-1:1
x(k) = (r(k)-g(k)*x(k+1))/f(k);
end
Musa Maulana
1406577354
Use the graphical approach to determine the mass of the bungee jumper with a drag coefficient
of 0.25 kg/m to have a velocity of 36 m/s after 4 s of free fall. Note: The acceleration of gravity
is 9.81 m/s2.
clear all;
clc;
cd = 0.25; g = 9.81; v = 36; t = 4;
mp = linspace(50,200);
fp = sqrt(g*mp/cd).*tanh(sqrt(g*cd./mp)*t)-v;
plot(mp,fp), grid
Musa Maulana
1406577354
Linear algebraic equations can arise when modeling distributed systems. For example, Fig. 9.7
shows a long, thin rod positioned between two walls that are held at constant temperatures.
Heat flows through the rod as well as between the rod and the surrounding air. For the steady-
state case, a differential equation based on heat conservation can be written for such a system
as
where T = temperature (C), x = distance along the rod (m), h_ = a heat transfer coefficient
between the rod and the surrounding air (m2), and Ta = the air temperature (C). Given values
for the parameters, forcing functions, and boundary conditions, calculus can be used to develop
an analytical solution. For example, if h_ = 0.01, Ta = 20, T(0) = 40, and T(10) = 200, the
solution is
T = 73.4523e0.1x 53.4523e0.1x + 20
Although it provided a solution here, calculus does not work for all such problems. In such
instances, numerical methods provide a valuable alternative. In this case study, we will use
finite differences to transform this differential equation into a tridiagonal system of linear
algebraic equations which can be readily solved using the numerical methods described in this
chapter.
Musa Maulana
1406577354
Use the M-file function from Fig. 6.7 to determine the mass of the bungee jumper with a drag
coefficient of 0.25 kg/m to have a velocity of 36 m/s after 4 s of free fall. The acceleration of
gravity is 9.81 m/s2.
To apply the Newton-Raphson method, the derivative of this function must be evaluated with
respect to the unknown, m:
Use MATLAB to solve the bungee jumper problem described at the beginning of this
chapter. The parameters for the problem are
common problem in electrical engineering involves determining the currents and voltages at
various locations in resistor circuits. These problems are solved using Kirchhoffs current and
voltage rules. The current (or point) rule states that the algebraic sum of all currents entering
a node must be zero, or
i = 0
where all current entering the node is considered positive in sign. The current rule is an
application of the principle of conservation of charge.
The voltage (or loop) rule specifies that the algebraic sum of the potential differences (i.e.,
voltage changes) in any loop must equal zero. For a resistor circuit, this is expressed as
iR = 0
where is the emf (electromotive force) of the voltage sources, and R is the resistance of any
resistors on the loop. Note that the second term derives from Ohm.s law, which states that the
voltage drop across an ideal resistor is equal to the product of the current and the resistance.
Kirchhoff.s voltage rule is an expression of the conservation of energy.
Jawaban:
Given these assumptions, Kirchhoff.s current rule is applied at each node to yield
or, substituting the resistances from Fig. 8.9 and bringing constants to the right-hand side,
Determine the distance fallen by the free-falling bungee jumper in the first 3 s by evaluating
the integral of Eq. (19.3).
For this example, assume the following parameter values: g = 9.81 m/s2,m = 68.1 kg, and cd =
0.25 kg/m. Note that the exact value of the integral can be computed with Eq. (19.4) as
41.94805. Eq. 19.4:
z = f (x, y)
where z = elevation, x = distance measured along the east-west axis, and y = distance
measured along the north-south axis.
For this example, the partial derivatives provide the slopes in the directions of the axes.
However, if you were mountain climbing, you would probably be much more interested in
determining the direction of the maximum slope. If we think of the two partial derivatives as
component vectors, the answer is provided very neatly by
where f is referred to as the gradient of f. This vector, which represents the steepest slope,
has a magnitude
and a direction
Now suppose that we generate a grid of points in the x-y plane and used the foregoing equations
to draw the gradient vector at each point. The result would be a field of arrows indicating the
Musa Maulana
1406577354
steepest route to the peak from any point. Conversely, if we plotted the negative of the gradient,
it would indicate how a ball would travel as it rolled downhill from any point.
Such graphical representations are so useful that MATLAB has a special function, called
quiver, to create such plots. A simple representation of its syntax is
quiver(x,y,u,v)
where x and y are matrices containing the position coordinates and u and v are matrices
containing the partial derivatives. The following example demonstrates the use of quiver to
visualize a field.
Employ the gradient function to determine to partial derivatives for the following two-
dimensional function:
clear all;
clc;
f=@(x,y) y-x-2*x.^2-2.*x.*y-y.^2;
[x,y]=meshgrid(-2:.25:0, 1:.25:3);
z=f(x,y);
[fx,fy]=gradient(z,0.25);
cs=contour(x,y,z);clabel(cs);
hold on
quiver(x,y,-fx,-fy);
hold off
Musa Maulana
1406577354
Musa Maulana
1406577354
Use Euler.s method to integrate y = 4e 0.5y from t = 0 to 4 with a step size of 1. The initial
condition at t = 0 is y = 2. Note that the exact solution can be determined analytically as
Determine the position and velocity of a bungee jumper with the following parameters: L = 30
m, g = 9.81 m/s2, m = 68.1 kg, cd = 0.25 kg/m, k = 40 N/m, and = 8 N s/m. Perform the
computation from t = 0 to 50 s and assume that the initial conditions are x(0) = v(0) = 0.
Use the graphical approach to determine the mass of the bungee jumper with a drag coefficient
of 0.25 kg/m to have a velocity of 36 m/s after 4 s of free fall. Note: The acceleration of gravity
is 9.81 m/s2. Estimated that the root fell between 140 and 150 kg.
Thus, a result of m = 142.74 kg is obtained after 21 iterations with an approximate relative error
of a = 0.00005345%, and a function value close to zero.