0% found this document useful (0 votes)
109 views8 pages

Komnum UAS

1. The document discusses various numerical methods in MATLAB including bisection, Newton-Raphson, Gauss-Seidel, Romberg integration, and trapezoidal rule. 2. It provides code examples and explanations for each method. 3. The document also includes exercises to use Romberg integration and trapezoidal rule to evaluate several integrals.

Uploaded by

Rusli Nurdin
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)
109 views8 pages

Komnum UAS

1. The document discusses various numerical methods in MATLAB including bisection, Newton-Raphson, Gauss-Seidel, Romberg integration, and trapezoidal rule. 2. It provides code examples and explanations for each method. 3. The document also includes exercises to use Romberg integration and trapezoidal rule to evaluate several integrals.

Uploaded by

Rusli Nurdin
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/ 8

Rusli Nudin (1506674886)

Komputasi Numerik - 02

A. MATERI MATLAB

1. BISECTION (hal. 139)


EDITOR
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{:});

COMMAND
>> fm=@(m) sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
>> [mass fx ea iter]=bisect(fm,40,200)
mass =
142.74
fx =
4.6089e-007
ea =
5.345e-005
iter =
21
2. NEWTRAPH (Hal. 161)
EDITOR
Rusli Nudin (1506674886)
Komputasi Numerik - 02

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;

COMMAND
>> y = @m sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
>> dy = @m 1/2*sqrt(9.81/(m*0.25))*tanh((9.81*0.25/m)^(1/2)*4)-
9.81/(2*m)*sech(sqrt(9.81*0.25/m)*4)^2;
>> newtraph(y,dy,140,0.00001)
ans =
142.7376
3. GAUSS-SEIDEL (Hal. 289)
EDITOR
function x = GaussSeidel(A,b,es,maxit)
% GaussSeidel: Gauss Seidel method
% x = GaussSeidel(A,b): Gauss Seidel without relaxation
% input:
% A = coefficient matrix
% b = right hand side vector
% es = stop criterion (default = 0.00001%)
% maxit = max iterations (default = 50)
% output:
% x = solution vector
if nargin<2,error('at least 2 input arguments required'),end
if nargin<4|isempty(maxit),maxit=50;end
if nargin<3|isempty(es),es=0.00001;end
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
C = A;
for i = 1:n
C(i,i) = 0;
Rusli Nudin (1506674886)
Komputasi Numerik - 02

x(i) = 0;
end
x = x';
for i = 1:n
C(i,1:n) = C(i,1:n)/A(i,i);
End
for i = 1:n
d(i) = b(i)/A(i,i);
end
iter = 0;
while (1)
xold = x;
for i = 1:n
x(i) = d(i)-C(i,:)*x;
if x(i) ~= 0
ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
end
end
iter = iter+1;
if max(ea)<=es | iter >= maxit, break, end
end

COMMAND
>> K = [150 -100 0;-100 150 -50;0 -50 50]

K =

150 -100 0
-100 150 -50
0 -50 50

>> mg = [588.6; 686.7; 784.8]

mg =

1.0e+02 *

5.886000000000000
6.867000000000001
7.848000000000000

>> GaussSeidel(K,mg,0.00001,100)

ans =

41.201988193557234
55.916986225816771
71.612986225816769
4. ROMBERG (Hal. 503)
EDITOR
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:
Rusli Nudin (1506674886)
Komputasi Numerik - 02

% 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=1000000;end
n = 1;
I(1,1) = trap(func,a,b,n,varargin{:});
iter = 0;
while iter<maxit
iter = iter+1;
n = 2^iter;
I(iter+1,1) = trap(func,a,b,n,varargin{:});
for k = 2:iter+1
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+1)-I(2,iter))/I(1,iter+1))*100;
if ea<=es, break; end
end
q = I(1,iter+1);

COMMAND
>> f=@(x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5;
>> romberg(f,0,0.8)
ans =
1.6405
5. TRAP (Hal. 474)
EDITOR
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=1000000;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{:});
Rusli Nudin (1506674886)
Komputasi Numerik - 02

I = (b - a) * s/(2*n);

COMMAND
>> v=@(t) sqrt(9.81*68.1/0.25)*tanh(sqrt(9.81*0.25/68.1)*t)
v =
@(t) sqrt(9.81*68.1/0.25)*tanh(sqrt(9.81*0.25/68.1)*t)

format long
>> trap(v,0,3,5)
ans =
41.86992959072735

>> trap(v,0,3,10000)
x =
41.94804999917528
Rusli Nudin (1506674886)
Komputasi Numerik - 02

B. TUGAS 19.2-19.4 (Menggunakan Metode Romberg dan Trap)


19.2 Evaluate this following integral
4
∫ (1 − 𝑒 −𝑥 ) 𝑑𝑥
0

TRAP ROMBERG
Rusli Nudin (1506674886)
Komputasi Numerik - 02

19.3 Evaluate this following integral

𝜋/2
∫ (8 + 4 cos 𝑥) 𝑑𝑥
0

TRAP ROMBERG
Rusli Nudin (1506674886)
Komputasi Numerik - 02

19.4 Evaluate this following integral


4
∫ (1 − 𝑥 − 4𝑥 3 + 2𝑥 5 ) 𝑑𝑥
−2

TRAP ROMBERG

You might also like