Anda di halaman 1dari 0

Abdul Azis Rahmansyah

P2700213006
Kelas B

TUGAS 2
PERBANDINGAN METODE SCANNING, METODE NEWTON RAPHSON, DAN
METODE SECANT.
Mencari akar-akar Persamaan f(x) = Bx
3
+ Cx
2
+ Dx + E menggunakan matlab.
NIM : P2700213006
Maka B = 2
C = 1
D = 3
E = 6
Sehingga persamaan menjadi f(x) = 2x
3
+ 1x
2
+ 3x + 6
Program ini di buat dalam model GUI dengan inputan sesuai model rumus yang telah di
tampilkan. GUI ini telah mencakup metode scanning, Newton raphson, dan secant sehingga
dapat langsung di bandingkan hasil Perolehan nilai. Khusus untuk metode Newton raphson
turunan harus di input terlebih dahulu di fungsi g(x) dengan fungsi bantu cek turunan. Untuk
plot metode scanning membandingkan plot f(x) dengan x, tetapi untuk metode Newton raphson
dan metode secant plot yang di tampilkan membandingkan antara f(x) dengan nilai x
perkiraan
Tampilan awal GUI sebagai berikut :



Abdul Azis Rahmansyah
P2700213006
Kelas B

Setelah di jalankan dengan inputan yang telah di tentukan dapat di lihat seperti gambar berikut
:

Di mana program membaca nilai akar adalah -1.2405 untuk persamaan
f(x) = 2x
3
+ 1x
2
+ 3x + 6
untuk metode scanning toleransi di atur sedemikian rupa agar di peroleh titik yang dapat
menampilkan nilai f(x) = 0.

Abdul Azis Rahmansyah
P2700213006
Kelas B

plot untuk metode scanning seperti berikut :

Di mana titik f(x) = 0 di tandai dengan titik o pada plot sebagai tanda bahwa terdapat nilai x
yang melewati titik sehingga f(x) = 0

Sedang untuk metode Newton raphson hasil perbandingan nilai perhitungan yang tampil di
command matlab sebagai berikut :


Abdul Azis Rahmansyah
P2700213006
Kelas B


Untuk metode Secant hasil perbandingan nilai perhitungan sebagai berikut :


Untuk listing program sebagai berikut :
Listing GUI metode scanning dan listing plot scanning
function run_Callback(hObject, eventdata, handles)
% hObject handle to run (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
As = get(handles.A,'string');
Bs = get(handles.B,'string');
Cs = get(handles.C,'string');
Ds = get(handles.D,'string');
Es = get(handles.E,'string');
mins = get(handles.minimum,'string');
maxs = get(handles.maximum,'string');
intervals = get(handles.interval,'string');
stoleransi = get(handles.stoleransi,'string');
A=str2num(As);
B=str2num(Bs);
C=str2num(Cs);
D=str2num(Ds);
E=str2num(Es);
minxx=str2num(mins);
maxxx=str2num(maxs);
intervalxx=str2num(intervals);
Tol=str2num(stoleransi);
akar=[];
a=1;
d=1;
x=minxx:intervalxx:maxxx;
akhir=maxxx-minxx+1;
z=length(x);
akar=[];
for j=1 :z
y(j) = (A*x(j)^4) +(B*x(j)^3) + (C*x(j)^2) + (D*x(j)) + E;
% pengujian pencarian titik 0
if y(j)>=(-Tol) && y(j)<=(Tol)
Abdul Azis Rahmansyah
P2700213006
Kelas B

akar(d)=x(j);
d=d+1;
end
% save data y;
end
akar
set(handles.hasilscan,'string',akar);
ind=find(y >=(-Tol) & y <= Tol);
xx=minxx:intervalxx:maxxx;
axes(handles.axes1); cla; plot(xx,y,xx(ind),y(ind),'o');axis tight;
% axes(handles.axes1); cla; plot(y);axis tight;text(0.2,0,'\fontsize{8}
P2700213006')
handles.y=y;
handles.Tol=Tol;
handles.minxx=minxx;
handles.maxxx=maxxx;
handles.akhir=akhir;
handles.intervalxx=intervalxx;
guidata(hObject,handles);

% --- Executes on button press in grafik.
function grafik_Callback(hObject, eventdata, handles)
% hObject handle to grafik (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(gcbo);
y=handles.y;
Tol=handles.Tol;
minxx=handles.minxx;
maxxx=handles.maxxx;
intervalxx=handles.intervalxx;
akhir=handles.akhir;

ind=find(y >=(-Tol) & y <= Tol);
x=minxx:intervalxx:maxxx;
figure(1),plot(x,y,x(ind),y(ind),'o'),axis tight,grid

Listing cek turunan, Listing metode Newton raphson dan listing plot Newton raphson
% --- Executes on button press in nrcek.
function nrcek_Callback(hObject, eventdata, handles)
% hObject handle to nrcek (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Asnr = get(handles.nrA,'string');
Bsnr = get(handles.nrB,'string');
Csnr = get(handles.nrC,'string');
Dsnr = get(handles.nrD,'string');
Esnr = get(handles.nrE,'string');
Anr=str2num(Asnr);
Bnr=str2num(Bsnr);
Cnr=str2num(Csnr);
Dnr=str2num(Dsnr);
Enr=str2num(Esnr);
syms x;
y = (Anr*x^4) +(Bnr*x^3) + (Cnr*x^2) + (Dnr*x) + Enr;
z=diff(y);
l=char(z);
set(handles.nrhasilcek,'string',l);

Abdul Azis Rahmansyah
P2700213006
Kelas B


% --- Executes on button press in nrrun.
function nrrun_Callback(hObject, eventdata, handles)
% hObject handle to nrrun (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Asnr = get(handles.nrA,'string');
Bsnr = get(handles.nrB,'string');
Csnr = get(handles.nrC,'string');
Dsnr = get(handles.nrD,'string');
Esnr = get(handles.nrE,'string');
Fsnr = get(handles.nrF,'string');
Gsnr = get(handles.nrG,'string');
Hsnr = get(handles.nrH,'string');
Isnr = get(handles.nrI,'string');
awalnr = get(handles.nrawal,'string');
epsilonnr = get(handles.nrepsilon,'string');
Anr=str2num(Asnr);
Bnr=str2num(Bsnr);
Cnr=str2num(Csnr);
Dnr=str2num(Dsnr);
Enr=str2num(Esnr);
Fnr=str2num(Fsnr);
Gnr=str2num(Gsnr);
Hnr=str2num(Hsnr);
Inr=str2num(Isnr);
X=str2num(awalnr);
Ep=str2num(epsilonnr);
i=0;
M=9;
xb=0;
disp('_________Metode Newton Raphson_________________');
disp('_______________________________________________');
disp(' i xi f(xi) epsilon');
disp('_______________________________________________');
a=1;
b=1;
while (Ep<M)
fx= (Anr*X^4) +(Bnr*X^3) + (Cnr*X^2) + (Dnr*X) + Enr;
gx= (Fnr*X^3) + (Gnr*X^2) + (Hnr*X) + Inr;
fxx(a)=fx;
xb=X-(fx/gx);
M= abs(X-xb);
X=xb;
xc(b)=xb;
i=i+1;
a=a+1;
b=b+1;
fprintf('%3.0f %12.6f %12.6f %12.6f\n',i,xb,fx,M);
end
disp('------------------------------')
fprintf('Akarnya Adalah = %10.8f\n',xb);
set(handles.nrakar,'string',xb);
fx= (Anr*X^4) +(Bnr*X^3) + (Cnr*X^2) + (Dnr*X) + Enr;
gx= (Fnr*X^3) + (Gnr*X^2) + (Hnr*X) + Inr;
fxx(a)=fx;
xb=X-(fx/gx);
M= abs(X-xb);
X=xb;
Abdul Azis Rahmansyah
P2700213006
Kelas B

xc(b)=xb;
i=i+1;
a=a+1;
b=b+1;
axes(handles.nrhasilplot); cla; plot(xc,fxx);axis tight;
handles.xc=xc;
handles.fxx=fxx;
guidata(hObject,handles);



% --- Executes on button press in nrplot.
function nrplot_Callback(hObject, eventdata, handles)
% hObject handle to nrplot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(gcbo);
xc=handles.xc;
fxx=handles.fxx;
figure(2),plot(xc,fxx),grid on


Listing metode secant dan listing plot secant
% --- Executes on button press in runse.
function runse_Callback(hObject, eventdata, handles)
% hObject handle to runse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Ase = get(handles.Ase,'string');
Bse = get(handles.Bse,'string');
Cse = get(handles.Cse,'string');
Dse = get(handles.Dse,'string');
Ese = get(handles.Ese,'string');
x1se = get(handles.x1se,'string');
x2se = get(handles.x2se,'string');
tolse = get(handles.tolse,'string');
Ae=str2num(Ase);
Be=str2num(Bse);
Ce=str2num(Cse);
De=str2num(Dse);
Ee=str2num(Ese);
x1e=str2num(x1se);
x2e=str2num(x2se);
tole=str2num(tolse);
disp('Program Metode Secant');
disp('=============================');

i=0;
M=9;
disp('_______________________________________________');
disp(' i xi f(xi) epsilon');
disp('_______________________________________________');
a=1;
b=1;
while (tole<M)
fx= Ae*(x1e)^4 + Be*(x1e)^3 + Ce*(x1e)^2 + De*(x1e) + Ee;
fxb= Ae*(x2e)^4 + Be*(x2e)^3 + Ce*(x2e)^2 + De*(x2e) + Ee;
fxse(a)=fx;
Abdul Azis Rahmansyah
P2700213006
Kelas B

d = x2e - (fxb*(x2e-x1e)/(fxb-fx));
M=abs(x1e-x2e);
x1e = x2e;
x2e = d;
x22(b)=x2e;
i=i+1;
a=a+1;
b=b+1;
fprintf('%3.0f %12.6f %12.6f %12.6f\n',i,x2e,fx,M);
end;
disp('_______________________________________________');
fprintf('Akarnya Adalah = %10.8f\n',x2e);
set(handles.akarse,'string',x2e);
axes(handles.sehasilplot); cla; plot(x22,fxse);axis tight;
handles.x22=x22;
handles.fxse=fxse;
guidata(hObject,handles);

% --- Executes on button press in plotse.
function plotse_Callback(hObject, eventdata, handles)
% hObject handle to plotse (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(gcbo);
x22=handles.x22;
fxse=handles.fxse;
figure(3),plot(x22,fxse),grid on