Anda di halaman 1dari 18

TUGAS 2

ANALISIS DAN KOMPUTASI NUMERIS


(Analisis Mfile Newton Raphson dan Penyelesaian Studi Kasus Teknik Kimia Dengan
Menggunakan Metode Newton Raphson)

Dosen Mata Kuliah:


Muhammad Hanif, S.T., M.T.

Nama Kelompok :
Amelia Virgiyani Sofyan

(1215041006)

Fahmi Alzie Putra

(1215041018)

Fita Desti Senja

(1215041022)

Sakha Abdussalam

(1215041043)

Dita Synthauli Evaniya

(1215041058)

TEKNIK KIMIA
FAKULTAS TEKNIK
UNIVERSITAS LAMPUNG
2016

Menganalisis M-File Newton Raphson Method Fig 12.4


function [x,f,ea,iter]=newtmult(func,x0,es,maxit,varargin)
Kalimat di atas merupakan Keterangan nama fungsi dari Newton Raphson Method. Fungsi
yang mengandung output x, f, Error, dan iterasi membutuhkan multiequation Newton
Raphson yang mengandung input fungsi, x awal, persentase Error relatif spesifik, dan
Varargin atau opsi dari fungsi-fungsi.
x = Nilai dari Jacobian
f = Nilai dari persamaan fungsi
ea = perkiraan persentase error relatif
es = persentase error relatif spesifik dengan nilai x yang sudah konstan
x0 = nila tebakan awal
maxit = jumlah maksimum iterasi
%
%
%
%

newtmult: Newton-Raphson root zeroes nonlinear systems


[x,f,ea,iter]=newtmult(func,x0,es,maxit,p1,p2,...):
uses the Newton-Raphson method to find the roots of
a system of nonlinear equations

Multiequation Newton Raphson menggunakan persamaan single-equation, yaitu:

)=

Persamaan di atas diperoleh dari First-order Taylor dengan f(xi+1) = 0

)= ( )+( )+(

) ( )

) =0 sehingga, menjadi persamaan di atas (blok kuning). Persamaan ini digunakan

untuk menentukan nilai yang mendekati akar sejati dari sistem persamaan nonlinier agar
memudahkan proses iterasi.
%
%
%
%
%
%

input:
func = name of function that returns f and J
x0 = initial guess
es = desired percent relative error (default = 0.0001%)
maxit = maximum allowable iterations (default = 50)
p1,p2,... = additional parameters used by function

Berikut data yang harus diinput:

Func= nama fungsi yang menunjukkan nilai f dan j


X0 = tebakan awal
es = persentase error yang diinginkan (biasanya hanya mencapai 0,0001 %
maxit = jumlah iterasi maksimum yang diizinkan (biasanya hanya 50 iterasi)
p1, p2 = parameter tambahan
%
%
%
%
%

output:
x = vector of roots
f = vector of functions evaluated at roots
ea = approximate percent relative error (%)
iter = number of iterations

Berikut data output yangakan dihasilkan:


x = vector akar sejati, yang akan selalu diiterasi hingga memperoleh error yang diinginkan
f= nilai vektor hasil dari turunan fungsi yang akan dievaluasi dengan nilai akar sejati
ea = nilai error yang akan selalu dihasilkan setelah memasukkan nilai x baru.
Iter = jumlah iterasi

if nargin<2,error('at least 2 input arguments required'),end


if nargin<3|isempty(es),es=0.0001;end
if nargin<4|isempty(maxit),maxit=50;end
Keterangan di atas merupakan syarat kondisi argument-argumen yang dibutuhkan.
a. Jika Input kurang dari 2 maka perhitungan akan error.
b. Jika input kurang dari 3 dan nilai error sudah mencapai 0,0001 maka perhitungan
akan dihentikan
c. Jika input kurang dari 4 dan jumlah iterasi sudah 50 kali maka perhitungan
dihentikan

iter = 0;
x=x0;
iterasi awal yaitu 0
x sama dengan x0

while (1)
[J,f]=func(x,varargin{:});
Matriks [J,f] berisikan x dan opsi fungsi-fungsi

dx=J\f; Turunan x merupakan invers J dikali f


x=x-dx; x merupakan hasil dari single-equation Newton raphson
iter = iter + 1; jumlah iter selalu bertambah 1
ea=100*max(abs(dx./x)); menentukan nilai error
if iter>=maxit|ea<=es, break, end
end
Jika jumlah lebih besar atau sama dengan jumlah maksimum iterasi yang ditentukan, dan ea
kurang dari atau sama dengan es maka perhitungan dapat dihentikan.

Kasus 12.3
Persamaan sistem nonlinear sering ditemukan dalam kasus-kasus perhitungan reaksi kimia,
sebagai contoh Reaksi kimia berikut terjadi dalam sistem tertutup:

2 +
+

.( )

..( )

Ada kondisi kesetimbangan, reaksi kimia tersebut dapat dinyatakan dalam:


=

.( )

.( )

ci merupakan konsentrasi i. Sedangkan x1 dan x2 merupakan jumlah mol C yang dihasilkan


dari reaksi a dan b.
JIka K1=4x10-4

K2=3.7x10-2

ca,0=50

cb,0=20

cc,0=5

cd,0=10

Gunakan Metode Newton Raphson untuk mencari solusi dari kasus di atas.
Jawab:
1. Gunakan stoikiometri dari reaksi a dan b untuk mencari konsentrasi dari masing
masing zat dengan memberikan x1 dan x2

2. Substitusikan ke persamaan c dan d, sehingga

=
=

(
(

)(

3. Persamaan di atas memiliki dua variabel yang tidak diketahi yaitu x1 dan x2 sehingga
digunakanlah single-equation Newton Raphson dan juga untuk menentukan akar
sejati

( ,

( ,

)=

)=

) (

)(

(4 10 )

(3.7 10 )

4. Karena menggunakan Newton Raphson Method, maka harus ditentukan dulu


determinan Jacobi dengan cara menurunkan persamaan di atas secara parsial

=
=
=
=

(
(

,
,

(
(

,
,

Kemudian persamaan determinan Jacobi tersebut dinatakan dengan M-File dalam


Matlab, yaitu:
function [J,f]=jfreact(x,varargin)
del=0.000001;
df1dx1=(u(x(1)+del*x(1),x(2))-u(x(1),x(2)))/(del*x(1));
df1dx2=(u(x(1),x(2)+del*x(2))-u(x(1),x(2)))/(del*x(2));
df2dx1=(v(x(1)+del*x(1),x(2))-v(x(1),x(2)))/(del*x(1));
df2dx2=(v(x(1),x(2)+del*x(2))-v(x(1),x(2)))/(del*x(2));
J=[df1dx1 df1dx2;df2dx1 df2dx2];
f1=u(x(1),x(2));
f2=v(x(1),x(2));
f=[f1;f2];
function f=u(x,y)
f = (5 + x + y) / (50 - 2 * x - y) ^ 2 / (20 - x) 0.0004;
function f=v(x,y)
f = (5 + x + y) / (50 - 2 * x - y) / (10 - y) - 0.037;
M-file di atas kemudian disimpan.
Sebelumnya sudah dibuat M-file untuk Newtmult (Multiequation Newton Raphson
Method) sehingga dapat langsung dikerjakan pada Command Window dengan
perintah seperti berikut:
>> format short e
>> [x,f,ea,iter]=newtmult(@jfreact,x0)
Setelah itu akan diperoleh hasil dari Matlab seperti berikut:
>> format short e
[x,f,ea,iter]=newtmult(@jfreact,x0)

x =
3.3366e+000
2.6772e+000

f =
5.1816e-015
1.8277e-014

ea =
2.2168e-009
iter =
5
Berdasarkan perhitungan tersebut, diperoleh x1 dan x2 yaitu:
x1 = 3.3366
x2 =2.6772
Kemudian nilai tersebut dapat disubstitusikan ke dalam persamaan konsentrasi dari
masing-masing zat.
=

= 20 3,3366 = 16,663

= 50 2(3,3366) 2,6772= 40,65

= 5 + 3,3366 +2,6772 = 11,014

= 10 2,6772 = 73,228

BERIKUT INI ADALAH LAMPIRAN PENYELESAIAN


MENGGUNAKAN MATLAB

4/10/16 2:55 PM C:\Users\ASUS\Documents\MATLAB\jfreact.m 1 of 1


function [J,f]=jfreact(x,varargin)del=0.000001;
df1dx1=(u(x(1)+del*x(1),x(2))-u(x(1),x(2)))/(del*x(1));
df1dx2=(u(x(1),x(2)+del*x(2))-u(x(1),x(2)))/(del*x(2));
df2dx1=(v(x(1)+del*x(1),x(2))-v(x(1),x(2)))/(del*x(1));
df2dx2=(v(x(1),x(2)+del*x(2))-v(x(1),x(2)))/(del*x(2));
J=[df1dx1 df1dx2;df2dx1 df2dx2];
f1=u(x(1),x(2));
f2=v(x(1),x(2));
f=[f1;f2];
function f=u(x,y)f = (5 + x + y) / (50 - 2 * x - y) ^ 2 / (20 - x) 0.0004;
function f=v(x,y)f = (5 + x + y) / (50 - 2 * x - y) / (10 - y) - 0.037;

4/10/16 2:55 PM C:\Users\ASUS\Documents\MATLAB\newtmult.m 1 of 1


function [x,f,ea,iter]=newtmult(func,x0,es,maxit,varargin)% newtmult:
Newton-Raphson root zeroes nonlinear systems
% [x,f,ea,iter]=newtmult(func,x0,es,maxit,p1,p2,...):
% uses the Newton-Raphson method to find the roots of
% a system of nonlinear equations
% input:
% func = name of function that returns f and J
% x0 = initial guess
% es = desired percent relative error (default = 0.0001%)% maxit = maximum
allowable iterations (default = 50)% p1,p2,... = additional parameters used
by function
% output:
% x = vector of roots
% f = vector of functions evaluated at roots
% ea = approximate percent relative error (%)% iter = number of iterations
if nargin<2,error('at least 2 input arguments required' ),end
if nargin<3||isempty(es),es=0.0001; end
if nargin<4||isempty(maxit),maxit=50; end
iter = 0;
x=x0;
while (1)[J,f]=func(x,varargin{:});
dx=J\f;
x=x-dx;
iter = iter + 1;
ea=100*max(abs(dx./x));
if iter>=maxit||ea<=es, break, end
end

CONTOH SOAL

KASUS I
You have a spherical storage tank containing oil. The tank has a diameter of 6 ft. You are
asked to calculate the height h to which a dipstick 8 ft long would be wet with oil when
immersed in the tank when it contains 6 ft3 of oil.

dipstick
Spherical storage tank

Figure 1. Spherical Storage Tank problem


The equation that gives the height h of the liquid in the spherical tank for the given volume and
radius is given by :
f(h) = h3 9h2 +3.8197 = 0
Use the Newton-Raphson method of finding roots of equations to find the height h to which
the dipstick is wet with oil. Conduct iterations to estimate the root of the above equation. Find
the absolute relative approximate error at the end of each iteration.
Solution :
Masalah diatas dapat diselesaikan menggunakan aplikasi matlab, dengan menggunakan metode
newton raphson. Berikut adalah langkah-langkah penyelesaiannya :

1.

Menurunkan Persamaan dan menentukan nilai tebakan awal atau h0


f(h) = h3 9h2 +3.8197
f(h) = 3h2 18h

Menghitung Dari persamaan diatas diketahui bahwa nilai f(h) = 0 . dan kita asumsikan untuk
tebakan nilai awal iterasi adalah 1, sehingga h0 atau hold = 1
Jika persamaan tersebut dimasukkan ke dalam matlab, hasilnya adalah sebagai berikut:
Memasukkan nilai h0 dalam aplikasi matlab:
>> hold=1
hold =
1
2.

Melakukan iterasi dan mencari nilai eror ()

Untuk f(h) di simbolkan dengan fh dan untuk f(h) disimbolkan dengan gh.
()

()

Untuk iterasi 1 :

( )
( )
Perhitungan yang dilakukan dalam matlab adalah sebagai berikut :
=

>> fh0=hold^3-9*hold^2+3.8197
fh0 =
-4.1803
>> gh0=3*hold^2-18*h0
gh0 =
-15
>> h1=h0-(fh0/gh0)
h1 =
0.7213
Dari perhitungan diatas didapat nilai hnew atau (h1) sebesar 0.7213, sehingga dapat dihitung
nilai error nya dengan rumus sebagai berikut :
| | =

Perhitungan pada matlabnya adalah :


>> e=abs((h1-hold)/h1)*100
e =
38.6360

100

Nilai eror masih sangat besar yaitu 38.636% sehingga perlu dilakukan iterasi yang ke-2

Iterasi ke-2
Nilai hold yang digunakan adalah nilai h(1) yang telah didapat dan dimasukkan kedalam
persamaan untuk menemukan nilai hnew atau h(2) dengan cara yang sama seperti iterasi
sebelumnya.
>> hold=h1
hold =
0.7213
Iterasi dilakukan dengan menggunakan rumus:

( )
( )
Sehingga perhitungan pada matlabnya adalah sebagai beikut:
=

>> fh1=hold^3-9*hold^2+3.8197
fh1 =
-0.4876
>> gh1=3*hold^2-18*hold
gh1 =
-11.4228
>> h2=hold-(fh1/gh1)
h2 =
0.6786
Dari perhitungan diatas didapat nilai hnew atau (h2) sebesar 0.6786, sehingga dapat dihitung
nilai error nya dengan rumus sebagai berikut :
| | =

100

>> e2=abs((h2-hold)/h2)*100
e2 =
6.2907
Nilai eror masih sangat besar yaitu 6.2907 % sehingga perlu dilakukan iterasi yang ke-3

Iterasi ke-3
Nilai hold yang digunakan adalah nilai h(2) yang telah didapat dan dimasukkan kedalam
persamaan untuk menemukan nilai hnew atau h(3) dengan cara yang sama seperti iterasi
sebelumnya.
>> hold=h2
hold =
0.7213
Iterasi dilakukan dengan menggunakan rumus:

( )
( )
Sehingga perhitungan pada matlabnya adalah sebagai beikut:
=

>> fh2=hold^3-9*hold^2+3.8197
fh2 =
-0.0125
>> gh2=3*hold^2-18*hold
gh2 =
-10.8336

>> h3=hold-(fh2/gh2)
h3 =
0.6775
Dari perhitungan diatas didapat nilai hnew atau (h3) sebesar 0.6775, sehingga dapat dihitung
nilai error nya dengan rumus sebagai berikut :
| | =

100

>> e3=abs((h3-hold)/h3)*100
e3 =
0.1708
Nilai eror masih cukup besar yaitu 0.1708 % sehingga perlu dilakukan iterasi yang ke-4

Iterasi ke-4
Nilai hold yang digunakan adalah nilai h(3) yang telah didapat dan dimasukkan kedalam
persamaan untuk menemukan nilai hnew atau h(4) dengan cara yang sama seperti iterasi
sebelumnya
>> hold=h2
hold =
0.7213
Iterasi dilakukan dengan menggunakan rumus:
=

>> fh3=hold^3-9*hold^2+3.8197
fh3 =
-9.3268e-006

( )
( )

>> gh3=3*hold^2-18*hold
gh3 =
-10.8175
>> h4=hold-(fh3/gh3)
h4 =
0.6775
Dari perhitungan diatas didapat nilai hnew atau (h4) sebesar 0.6775, nilai eror nya adalah:

| | =

100

>> e4=abs((h4-hold)/h4)*100
e4 =
1.2727e-004
Nilai hnew yang didapat suma dengan nilai hold yang sebelumnya, serta nilai eror nyapun
sudah sangat kecil yaitu 0.00012727 %. Sehingga proses iterasi ini dapat dihentikan.

LAMPIRAN PENGERJAAN SOAL PADA MATLAB

4/10/16 10:30 AM MATLAB Command Window


>> hold=1
hold =
1
>> fh0=hold^3-9*hold^2+3.8197
fh0 =
-4.1803
>> gh0=3*hold^2-18*hold
gh0 =
-15
>> h1=hold-(fh0/gh0)
h1 =
0.7213
>> e=abs((h1-hold)/h1)*100
e =
38.6360
>> hold=h1
hold =
0.7213
>> fh1=hold^3-9*hold^2+3.8197
fh1 =
-0.4876
>> gh1=3*hold^2-18*hold
gh1 =
-11.4228
>> h2=hold-(fh1/gh1)
h2 =
0.6786
>> e2=abs((h2-hold)/h2)*100
e2 =
6.2907
>> hold=h2
hold =
0.6786
>> fh2=hold^3-9*hold^2+3.8197
fh2 =
-0.0125
>> gh2=3*hold^2-18*hold
gh2 =
-10.8336
>> h3=hold-(fh2/gh2)
h3 =

1 of 3

0.6775
>> e3=abs((h3-hold)/h3)*100
e3 =
0.1708
>> hold=h3
hold =
0.6775
>> fh3=hold^3-9*hold^2+3.8197
fh3 =
-9.3268e-006
>> gh3=3*hold^2-18*hold
gh3 =
-10.8175
>> h4=hold-(fh3/gh3)
h4 =
0.6775
>> e4=abs((h4-hold)/h4)*100
e4 =
1.2727e-004

KASUS 2
Finding a Root of an nth-Degree Polynomial by Newton-raphson. Method Applied to the
Soave-Redlich-Kwong Equation of State.
Develop a MATLAB function to calculate a root of a polynomial equation by Newton-Raphson
method. Calculate the specific volume of a pure gas, at a given temperature and pressure, by
using the Soave- Redlich-Kwong equation of state:
=

( + )

The equation constants, a and b, are obtained from:


=

0.4278

0.0867

where TC and PC are critical temperature and pressure, respectively. The variable is an
empirical function of temperature:
= 1+

The value of S is a function of the acentric factor, , of the gas:


S = 0.48508 + 1.55171 0. 15613 2

The physical properties of n-butane are:


Tc = 425.2 K,

Pc = 3797 kPa,

= 0.1931

and the gas constant is:


R= 8314 J/kmol.K
Calculate the specific volume of n-butane vapor at 500 K and at temperatures from 1 to 40 atm.
Compare the results graphically with the ones obtained from using the ideal gas law. What
conclusion do you draw from this comparison?

Method of Solution: Case 2 is used for Newton-Raphson evaluation of the root. For finding
the gas specific volume from the Soave-Redlich-Kwong equation of state, Case 2,
which is a third-degree polynomial in compressibility factor, is solved. Starting value for the
iterative method is Z = 1, which is the compressibility factor of the ideal gas.

Berikut ini adalah penyelesaiaanya dengan menggunakan mfilenya :


Program
Example1_2.m
% Kasus_2.m
% This program solves the problem posed in Kasus_2.
% It calculates the real gas specific volume from the
% SRK equation of state using the Newton-Raphson method
% for calculating the roots of a polynomial.
clear
clc
clf
% Input data
P = input(' Input the vector of pressure range (Pa) = ');
T = input(' Input temperature (K) = ');
R = 8314; % Gas constant (J/kmol.K)
Tc = input(' Critical temperature (K) = ');
Pc = input(' Critical pressure (Pa) = ');
omega = input(' Acentric factor = ');
% Constants of Soave-Redlich-Kwong equation of state
a = 0.4278 * R^2 * Tc^2 / Pc;
b = 0.0867 * R * Tc / Pc;
sc = [-0.15613, 1.55171, 0.48508];
s = polyval(sc,omega);
alpha = (1 + s * (1 - sqrt(T/Tc)))^2;
A = a * alpha * P / (R^2 * T^2);
B = b * P / (R * T);
for k = 1:length(P)
% Defining the polynomial coefficients
coef = [1, -1, A(k)-B(k)-B(k)^2, -A(k)*B(k)];
v0(k) = R * T / P(k); % Ideal gas specific volume
vol(k) = NRpoly(coef , 1) * R * T / P(k); % Finding the root
end

% Show numerical results


fprintf('\nRESULTS:\n');
fprintf('Pres. = %5.2f Ideal gas vol. =%7.4f',P(1),v0(1));
fprintf(' Real gas vol. =%7.4f\n',vol(1));
for k=10:10:length(P)
fprintf('Pres. = %5.2f Ideal gas vol. =%7.4f',P(k),v0(k));
fprintf(' Real gas vol. =%7.4f\n',vol(k));
end
% plotting the results
loglog(P/1000,v0,'.',P/1000,vol)
xlabel('Pressure, kPa')
ylabel('Specific Volume, m^3/kmol')
legend('Ideal','SRK')
NRpoly.m
function x = NRpoly(c,x0,tol,trace)
%NRPOLY Finds a root of polynomial by the Newton-Raphson method.
%
%
NRPOLY(C,X0) computes a root of the polynomial whose
%
coefficients are the elements of the vector C.
%
If C has N+1 components, the polynomial is
%
C(1)*X^N + ... + C(N)*X + C(N+1).
%
X0 is a starting point.
%
%
NRPOLY(C,X0,TOL,TRACE) uses tolerance TOL for convergence
%
test. TRACE=1 shows the calculation steps numerically and
%
TRACE=2 shows the calculation steps both numerically and
%
graphically.
%
%
See also ROOTS, NR, LI, XGX, FZERO.
% (c) by N. Mostoufi & A. Constantinides
% January 1, 1999
% Initialization
if nargin < 3 | isempty(tol)
tol = 1e-6;
end
if nargin < 4 | isempty(trace)
trace = 0;
end
if tol == 0
tol = 1e-6;
end
if (length(x0) > 1) | (~isfinite(x0))
error('Second argument must be a finite scalar.')
end
iter = 0;
fnk = polyval(c,x0); % Function
if trace
header = ' Iteration
x
f(x)';
disp(header)
disp([sprintf('%5.0f
%13.6g %13.6g ',iter, [x0 fnk])])
if trace == 2
xpath = [x0 x0];
ypath = [0 fnk];
end
end

x = x0;
x0 = x + .1;
maxiter = 100;
% Solving the polynomial by Newton-Raphson method
while abs(x0 - x) > tol & iter < maxiter
iter = iter + 1;
x0 = x;
fnkp = polyval(polyder(c),x0); % Derivative
if fnkp ~= 0
x = x0 - fnk / fnkp; % Next approximation
else
x = x0 + .01;
end
fnk = polyval(c,x); % Function
% Show the results of calculation
if trace
disp([sprintf('%5.0f
%13.6g %13.6g ',iter, [x fnk])])
if trace == 2
xpath = [xpath x x];
ypath = [ypath 0 fnk];
end
end
end
if trace == 2
% Plot the function and path to the root
xmin = min(xpath);
xmax = max(xpath);
dx = xmax - xmin;
xi = xmin - dx/10;
xf = xmax + dx/10;
yc = [];
for xc = xi : (xf - xi)/99 : xf
yc = [yc polyval(c,xc)];
end
xc = linspace(xi,xf,100);
ax = linspace(0,0,100);
plot(xc,yc,xpath,ypath,xc,ax,xpath(1),ypath(2),'*',x,fnk,'o')
axis([xi xf min(yc) max(yc)])
xlabel('x')
ylabel('f(x)')
title('Newton-Raphson : The function and path to the root (* :
initial guess ; o : root)')
end
if iter == maxiter
disp('Warning : Maximum iterations reached.')
end

Setelah dibuat mfile seperti diatas, mfile dijalankan sehingga hasilnya akan muncul
seperti berikut ini :
Input and Results
>>Example1_2
Input the vector of pressure range (Pa) : [1:40]*101325
Input temperature (K) : 500
Critical temperature (K) : 425.2
Critical pressure (Pa) : 3797e3
Acentric factor : 0.1931
Pres. = 101325.00 Ideal gas vol. =41.0264 Real gas vol.
Pres. = 1013250.00 Ideal gas vol. = 4.1026 Real gas vol.
Pres. = 2026500.00 Ideal gas vol. = 2.0513 Real gas vol.
Pres. = 3039750.00 Ideal gas vol. = 1.3675 Real gas vol.
Pres. = 4053000.00 Ideal gas vol. = 1.0257 Real gas vol.

KASUS 3 :

=40.8111
= 3.8838
= 1.8284
= 1.1407
= 0.7954

Anda mungkin juga menyukai