Sie sind auf Seite 1von 16

TUGAS KOMPUTASI NUMERIK

LAPORAN PENGGUNAAN MATLAB

NAMA : CALVIN SIMON ANDREAS LUMBAN GAOL

NPM : 1606876714

KOMPUTASI NUMERIK - 01

DEPARTEMEN TEKNIK METALURGI DAN MATERIAL

FAKULTAS TEKNIK

UNIVERSITAS INDONESIA

DEPOK
Minggu 1

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.000000001;end
if nargin<5|isempty(maxit), maxit=164;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{:});

1. 𝑋 4 + 3𝑋 3 − 2𝑋 2 + 7
>> fa=@(a) (a^4)+3*(a^3)-2*(a^2)+7

fa =

@(a)(a^4)+3*(a^3)-2*(a^2)+7

>> format long

>> [root,fx,ea,iter]=bisect(fa,-5,-2)
root =

-3.409974038171640

fx =

-1.264410798285098e-10

ea =

6.401184465983192e-10

iter =

37

sinh⁡(3𝑥 2 +1)
2.
tanh⁡(2𝑥 2 +3)

>> fb=@(b) (sinh(3*(b^2)+1))/(tanh(2*(b^3)+3))

fb =

@(b)(sinh(3*(b^2)+1))/(tanh(2*(b^3)+3))

>> [root,fx,ea,iter]=bisect(fb,-20,10)

Error using bisect (line 18)

no sign change

(tidak ada solusi baik biseksi ataupun newton raphson)


NEWTON-RAPHSON
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.00000001;end
if nargin<5|isempty(maxit),maxit=1000;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;

Nomor 1

>> b=@(b) b^4+3*(b^3)-2*(b^2)+7

b=

@(b)b^4+3*(b^3)-2*(b^2)+7

>> db=@(b) 4*(b^3)+9*(b^2)-4*b

db =

@(b)4*(b^3)+9*(b^2)-4*b

>> newtraph(b,db,-5,0.00001)

ans =

-3.409974038174776
>> newtraph(b,db,-2,0.00001)

ans =

-1.290083270378526

Nomor 2

>> da=@(a) (6*a*cosh(3*a^2 + 1))/tanh(2*a^2 + 3) + (4*a*sinh(3*a^2 +


1)*(tanh(2*a^2 + 3)^2 - 1))/tanh(2*a^2 + 3)^2

da =

@(a)(6*a*cosh(3*a^2+1))/tanh(2*a^2+3)+(4*a*sinh(3*a^2+1)*(tanh(2*a^2
+3)^2-1))/tanh(2*a^2+3)^2

>> newtraph(a,da,-2,0.000001)

ans =

NaN

Tidak ada solusi


Minggu 2
Fzero dan polyval
6.13

>> x = fzero(@(x) -1.1+0.99403+((1.671*(10^-4))*(x))+((9.7215*(10^-


8))*(x^2))-((9.5838*(10^-11))*(x^3))+((1.9520*(10^-14))*(x^4)),0)

x = 5.440875376555090e+02

polyval

>> a=[1.9520*(10^-14) -9.5838*(10^-11) 9.7215*(10^-8) 1.671*(10^-4)


0.99403]

a =

Columns 1 through 4

0.000000000000020 -0.000000000095838 0.000000097215000


0.000167100000000

Column 5

0.994030000000000

>> x = fzero(@(x) polyval(a,x)-1.1,0)

x =

5.440875376555089e+02

6.14

>> x = fzero(@(x) (((x)/(1-x))*(sqrt((2*3)/(2+x))))-0.05,0)

x = 0.028249441148471
Gauss Seidel
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;
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

>> GaussSeidel(A,B,0.0000001,1000)

ans =

41.201999871908995

55.916999850560501

71.612999850560499
12.3
Script GaussSeidel
>> A = [ 10 2 -1 ; -3 -6 2 ; 1 1 5 ]

A =

10 2 -1

-3 -6 2

1 1 5

>> B = [ 27 ; -61.5 ; -21.5 ]

B =

27.000000000000000

-61.500000000000000

-21.500000000000000

>> GaussSeidel (A,B,0.00000001,1000)

ans =

0.500000000002772

8.000000000002121

-6.000000000000979

Tanpa Script

>> [X]= [A]\[B]

X =

0.500000000000000

7.999999999999999

-6.000000000000000
12.5 – Script GaussSeidel

>> A = [ 15 -3 -1 ; -3 18 -6 ; -4 -1 12 ]

A =

15 -3 -1

-3 18 -6

-4 -1 12

>> B = [ 3800 ; 1200 ; 2350 ]

B =

3800

1200

2350

>> GaussSeidel (A,B,0.00000001,1000)

ans =

1.0e+02 *

3.202072538831900

2.272020725364619

3.215025906724352
Tanpa script

>> [X]= [A]\ [B]

X =

1.0e+02 *

3.202072538860104

2.272020725388602

3.215025906735752
Minggu 3
Metode trapezoid
>> format long

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

>> g=@(x) x^2-1

g=

@(x)x^2-1

>> trap(g,0,5,5)

ans =

37.500000000000000

>> trap(g,0,5,10)

ans =

36.875000000000000

>> trap(g,0,5,100)

ans =

36.668749999999903
>> trap(g,0,5,1000)

ans =

36.666687499998794

>> trap(g,0,5,10000)

ans =

36.666666875004090
Metode 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+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);

>> romberg(g,0,5,0.000001,5)

ans =

36.666666666666664

>> romberg(g,0,5,0.000001,10)

ans =

36.666666666666664

>> romberg(g,0,5,0.000001,1000000)

ans =

36.666666666666664
19.2

>> A=@(x) 1-exp(-x)

A=

@(x)1-exp(-x)

>> trap(A,0,4,5)

ans =

2.966509232940782

>> trap(A,0,4,10)

ans =

3.005261285967605

>> trap(A,0,4,100)

ans =

3.018184751130887

>> trap(A,0,4,10000)

ans =

3.018315625799555

>> romberg(A,0,4,0.000001,10)

ans =

3.018315638883668
19.3

>> B=@(x) 8+(4*cos(x))

B =

@(x)8+(4*cos(x))

>> trap(B,0,(pi/2),5)

ans =

16.533417689378080

>> trap(B,0,(pi/2),10)

ans =

16.558142559776606

>> trap(B,0,(pi/2),100)

ans =

16.566288367317608

>> trap(B,0,(pi/2),1000)

ans =

16.566369791892136

>> trap(B,0,(pi/2),10000)
ans =

16.566370606134114

>> romberg(B,0,(pi/2),0.00001,10)

ans =

16.566370646935255

>> romberg(B,0,(pi/2),0.00001,5)

ans =

16.566370646935255
19.4

C=

@(x)1-x-4*(x^3)+2*(x^5)

>> trap(C,-2,4,5)

ans =

1.370572800000000e+03

>> trap(C,-2,4,10)

ans =

1.171420800000000e+03

>> trap(C,-2,4,100)

ans =

1.104676774080006e+03

>> trap(C,-2,4,1000)

ans =

1.104006767997325e+03

>> trap(C,-2,4,10000)

ans =

1.104000067679446e+03

>> romberg(C,-2,4,0.000001,10)

ans =

1104