komputasi teknik dengan menggunakan perangkat lunak...
TRANSCRIPT
Komputasi teknik dengan menggunakan Perangkat lunak Matlab
Oleh : Ir. M.Syahril Gultom, MT
Staf Pengajar Departemen Teknik Mesin FT USU
DEPARTEMEN TEKNIK MESIN
FAKULTAS TEKNIK UNIVERSITAS SUMATERA UTARA
2007
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Komputasi teknik dengan menggunakan Perangkat lunak Matlab
Oleh :
Ir. M.Syahril Gultom, MT Staf Pengajar Departemen Teknik Mesin FT USU
P e n d a h u l u a n
Mana kala suatu persoalan yang diselesaikan secara matematis analitis tidak lagi
mampu ataupun menjadi begitu sulit diperoleh penyelesaiannya, maka analisa
numerik menjadi alternatif pemecahan yang mudah diterapkan dengan kemampuan
processing komputer berkecepatan tinggi saat ini .
Dalam hal yang menyangkut komputasi teknik tidak sedikit kasus kasus yg ditemui
ketika dalam pemodelan matematisnya telah dibentuk , ternyata tidak dapat
diselesaikan secara teknik analitis, ataupun terbentur dengan proses penyelesaian yg
rumit dan berbelit belit.
Pemodelan matematis pada kasus kasus keteknikan yang identik dengan bentuk
suatu fungsi yangg ingin dicari nilai akarnya yang mewakili besaran tertentu dari
masalah keteknikan akan menjadi mudah dengan pemanfaatan metode metode
numerik yang sudah dikenal .
Disini, dipaparkan teknik iterasi dan aplikasi dengan menggunakan prangkat lunak
Matlab dalam kasus menentukan akar akar persamaan dari suatu fungsi .
Dengan menggunakan metode Newton – Rhapson dan metode Secant dalam
penentuan akar akar dari suatu fungsi , perogam dilaksanakan pada sejumlah
perhitungan secara iteratif dalam memperoleh hasil pendekatan (aproksimasi)
terhadap nilai eksas dari akar akar fungsi yang dipecahkan .
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
A p l i k a s i M a t l a b
Teknik iterasi digunakan untuk mendapatkan pemecahan dari persamaan-
persamaan. Formula yang memberikan jawaban nilai numerik yang eksak terhadap
sebuah persamaan hanya ada pada kondisi yang sangat sederhana. Pada umumnya,
kita harus menggunakan pendekatan (aproksimasi). Metode iterasi dimulai dengan
sebuah nilai inisial (initial guess) (yang nilainya mungkin kurang tepat) dan
kemudian menghitung setahap-demi-setahap untuk mendekati (secara umum nilai
ini kemudian menjadi lebih baik dan lebih baik) sebuah solusi yang tidak diketahui
dari sebuah persamaan.
M-file : approot.m
Menggunakan algoritma iterasi yang sederhana yang di usulkan pada tahun 1999
oleh John H. Mathews dan Kurtis D. Fink (NUMERICAL METHODS Using
MATLAB). Kode matlab yang ditampilkan ini telah di perbaiki untuk memberikan
kemudahan bagi komputasi teknik. Algoritma ini bertujuan meng-evaluasi sebuah
vektor dari perkiraan lokasi akar dari sebuah fungsi f yang disimpan di dalam *.m
file yang berbeda. Dalam hal ini , diperkenankan untuk merubah fungsi dan
melakukan pendekatan (perkiraan) akar-akar fungsi tersebut.
% Input - f adalah object function disimpan di dalam M-file f.m
% - X adalah vector dari abscissas
% - epsilon adalah tolerance
% Output - R adalah vector perkiraan lokasi akar-akar fungsi
while 1
clear, clf,clc
fprintf( '\n===========================================\n' );
fprintf( ' Approximative Root Method \n' );
i = 1;
fprintf( '** Give the X abscissa values:\n');
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
while 1
km = input( '' );
X(i) = km;
i=i+1;
kont = input( 'Type 1 to continue, or 0 to stop adding X values: ' );
if kont == 0, break; end
end
while 1
fprintf( '\n Input the tolerance value, epsilon = ')
epsilon = input('');
if epsilon < 1 & epsilon > 0; break; end
fprintf( ' \n Epsilon should be greater than 0 and less than 1!!! ')
fprintf( ' \n Repeat your input for tolerance value! ')
end
Y=f_test(X);
axes;
plot(X,Y,'r-o');
centaxes
yrange = max(Y)-min(Y);
epsilon2 = yrange*epsilon;
n=length(X);
m=0;
X(n+1)=X(n);
Y(n+1)=Y(n); for k=2:n,
if Y(k-1)*Y(k) <= 0,
m=m+1;
R(m)=(X(k-1)+X(k))/2;
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
s=(Y(k)-Y(k-1))*(Y(k+1)-Y(k));
if (abs(Y(k)) < epsilon2) & (s <= 0),
m=m+1;
R(m)=X(k);
end
end
fprintf(' The solutions are: \n');
if length(R) ~= 0
for k=1:length(R),
fprintf(' R(%u) = %6.2f \n', k, R(k));
end
else
fprintf(' No root values! ')
end
fprintf('\n============================================')
kont = input( 'Type 1 to continue, or 0 to stop: ' );
if kont == 0, break; end
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Kita gunakan fungsi f berikut sebagai contoh : f(x) = x3 + x2 - 1.25x - 0.75 . Gambar
yang dihasilkan dari algoritma diberikan dibawah ini :
Nilai-nilai perkiraan abscissas dimana fungsi f(x) mendekati X – axis adalah sebagai
berikut (R adalah vector dimana mereka disimpan):
R(1) = -1.75
R(2) = -1.25
R(3) = -1.00
R(4) = -0.75
R(5) = -0.25
R(6) = 0.75
R(7) = 1.25
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Nilai diatas tidak berarti bahwa fungsi f(x) memiliki 7 akar. Algoritma menemukan
di koordinat-koordinat mana fungsi f(x) yang diberikan dengan titik-titik abscissa X
berikut : X0 = -3, X1 = -2.5, X2 = -2, X3 = -1.5, X4 = -1, X5 = -0.5, X6 = 0, X7 = 0.5, X8 =
1, X9 = 1.5, X10 = 2, X11 = 2.5, X12 = 3, X13 = 3.5 dengan toleransi epsilon = 0.01
M-file : secant_alg.m
Kemudian kita uji algoritma iterasi Secant. Kode Matlab dari algoritma yang
dipakai diperbaiki dari "NUMERICAL METHODS Using MATLAB" oleh John H.
Mathews dan Kurtis D. Fink. Kode matlab tersebut adalah sebagai berikut:
%function [p1,err,k,y]=secant(f,p0,p1,delta,epsilon,max1)
%Input - f is the object function input as a string 'f'
% - p0 and p1 are the initial approximations to a zero of f
% - delta is the tolerance for p1
% - epsilon is the tolerance for the function values y
% - max1 is the maximum number of iterations
%Output - p1 is the secant method approximation to the zero
% - err is the error estimate for p1
% - k is the number of iterations
% - y is the function value f(p1)
while 1
clear, clf,clc
fprintf( '\n============================================\n' );
fprintf( ' Secant Method \n' );
while 1
fprintf( ' \n The absciss extremes a and b (a < b)')
fprintf( ' \n a = '); a = input('');
fprintf( ' b = '); b = input('');
if a < b; break; end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
fprintf( ' \n a should be less than b! ')
fprintf( ' \n Repeat your input for a and b! ')
end
fprintf( ' \n Now the function is plotted \n')
x=a:0.1:b;
f = feval('f306',x);
plot(x, f, 'b-');
centaxes
hold on;
fprintf( '** Give the Po, first initial abscissa value approximation to a zero of f:\n');
p0 = input('Initial approximation Po = ');
fprintf( '** Give the P1, second initial abscissa value approximation to a zero of f:\n');
p1 = input('Initial approximation P1 = ');
while 1
fprintf( '\n Input the tolerance value for P1, delta = ')
delta = input('');
if delta < 1 & delta > 0; break; end
fprintf( ' \n Delta should be greater than 0 and less than 1!!! ')
fprintf( ' \n Repeat your input for tolerance value! ')
end
while 1
fprintf( '\n Input the tolerance value for the function Y values, epsilon = ')
epsilon = input('');
if epsilon < 1 & epsilon > 0; break; end
fprintf( ' \n Epsilon should be greater than 0 and less than 1!!! ')
fprintf( ' \n Repeat your input for tolerance value! ')
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
while 1
fprintf( '\n Input the maximum number of iterations, N_max = ')
max1 = input('');
if max1 > 1; break; end
fprintf( ' \n The number of iterations should be greater than 1!!! ')
fprintf( ' \n Repeat your input for the number of iterations value! ')
end
% inisial dari fungsi secant
for k=1:max1
p2=p1-feval('f306',p1)*(p1-p0)/(feval('f306',p1)-feval('f306',p0));
err=abs(p2-p1);
relerr=2*err/(abs(p2)+delta);
p0=p1;
p1=p2;
y=feval('f306',p1);
if (err<delta)|(relerr<delta)|(abs(y)<epsilon),break,end
end
fprintf('\n==============RESULTS================')
fprintf('\n P1 is the Secant approximation to zero = %12.6f', p1);
fprintf('\n Err is the error estimate for P1 = %12.6f', err);
fprintf('\n K is the number of iterations = %u', k);
fprintf('\n Y is the function value f(P1) = %12.6f', y);
fprintf('\n============================================')
kont1 = input( 'Type 1 to continue, or 0 to stop: ' );
if kont1 == 0, break; end
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Penjelasan dari bebeapa variable input yang diberikan ke dalam algoritma Secant
dapat dibaca pada bagian comments pada awal dari kode matlab diatas. Kemudian,
algoritma dijalankan untuk contoh fungsi berikut: f(x) = x-cos(x) yang terdapat pada
f306.m. (isi m-file f306.m adalah :
function fv=f306(x);
fv=x-cos(x);
Fungsi tersebut diberikan untuk interval a = -10, b = 10 dan terlihat seperti dibawah
ini :
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Parameter-parameter input yang lain adalah: P0 = -1.67, P1 = -2.21, delta = 0.002,
epsilon = 0.012, N_max = 100 dan hasilnya diambil dari (command) Matlab window:
P1 is the Secant approximation to zero = 0.737000
Err is the error estimate for P1 = 0.064575
K is the number of iterations = 5
Y is the function value f(P1) = -0.003488
Inisial dari Fungsi Secant terlihat pada kode diatas sebagai berikut :
for k=1:max1
p2=p1-feval('f306',p1)*(p1-p0)/(feval('f306',p1)-feval('f306',p0));
err=abs(p2-p1);
relerr=2*err/(abs(p2)+delta);
p0=p1;
p1=p2;
y=feval('f306',p1);
if (err<delta)|(relerr<delta)|(abs(y)<epsilon),break,end
end
M-file : newt_n.m
Metode iterasi yang ditelaah berikutnya adalah Newton. Kode matlab dikembangkan
dari kode yang ditulis oleh Nakamura. Algoritma dijalankan untuk fungsi yang
sama: f(x) = x-cos(x) yang diberikan dalam f306.m. Kode lain dari fungsi Newton di
berikan didalam fnewton.m.
% Newt_n(f_name, x0) finds a root of a function by
% Newton iteration
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
% f_name: the function name that defines the equation
% to solve
% x0: an initial guess.
% improved after Copyright S. Nakamura, 1995
% function x = Newt_n(f_name, x0)
while 1
clear, clf,clc
fprintf( '\n============================================\n' );
fprintf( ' Newton Iteration Method (without graphics) \n' );
fprintf( '** Give the Xo abscissa value approximation:\n');
x0 = input('Initial guess Xo = ');
x = x0; xb=x-999;
n=0; del_x = 0.01;
while abs(x-xb)>0.000001
n=n+1; xb=x;
if n>300 break; end
y=feval('f306', x);
y_driv=(feval('f306', x+del_x) - y)/del_x;
x = xb - y/y_driv;
fprintf(' n=%3.0f, x=%12.5e, y = %12.5e, ', n,x,y)
fprintf(' yd = %12.5e \n', y_driv)
end
fprintf('\n Final answer = %12.6e\n', x);
fprintf('\n============================================')
kont = input( 'Type 1 to continue, or 0 to stop: ' );
if kont == 0, break; end
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
The initial X0 abscissa value approximation and the results are given below exactly the
same way they are listed by the algorithm in the Matlab command window:
============================================
Newton Iteration Method (without graphics)
** Give the Xo abscissa value approximation:
Initial guess Xo = -1.23
n= 1, x=2.51938e+001, y = -1.56424e+000, yd = 5.91981e-002
n= 2, x=2.49622e+000, y = 2.41957e+001, yd = 1.06600e+000
n= 3, x=4.33557e-001, y = 3.29509e+000, yd = 1.59750e+000
n= 4, x=7.66219e-001, y = -4.73920e-001, yd = 1.42463e+000
n= 5, x=7.39300e-001, y = 4.56807e-002, yd = 1.69701e+000
n= 6, x=7.39086e-001, y = 3.59955e-004, yd = 1.67745e+000
n= 7, x=7.39085e-001, y = 8.07423e-007, yd = 1.67730e+000
Final answer = 7.390851e-001
============================================Type 1 to continue, or 0 to
stop:
M-file dari : fnewton.m
function [res, it]=fnewton(func,dfunc,x,tol)
% Finds a root of f(x) = 0 using Newton's method.
% Example call: [res, it]=fnewton(func,dfunc,x,tol)
% The user defined function func is the function f(x),
% The user defined function dfunc is df/dx.
% x is an initial starting value, tol is required accuracy.
it=0; x0=x;
d=feval(func,x0)/feval(dfunc,x0);
while abs(d)>tol
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
x1=x0-d;
it=it+1;
x0=x1;
d=feval(func,x0)/feval(dfunc,x0);
end;
res=x0;
M-file : newt_g.m
Sekarang metode Newton diatas (newt_n) dianalisa kembali dengan contoh kode
berikut beserta fitur grafiknya :
% Newt_g(f_name, x0, xmin, xmax, n_points)
% finds a root of a function f_name by
% newton iteration (with graphics)
% improved after Copyright S. Nakamura, 1995
while 1
clear, clf,clc
fprintf( '\n============================================\n' );
fprintf( ' Newton Iteration Method (with graphics) \n' );
fprintf( '** Give the Xmin and Xmax abscissa values:\n');
while 1
xmin = input( 'Xmin = ' );
xmax = input( 'Xmax = ' );
if xmin < xmax; break; end
fprintf( ' \n Xmin has to be less than Xmax!!! ')
fprintf( ' \n Repeat your input for Xmin and Xmax values! ')
end
fprintf( '\n Input the number of points\n ')
n_points = input('N_points = ');
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
fprintf( '\n Input an initial guess\n ')
x0 = input('Xo = ');
clf, hold off
% xmin, xmax: min and max for plotting
del_x=0.001;
wid_x = xmax - xmin; dx = (xmax- xmin)/n_points;
xp=xmin:dx:xmax; yp=feval('f306', xp);
plot(xp,yp); xlabel('x');ylabel('f(x)');
title('Newton Iteration'),hold on
ymin=min(yp); ymax=max(yp);wid_y = ymax-ymin;
yp=0.*xp; plot(xp,yp)
x = x0; xb=x+999; n=0;
while abs(x-xb)>0.000001
if n>300 break; end
y=feval('f306', x);
plot([x,x],[y,0],'--');
plot(x,0,'o')
fprintf(' n=%3.0f, x=%12.5e, y=%12.5e\n', n,x,y);
xsc=(x-xmin)/wid_x;
if n<4,
text(x, -wid_y/20, [ num2str(n)], 'FontSize', [16]),
end
y_driv=(feval('f306', x+del_x) - y)/del_x;
xb=x;
x = xb - y/y_driv; n=n+1;
plot([xb,x],[y,0],':')
end
plot([x x],[0.02*wid_y 0.2*wid_y])
text( x, 0.25*wid_y, 'Final solution','FontSize', [16])
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
plot([x (x-wid_x*0.004)],[0.02*wid_y 0.05*wid_y])
plot([x (x+wid_x*0.004)],[0.02*wid_y 0.05*wid_y])
axis([xmin,xmax,ymin*1.2,ymax*1.2])
fprintf('\n============================================')
kont = input( 'Type 1 to continue, or 0 to stop: ' );
if kont == 0, break; end
end
Sebagai contoh algoritma diatas di jalankan dengan input dan hasil persis seperti
terlihat didalam Matlab command window:
============================================
Newton Iteration Method (with graphics)
** Give the Xmin and Xmax abscissa values:
Xmin = -10
Xmax = 10
Input the number of points
N_points = 30
Input an initial guess
Xo = -1.56
n= 0, x=-1.56000e+000, y=-1.57080e+000
n= 1, x=2.46019e+004, y=2.46029e+004
n= 2, x=-2.79821e+003, y=-2.79763e+003
n= 3, x=1.23465e+004, y=1.23455e+004
n= 4, x=7.16039e+002, y=7.15068e+002
n= 5, x=-2.26678e+002, y=-2.27563e+002
n= 6, x=1.98154e+002, y=1.99127e+002
n= 7, x=-6.11256e+001, y=-6.09906e+001
n= 8, x=-3.04890e+001, y=-3.10893e+001
n= 9, x=-1.32181e+001, y=-1.40131e+001
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
n= 10, x=2.23598e+001, y=2.32926e+001
n= 11, x=-1.40845e+001, y=-1.41371e+001
n= 12, x=9.98146e+003, y=9.98227e+003
n= 13, x=-1.39418e+004, y=-1.39427e+004
n= 14, x=-4.87296e+003, y=-4.87202e+003
n= 15, x=-1.23669e+003, y=-1.23715e+003
n= 16, x=-5.81733e+002, y=-5.80875e+002
n= 17, x=-1.97658e+002, y=-1.96692e+002
n= 18, x=6.80764e+001, y=6.75690e+001
n= 19, x=-4.19595e+002, y=-4.19785e+002
n= 20, x=-2.07767e+002, y=-2.08679e+002
n= 21, x=1.45265e+002, y=1.44534e+002
n= 22, x=5.93973e+001, y=6.03547e+001
n= 23, x=1.25486e+001, y=1.15488e+001
n= 24, x=7.97007e-001, y=9.81568e-002
n= 25, x=7.39794e-001, y=1.18581e-003
n= 26, x=7.39085e-001, y=4.46683e-007
======================================Type 1 to continue, or 0 to stop:
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Representasi grafik dari jalannya algoritma diatas adalah sebagai berikut :
M-file : newton_raphson_polynom.m
Metode berikutnya adalah yang diusulkan oleh Newton-Raphson. Kode matlab yang
dibuat adalah pengembangan dari newton_raphson.m (diusulkan di dalam
"NUMERICAL METHODS Using MATLAB" oleh John H. Mathews dan Kurtis D.
Fink) dan di khususkan untuk kasus fungsi-fungsi polynomial karena turunan
pertama analitiknya dapat dihitung dengan mudah.
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
%function [p0,err,k,y]=newton(f,df,p0,delta,epsilon,max1)
%Input - f is the object function input as a string 'f'
% - df is the derivative of f input as a string 'df'
% - p0 is the initial approximation to a zero of f
% - delta is the tolerance for p0
% - epsilon is the tolerance for the function values y
% - max1 is the maximum number of iterations
%Output - p0 is the Newton-Raphson approximation to the zero
% - err is the error estimate for p0
% - k is the number of iterations
% - y is the function value f(p0)
while 1
clear, clf,clc
fprintf( '\n============================================\n' );
fprintf( ' Newton-Raphson Method. Polynomial examples \n' );
while 1
km = input( '** The degree of the polynom ? N = ' );
if km>1 break; end
fprintf(' Input is invalid: Repeat.\n')
end
while 1
fprintf( '\n Input the polynomial coefficients ')
fprintf( '\n like [x^N x^N-1 ... x^1 x^0]'); coef = input('');
if length(coef) == km+1; break; end
fprintf( ' \n Number of coefficients do not match with the polynomial degree ')
fprintf( ' \n Repeat your input for coefficients! ')
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
while 1
fprintf( ' \n The absciss extremes a and b (a < b)')
fprintf( ' \n a = '); a = input('');
fprintf( ' b = '); b = input('');
if a < b; break; end
fprintf( ' \n a should be less than b! ')
fprintf( ' \n Repeat your input for a and b! ')
end
fprintf( ' \n Now the polynomial function is plotted ')
x=a:0.1:b;
f = polyval(coef,x);
plot(x, f, 'b-');
centaxes
hold on;
for i=1:km+1
pow_def(i) = km - i + 1; % diagonal elements
end;
dev1_coef=pow_def.*coef;
dev1_coef=dev1_coef(1:km)
df=polyval(dev1_coef, x)
fprintf( ' \n Now the 1st derivative of the polynomial function is plotted \n')
plot(x, df,'c-'); % the analytical 1st derivative
hold on;
fprintf( '** Give the Po initial abscissa value approximation to a zero of f:\n');
p0 = input('Initial approximation Po = ');
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
while 1
fprintf( '\n Input the tolerance value for Po, delta = ')
delta = input('');
if delta < 1 & delta > 0; break; end
fprintf( ' \n Delta should be greater than 0 and less than 1!!! ')
fprintf( ' \n Repeat your input for tolerance value! ')
end
while 1
fprintf( '\n Input the tolerance value for the function Y values, epsilon = ')
epsilon = input('');
if epsilon < 1 & epsilon > 0; break; end
fprintf( ' \n Epsilon should be greater than 0 and less than 1!!! ')
fprintf( ' \n Repeat your input for tolerance value! ')
end
while 1
fprintf( '\n Input the maximum number of iterations, N_max = ')
max1 = input('');
if max1 > 1; break; end
fprintf( ' \n The number of iterations should be greater than 1!!! ')
fprintf( ' \n Repeat your input for the number of iterations value! ')
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
for k=1:max1
p1=p0-polyval(coef,p0)/polyval(dev1_coef,p0);
err=abs(p1-p0);
relerr=2*err/(abs(p1)+delta);
p0=p1;
y=polyval(coef,p0);
if (err<delta)|(relerr<delta)|(abs(y)<epsilon),break,end
end
fprintf('\n==============RESULTS================')
fprintf('\n Po is the Newton-Raphson approximation to zero = %12.6f', p0);
fprintf('\n Err is the error estimate for Po = %12.6f', err);
fprintf('\n K is the number of iterations = %u', k);
fprintf('\n Y is the function value f(Po) = %12.6f', y);
fprintf('\n============================================')
kont1 = input( 'Type 1 to continue, or 0 to stop: ' );
if kont1 == 0, break; end
end
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Sebagai contoh, digunakan fungsi polynomial yang sebelumnya : f(x) = x3 + x2 -
1.25x - 0.75. Input dan output dari algoritma diambil dari Matlab command window
============================================
Newton-Raphson Method. Polynomial examples
** The degree of the polynom ? N = 3
Input the polynomial coefficients
like [x^N x^N-1 ... x^1 x^0][1 1 -1.25 -0.75]
The absciss extremes a and b (a < b)
a = -10
b = 10
Now the 1st derivative of the polynomial function is plotted
** Give the Po initial abscissa value approximation to a zero of f:
Initial approximation Po = -2.34
Input the tolerance value for Po, delta = 0.034
Input the tolerance value for the function Y values, epsilon = 0.023
Input the maximum number of iterations, N_max = 10
==============RESULTS================
Po is the Newton-Raphson approximation to zero = -1.500155
Err is the error estimate for Po = 0.010475
K is the number of iterations = 4
Y is the function value f(Po) = -0.000386
============================================Type 1 to continue, or 0 to
stop:
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Figure window memperlihatkan fungsi f(x) dan turunan pertamanya :
M-file : fzero_root.m
Aplikasi kali ini adalah perbandingan grafik sederhana antara akar yang didekati
dengan cursor dan yang eksak. Kode berikut menggunakan fungsi Matlab
plotapp,dimana Plottapp mem-plot sebuah fungsi untuk memperkirakan sebuah
akar tertentu dengan menggunakan cursor. Fungsi Matlab Fzero adalah fungsi yang
digunakan untuk menentukan akar yang eksak.
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Dibawah ini diperlihatkan Figure Window dan command window dari contoh yang
dijalan dengan menggunakan fungsi f306.m
============================================
Approximate root is 0.75
Exact root is 0.73906
========================================Type 1 to continue, or 0 to stop:
M-file : mat_root.m
x=-4:.1:0.5;
plot(x,f306(x)); grid on;
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
xlabel('x');ylabel('f(x)');
title('f(x)=x-cos(x)');
root=fzero('f306',1.65,0.00005);
fprintf('\n============================================')
fprintf('\nThe root of this equation is %6.4f\n',root);
fprintf('\n============================================')
Aplikasi sederhana diatas menggunakan fungsi Matlab fzero untuk menghitung akar
ekasak dari fungsi f306.m.
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007
Daftar pustaka .
1. Jhon H.Mathews ……………………………………Nunerical method using
MATLAB , 1999.
2. Steven C.Chapra, Raymond P.Canale………. ......Numerical Methods for
Engineers , 2 nd Edition 1988
3. V.Rajarama…………………………………………Computer oriented Numerical
Methods ,3 rd Ed , Feb 1996
4. Steven C. Chapra …………………………………..Introduction to Computing for
Engineer, 1982.
M. Syahril Gultom : Komputasi Teknik dengan Menggunakan Perangkat Lunak Matlab, 2007 USU Repository © 2007