0% found this document useful (0 votes)
66 views

Contoh PROGRAM Tugas 1 Konversi Bilangan Biner Ke Desimal

This document contains examples of programs written in Pascal for numerical methods exercises. The programs demonstrate various numerical methods including: 1. Binary to decimal conversion 2. Finite difference tables 3. Divided differences tables 4. Bisection method 5. Regula Falsi method 6. Modified Regula Falsi 7. Iteration method 8. Aitken's method for accelerating convergence 9. Newton-Raphson method 10. Data fitting 11. Numerical differentiation The programs include inputs, outputs, calculations, and formatting to illustrate how to code the numerical methods in a programming language. Comments are also included in English to explain the purpose and steps

Uploaded by

Thoha Firdaus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Contoh PROGRAM Tugas 1 Konversi Bilangan Biner Ke Desimal

This document contains examples of programs written in Pascal for numerical methods exercises. The programs demonstrate various numerical methods including: 1. Binary to decimal conversion 2. Finite difference tables 3. Divided differences tables 4. Bisection method 5. Regula Falsi method 6. Modified Regula Falsi 7. Iteration method 8. Aitken's method for accelerating convergence 9. Newton-Raphson method 10. Data fitting 11. Numerical differentiation The programs include inputs, outputs, calculations, and formatting to illustrate how to code the numerical methods in a programming language. Comments are also included in English to explain the purpose and steps

Uploaded by

Thoha Firdaus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CONTOH

PROGRAM Tugas 1

KONVERSI BILANGAN BINER


KE DESIMAL

Program biner;
uses wincrt;
var

a,b:array [0..10] of byte;


n,i:byte;

BEGIN
clrscr;
writeln('
Program Biner');
writeln('-----------------------------');
write ('masukkan jumlah bit:');

read (n);

for i:= n downto 0 do


begin
write ('a[',i,'] = ');readln(a[i]);
end;

writeln('-----------------------------');
writeln('');
b[n]:=a[n];
writeln ('b[',n,']=',b[n]);
for i:= n-1 downto 0 do
begin
b[i]:=a[i]+2*b[i+1];
writeln ('b[',i,']=',b[i]);
b[i+1]:=b[i];
end;
writeln('-----------------------------');
writeln;
write('Bilangan yang akan dikonversi = ');
write('[');
for i:=n downto 0 do
begin
write (a[i]);
end;
writeln(']2');
writeln('');
write('Hasil Konversinya = ',b[i])
readln;
END.

15

Contoh
Program Tugas 2

Tabel Selisih Berhingga

PROGRAM SELISIH_BERHINGGA;
uses wincrt;
var
f,df,d2f,d3f : array[1..100] of integer;
i,n
: integer;
begin
clrscr;
writeln('f(x):=x^3-3x+6x+9');
write('Masukkan n = ');readln(n);
writeln('---------------------------------------------------');
writeln('x
f(x)
df(x)
d2f(x)
d3f(x)
');
writeln('---------------------------------------------------');
for i:=0 to n do
begin
f[i]:=i*i*i-3*i+6*i+9;
gotoxy(1,6+2*i);
writeln(i);
gotoxy(5,6+2*i);
writeln('f(',i,')= ',f[i]);
end;
for i:=0 to n-1 do
begin
df[i]:=f[i+1]-f[i];
gotoxy(16,6+(2*i)+1);
writeln('df(',i,')= ',df[i]);
end;
for i:=0 to n-2 do
begin
d2f[i]:=df[i+1]-df[i];
gotoxy(28,6+(2*i)+2);
writeln('d2f(',i,')= ',d2f[i]);
end;
for i:=0 to n-3 do
begin
d3f[i]:=d2f[i+1]-d2f[i];
gotoxy(42,6+(2*i)+3);
writeln('d3f(',i,')= ',d3f[i]);
end;
gotoxy(1,15+(2*i));
writeln('---------------------------------------------------');
end.

16

Contoh
program Tugas 3

Tabel Selisih Bagi

PROGRAM SELISIH_BAGI;
uses wincrt;
var
x,y : array[1..100] of integer;
sb1y,sb2y,sb3y : array[1..100] of real;
i,n
: integer;
BEGIN
clrscr;
write('Jumlah data: ');readln(n);
writeln('Masukkan datanya');
for i:=0 to n do
begin
gotoxy(1,4+i);
write('x[',i,']:'); readln(x[i]);
end;
for i:=0 to n do
begin
gotoxy(10,4+i);
write('y[',i,']:'); readln(y[i]);
end;
clrscr;
writeln('---------------------------------------------------');
writeln('i
x
y
sb1y
sb2y
sb3y
');
writeln('---------------------------------------------------');
for i:=0 to n do
begin
gotoxy(1,4+2*i);
writeln(i);
gotoxy(5,4+2*i);
writeln(x[i]);
gotoxy(12,4+2*i);
writeln(y[i]);
end;
for i:=0 to n-1 do
begin
sb1y[i]:=(y[i+1]-y[i])/(x[i+1]-x[i]);
gotoxy(18,4+2*i+1);
writeln(' ',sb1y[i]:4:2);
end;
for i:=0 to n-2 do
begin
sb2y[i]:=(sb1y[i+1]-sb1y[i])/(x[i+2]-x[i]);;
gotoxy(30,4+2*i+2);
writeln(' ',sb2y[i]:4:2);
end;
for i:=0 to n-3 do
begin
sb3y[i]:=(sb2y[i+1]-sb2y[i])/(x[i+3]-x[i]);
gotoxy(42,4+2*i+3);

17

writeln(' ',sb3y[i]:4:2);
end;
gotoxy(1,4+2*n+2);
writeln('---------------------------------------------------');
gotoxy(1,4+2*n+3);
writeln('Ket: sb1y = selisih bagi tingkat 1 untuk fungsi y');
END.

18

Contoh

Metode Biseksi (Bagi dua)

Program Tugas 4
PROGRAM BISEKSI;
uses wincrt;
var
a,b,m,fa,fb,fm
i,n

: real;
: integer;

Function F(x:real) : real;


begin
F:=sqr(x*x)-2*x-5;
end;
BEGIN
writeln('
Program Biseksi');
write('batas kiri a:');readln(a);
write('batas kanan b:');readln(b);
writeln('----------------------------------------------------------------------------');
writeln(' i
a
b
m
fa
fb
fm
');
writeln('----------------------------------------------------------------------------');
i:=0;
repeat
m:=(a+b)/2;
fa:=F(a);
fb:=F(b);
fm:=F(m);
writeln( ' ',i,'');
gotoxy(7,i+7);
write(a:3:3) ;writeln(' ');
gotoxy(15,i+7);
write(b:3:3) ;writeln(' ');
gotoxy(23,i+7);
write(m:3:3) ;writeln(' ');
gotoxy(32,i+7);
write(fa:11:7);writeln(' ');
gotoxy(48,i+7);
write(fb:11:7);writeln(' ');
gotoxy(61,i+7);
write(fm:11:7);writeln(' ');
if fm*fb>0 then b:=m else a:=m;
i:=i+1;
until abs(fm) <=10e-4;
writeln('-------------------------------------------------------------------------');
writeln;
writeln('Jadi Akar-akarnya =',m:3:3);
readln;
END.

19

Contoh
Program Tugas 5

Metode Regula Falsi

PROGRAM REGULA_FALSI;
uses wincrt;
var
a,b,c,fa,fb,fc
: real;
i,n
: integer;
Function F(x:real) : real;
begin
F:=sqr(x)-5*x+4;
end;
BEGIN
writeln('
PROGRAM REGULA FALSI');
write('batas kiri a:');readln(a);
write('batas kanan b:');readln(b);
writeln('----------------------------------------------------------------------------');
writeln(' i
a
b
c
fa
fb
fc
');
writeln('----------------------------------------------------------------------------');
i:=0;
repeat
fa:=F(a);
fb:=F(b);
c:=b-fb*(b-a)/(fb-fa));
fc:=F(c);
if fc*fb>0 then b:=c else a:=c;
writeln( ' ',i,'');
gotoxy(7,i+7);
write(a:3:3) ;writeln(' ');
gotoxy(15,i+7);
write(b:3:3) ;writeln(' ');
gotoxy(23,i+7);
write(c:3:3) ;writeln(' ');
gotoxy(32,i+7);
write(fa:11:7);writeln(' ');
gotoxy(48,i+7);
write(fb:11:7);writeln(' ');
gotoxy(61,i+7);
write(fc:11:7);writeln(' ');
i:=i+1;
until abs(fc) <=1.0e-4;
writeln('-----------------------------------------------------------------------------');
writeln;
writeln('Jadi Akar-akarnya =',c:3:3);
readln;
END.

20

CONTOH
Praktikum Tugas 6

REGULAFALSI TERMODIFIKASI

PROGRAM REGULA_FALSI_TERMODIFIKASI;
uses wincrt;
var
a,b,c,fa,fb,fc,Ge
: real;
i,n
: integer;
Function F(x:real) : real;
begin
F:=sqr(x)-5*x+4;
end;
BEGIN
writeln('
PROGRAM REGULA FALSI TERMODIFIKASI');
WRITELN('FUNGSI F:=x^2-5x+4');
gotoxy(1,3);write('batas kiri a:');read(a);
gotoxy(24,3);write('batas kanan b:');readln(b);
writeln('-------------------------------------------------------------------------');
writeln(' i
a
b
c
Fa
Fb
Fc
');
writeln('-------------------------------------------------------------------------');
Fa:=F(a);
Fb:=F(b);
i:=0;
Repeat
c:=(a*Fb-b*Fa)/(Fb-Fa);
Fc:=F(c);
writeln( ' ',i,'');
gotoxy(7,i+7);
write(a:3:3) ;writeln(' ');
gotoxy(15,i+7);
write(b:3:3) ;writeln(' ');
gotoxy(23,i+7);
write(c:3:3) ;writeln(' ');
gotoxy(32,i+7);
write(fa:11:7);writeln(' ');
gotoxy(48,i+7);
write(fb:11:7);writeln(' ');
gotoxy(61,i+7);
write(fc:11:7);writeln(' ');
if Fa*Fc <=0 then
begin
b:=c ;
Fa:=Fa/2
end else
begin
a:=c;
Fb:=Fb/2;
end;
i:=i+1;

21

until abs(fc) <=1.0e-4;


writeln('------------------------------------------------------------------------');
writeln;
writeln('Jadi Akar-akarnya =',c:3:3);
readln;
END.

22

CONTOH
Praktikum Tugas 7

METODE ITERASI

Program metode iterasi sebagai berikut:


Program iterasi;
uses wincrt;
var x:array[0..100] of real;
b: real;
i,n:integer;
BEGIN
clrscr;
writeln('Soalnya: f(x)=sqr(x)-5*x+4');
writeln('Rumus Iterasi: x(i+1)=[x(i)^2+4]/5');
write('tebakan awal x[',i,']:=');readln(x[i]);
writeln('--------------------------------------------------------------------');
writeln('i
x(i)
x(i)^2
[x(i)^2+4]/5
x[i+1]-x[i] ');
writeln('--------------------------------------------------------------------');
i:=0;
Repeat
begin
x[i+1]:=(x[i]*x[i]+4)/5;
b:= x[i+1]-x[i];
write(i);
write('
',x[i]:4:3);
write('
',x[i]*x[i]:4:5);
write('
',(x[i+1]):5:5);
writeln('
',b:5:7);
x[i]:=x[i+1];
i:=i+1;
end;
until b<=0.00001;
writeln('-------------------------------------------------------------------');
writeln('akarnya adalah:',x[i]:4:6);
readln;
END.

23

CONTOH
Praktikum Tugas 8

METODE AITKEN (PERCEPATAN


KONVERGENSI)

PROGRAM METODE_AITKEN; {ATAU PERCEPATAN KONVERGENSI}


uses wincrt;
var
x,dx,d2x : array[0..100] of real;
i,n
: integer;
BEGIN
clrscr;
writeln('f(x):=x^2-5x+4');
write('nilai awal x[0]:');readln(x[0]);
n:=2;
writeln('Rumus Iterasi: x[i]:=(x[i-1]^2+4)/5');
writeln('---------------------------------------------------');
writeln('i
x[i]
dx[i]
d2x[i]
');
writeln('---------------------------------------------------');
for i:=0 to n do
begin
x[i]:=(sqr(x[i-1])+4)/5;
gotoxy(1,7+2*i);
write(i);
gotoxy(4,7+2*i);
writeln('x[',i,']=',x[i]:3:4);
x[i-1]:=x[i];
end;
for i:=0 to n-1 do
begin
dx[i]:=x[i]-x[i-1];
gotoxy(18,6+(2*i)+2);
writeln('dx[',i,']=',dx[i]:3:4);
end;
for i:=0 to n-2 do
begin
d2x[i]:=dx[i+1]-dx[i];
gotoxy(35,7+(2*i)+2);
writeln('d2x[',i,']=',d2x[i]:3:4);
end;
x[3]:=x[2]-(sqr(dx[1])/d2x[0]);
gotoxy(1,13+(2*i));
writeln('---------------------------------------------------');
gotoxy(1,15+2*i);
writeln('x[3]:',x[3]:3:4);
END.

24

CONTOH
Praktikum Tugas 9

METODE NEWTON RAPHSON

Program Newton_Raphson;
uses wincrt;
var
x,y,dy:array [0..100] of real;
tol:real;
i:integer;
BEGIN
clrscr;
writeln('f(x)=x^2-5x+4');
write (' Titik awal x[0]: '); readln (x[0]);
writeln('---------------------------------------------------');
writeln('i
x[i]
y(i)
dy(i)
abs(x[i]-x[i-1])');
writeln('---------------------------------------------------');
i:=1;
Repeat
y[i-1]:=sqr(x[i-1])-5*x[i-1]+4;
{fungsi f(x)}
dy[i-1]:=2*x[i-1]-5;
{turunan dari f(x)}
x[i]:=x[i-1]-(y[i-1]/dy[i-1]);{rumus Newton Raphson}
write (i-1 , ' ',x[i-1]:4:4,'
',y[i-1]:4:4,' ',dy[i-1]:4:4);
writeln('
', abs(x[i]-x[i-1]):4:7);
tol:=abs(x[i]-x[i-1]);
i:=i+1;
until i=10;
writeln('--------------------------------------------------');
writeln;
writeln('Akarnya= ',x[i-1]:3:3);
readln;
END.

25

CONTOH
Praktikum Tugas 12

FITTING DATA

PROGRAM FITTING;
uses wincrt;
var
a,b,c,d:real;
x,y:array [1..100] of real;
k1,k2:real;
i,n:integer;
begin
clrscr;
write ('n: ');readln(n);
writeln(' L
T
');
for i:= 1 to n do
begin
gotoxy (2,i+2); readln (x[i]);
gotoxy (10,i+2); readln (y[i]);
end;
a:=0; b:=0; c:=0; d:=0;
for i:= 1 to n do
begin
a:=a+x[i];
b:=b+sqr(x[i]);
c:=c+y[i];
d:=d+x[i]*y[i];
end;
writeln ('a= ',a:4:2,' b= ',b:4:2,' c= ',c:4:2,' d= ',d:4:2);
k1:=(n*d-a*c)/(n*b-sqr(a));
k2:=(c-a*k1)/n;
writeln ('Konstanta-konstanta: ');
writeln ('A=',k1:3,' B= ',k2:3);
writeln ('Pers. Hasil Fitting Liniernya adalah: ');
writeln ('y=',k1:3:2,'x+',k2:3:2);
readln;
end.

26

CONTOH
Praktikum Tugas 13

DIFERENSIASI NUMERIK

Program DEFERENSIAL2;
uses wincrt;
var
dy,dy1,dy2,dy3,dy4,dy5:real;
d2y,d2y1,f2y2,d2y3,d2y4,d2y5:real;
i,n:integer;
x,y,sy,sy1,sy2,sy3,sy4,sy5,sy6:array[1..100] of real;
begin
clrscr;
write ('n= ');readln(n);
for i:= 1 to n do
begin
gotoxy (7,i+1);
write ('x[',i,']: ');readln(x[i]);
end;
for i:= 1 to n do
begin
gotoxy (25,i+1);
write ('y[',i,']: ');readln(y[i]);
end;
clrscr;
writeln (' x
y
sy1 sy2
sy3
sy4
sy5
sy6');
for i:=1 to n do
begin
writeln (x[i]:3:1);
gotoxy (7,2+2*i);
writeln (y[i]:3:1);
end;
for i:= 1 to n-1 do
begin
sy1[i]:=(y[i+1]-y[i]);
gotoxy (16,3+2*i);
writeln (sy[i]:3:4);
end;
for i:= 1 to n-2 do
begin
sy2[i]:=(sy1[i+1]-sy1[i]);
gotoxy (22,4+2*i);
writeln (sy2[i]:3:4);
end;
for i:= 1 to n-3 do
begin
sy3[i]:=(sy2[i+1]-sy2[i]);
gotoxy (27,5+2*i);
writeln (sy3[i]:3:4);
end;
for i:= 1 to n-4 do
begin
sy4[i]:=(sy3[i+1]-sy3[i]);
gotoxy (32,6+2*i);
writeln (sy4[i]:3:4);
end;
for i:= 1 to n-5 do
begin

27

sy5[i]:=(sy4[i+1]-sy4[i]);
gotoxy (37,7+2*i);
writeln (sy5[i]:3:4);
end;
for i:= 1 to n-6 do
begin
sy6[i]:=(sy5[i+1]-sy5[i]);
gotoxy (42,8+2*i);
writeln (sy6[i]:3:4);
end;
dy1:=(sy1[i]-0.5*sy2[i]+(1/3)*sy3[i]-(1/4)*sy4[i]+(1/5)*sy5[i](1/6)*sy6[i]);
gotoxy(1,8+5*i);writeln('turunan pertama: ',dy1);
writeln('turunan kedua: ',d2y1);
end.

28

CONTOH
Praktikum Tugas 14

INTEGRASI NUMERIK

Program Integral_Trapesoid;
uses wincrt;
var
x,h,a,b,fcx,s,int:real;
i,n:integer;
cf,fx:array [1..100] of real;
function F(x:real):real;
begin
F:=3*x*x+2*x-1;
end;
begin;
clrscr;
write ('Batas awal= ');readln(a);
writeln('Batas akhir= ');readln(b);
Fx[1]:=f(0);
h:=0.25;
n:=round((b-a)/h);
for i:=2 to n do
begin
Fx[i]:=f(h);
writeln ('Fx[',i,']=',Fx[i]);
h:=h+0.25;
end;
s:=0;
for i:= 1 to n do
begin
Fcx:=s+Fx[i];
s:=Fcx;
end;
int:=(h/2)*(2*Fcx-Fx[i]-Fx[n]);
writeln;
writeln ('Integral y=',int);
readln;
end.

29

You might also like