Anda di halaman 1dari 19

Musa Maulana

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

NEWTON RAPHSON NEWTRAPH

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

ROMBERG INTEGRATION ALGORITHM ROMBERG

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

TRAPEZOIDAL RULE TRAP

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);

TRIDIAGONAL SYSTEM TRIDIAG

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

EULERS METHOD EULODE

function [t,y] = eulode(dydt,tspan,y0,h,varargin)


% eulode: Euler ODE solver
% [t,y] = eulode(dydt,tspan,y0,h,p1,p2,...):
% uses Euler's method to integrate an ODE
% input:
% dydt = name of the M-file that evaluates the ODE
% tspan = [ti, tf] where ti and tf = initial and
% final values of independent variable
% y0 = initial value of dependent variable
% h = step size
% p1,p2,... = additional parameters used by dydt
% output:
% t = vector of independent variable
% y = vector of solution for dependent variable
if nargin<4,error('at least 4 input arguments required'),end
ti = tspan(1);tf = tspan(2);
if ~(tf>ti),error('upper limit must be greater than lower'),end
t = (ti:h:tf)'; n = length(t);
% if necessary, add an additional value of t
% so that range goes from t = ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
end
y = y0*ones(n,1); %preallocate y to improve efficiency
for i = 1:n-1 %implement Euler's method
y(i+1) = y(i) + dydt(t(i),y(i),varargin{:})*(t(i+1)-t(i));
end
Musa Maulana
1406577354

Latihan BUNGEE JUMPER

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.

Jawaban dengan M-file:

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

Latihan HEATED ROD

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

Jawaban dengan MATLAB:


Musa Maulana
1406577354

Latihan BUNGEE JUMPER NEWTRAPH

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.

The function to be evaluated is

To apply the Newton-Raphson method, the derivative of this function must be evaluated with
respect to the unknown, m:

Jawaban dengan MATLAB:


Musa Maulana
1406577354

Latihan BUNGEE JUMPER MATRIKS

Use MATLAB to solve the bungee jumper problem described at the beginning of this
chapter. The parameters for the problem are

Jawaban dengan MATLAB:


Musa Maulana
1406577354

Latihan CURRENT AND VOLTAGE MATRIX

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

i12 + i52 + i32 = 0


i65 i52 i54 = 0
i43 i32 = 0
i54 i43 = 0
Application of the voltage rule to each of the two loops gives

i54R54 i43R43 i32R32 + i52R52 = 0


i65R65 i52R52 + i12R12 200 = 0
Musa Maulana
1406577354

or, substituting the resistances from Fig. 8.9 and bringing constants to the right-hand side,

15i54 5i43 10i32 + 10i52 = 0


20i65 10i52 + 5i12 = 200
Therefore, the problem amounts to solving six equations with six unknown currents. These
equations can be expressed in matrix form as

Jawaban dengan MATLAB:


Musa Maulana
1406577354

Latihan BUNGEE JUMPER TRAP

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:

Jawaban dengan MATLAB:


Musa Maulana
1406577354

Latihan MOUNTAIN VIEW

we used mountain elevation as an example of a two-dimensional function. We can represent


such a function mathematically as

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

where = the angle measured counterclockwise from the x axis.

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:

f (x, y) = y x 2x2 2xy y2

from x = 2 to 2 and y = 1 to 3. Then use quiver to superimpose a vector field on a contour


plot of the function.

Jawaban dengan M-file:

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

Latihan EULERS METHOD

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

Jawaban dengan MATLAB:

Latihan BUNGEE JUMPER WITH CORD

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.

Jawaban dengan M-file dan MATLAB:

function dydt = bungee(t,y,L,cd,m,k,gamma)


g = 9.81;
cord = 0;
if y(1) > L %determine if the cord exerts a force
cord = k/m*(y(1)-L)+gamma/m*y(2);
end
dydt = [y(2); g - sign(y(2))*cd/m*y(2)^2 - cord];
Musa Maulana
1406577354
Musa Maulana
1406577354

Latihan BUNGEE JUMPER BISECT

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.

Jawaban dengan MATLAB:

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.

Anda mungkin juga menyukai