Month: April 2014

Numerical Methods in Pascal: Polynomial Root Finder

program polinomial; {Mencari seluruh akar polinom}
{https://syeilendrapramuditya.wordpress.com}
uses crt;

const
  max = 100;
var
  n  : integer;
  a  : array[-1..max] of real;
  b  : array[-1..max] of real;
  iter,i,j,k   : longint;
  x0,x1,t,beda : real;

begin
     clrscr;
     writeln;
     writeln('           Program Polinom');
     writeln('           ===============');
     writeln;
     write('Masukan nilai tebakan awal (misal : 5) : ');
     iter := 0;
     readln(x0);
     write('Masukan orde polinomial : ');readln(n);
     { misal (x-1)*(x+1)*(x+2) = x^3+2*x^2-x-2 }
     writeln('Masukan satu per satu koefisien polinom');
     writeln('dimulai dari suku polinom dengan orde terendah (x pangkat nol)');
     for i := 0 to n do
     begin
       write('masukan koefisien ke ',i,' : ');
       readln(a[i]);
     end;
     writeln;
     repeat
       repeat
           iter := iter +1;
          b[n-1] := a[n];
          t := b[n-1];
          for j := n downto 2 do
          begin
            b[j-2] := b[j-1]*x0+a[j-1];
            t := t*x0+b[j-2];
          end;
          b[-1] := b[0]*x0 + a[0];
          x1 := x0 - b[-1]/t;
          beda := (x1-x0)/x0;
          x0 := x1;
       until (abs(beda)<1e-6)or (iter>1e6);
     n := n-1;
     writeln('nilai x',n+1, ' adalah',x1:10:4 );
     for k := 0 to n do a[k] := b[k];
     until (n<=0);
     readln;
end.

Numerical Methods in Pascal: Euler 2D

Program Planetary_Motion;
{https://syeilendrapramuditya.wordpress.com}
{Gerak bulan mengitari bumi karena pengaruh gaya gravitasi}
{Data t[i],x[i],y[i],vx[i],vy[i] ditulis ke dalam file excel (D:\orbit.xls) }
{Data D:\orbit.xls tersebut kemudian diplot menggunakan Excel}
uses crt;
const GM = 3.986344692e14;
Type
 DataPlot = record
            dataT,dataX,dataY,dataVX,dataVY : real;
            end;
 ListData = array[0..1000] of DataPlot;
var t0,x0,y0,vx0,vy0,m,k,b,t,x,y,vx,vy,x12,y12,vx12,vy12,dt : real;
    i,N : integer;
    FileData : text;
    S:ListData;

Begin
clrscr;
writeln;
writeln('      Solusi Numerik PDB Orde 2 Terkopling');
writeln('              Kasus  Gerak  Bulan         ');
writeln;
writeln('Nilai konstanta yang digunakan dalam perhitungan :');
writeln('Massa Bumi  = 5.9742e24 Kg               ');
writeln('Massa Bulan = 7.3500e24 Kg               ');
writeln('d           = 3.844e5 Km                 ');
writeln('T           = 27.322 hari                ');
writeln('G           = 6.6726e-11 N*sqr(m)/sqr(Kg ');
writeln;
writeln('Nilai parameter (default) yang digunakan dalam perhitungan :');
writeln;
writeln('x0  = 3.844e5 ');
writeln('y0  = 0       ');
writeln('vx0 = 0       ');
writeln('vy0 = 32000   ');
writeln('dt  = 1       ');
writeln('N   = 1000    ');
writeln;
write('Masukan nilai x0,y0,vx0,vy0 pada t = 0  : ');readln(x0,y0,vx0,vy0);
write('Masukan parameter iterasi (dt dan N) : ');readln(dt,N);
writeln;
t0 := 0;
Assign(FileData,'D:\orbit.xls');
rewrite(FileData);
 S[0].DataT  := 0;
 S[0].DataX  := x0;
 S[0].DataY  := y0;
 S[0].DataVX := vx0;
 S[0].DataVY := vy0;
 writeln(FileData,'      t               x               y               vx               vy');
 writeln(FileData,s[0].dataT:20:5,s[0].dataX:20:5,s[0].DataY:20:5,s[0].DataVX:20:5,s[0].DataVY:20:5);
for i := 1 to N do
 begin
 x12  := x0 + (dt/2) * vx0;
 vx12 := vx0 - ( (GM * x0) /  ( exp(1.5 * ln( sqr(x0) + sqr(y0) ))) ) * (dt/2);
 y12  := y0 + (dt/2) * vy0;
 vy12 := vy0 - ( (GM * y0) /  ( exp(1.5 * ln( sqr(x0) + sqr(y0) ))) ) * (dt/2);
 x  := x0 + dt * vx12;
 vx := vx0 - ( (GM * x12)  /  (exp(1.5 * ln(sqr(x0) + sqr(y0) )))) * (dt);
 y  := y0 + dt * vy12;
 vy := vy0 - ( (GM * y12)  /  (exp(1.5 * ln(sqr(x0) + sqr(y0) )))) * (dt);
 t := t0 + dt;
 S[i].DataT  :=  t;
 S[i].DataX  :=  x;
 S[i].DataY  :=  y;
 S[i].DataVX := vx;
 S[i].DataVY := vy;
 writeln(FileData,s[i].dataT:20:5,s[i].dataX:20:5,s[i].DataY:20:5,s[i].DataVX:20:5,s[i].DataVY:20:5);
 t0  :=  t;
 x0  :=  x;
 y0  :=  y;
 vx0 := vx;
 vy0 := vy;
 end;
close(FileData);
writeln('Data t,x,y,vx,vy telah disimpan di D:\orbit.xls , silahkan buka file tersebut');
readln;
end.

Numerical Methods in Pascal: Modified Euler

Program Modified_Euler;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
var h,x0,y0,x12,y12,x,y : real;
    i,N : integer;

Function Fxy(x,y:real):real;
begin
Fxy := sqrt(x*x + y*y);
end;

Begin
clrscr;
write('Masukan nilai x0 dan y0 : ');readln(x0,y0);
write('Masukan nilai h dan N   : ');readln(h,N);
x:=x0;
for i := 1 to N do
 begin
 x12 := x0 + h/2;
 y12 := y0 + (h/2) * Fxy(x0,y0);
 x := x0 + h;
 y := y0 + h*Fxy(x12,y12);
 writeln('x = ',x:10:5,'    y = ',y:10:5);
 x0:=x;
 y0:=y;
 end;
readln;
end.

Numerical Methods in Pascal: Simple Euler

Program Simple_Euler;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
var h,x0,y0,x,y : real;
    i,N : integer;

Function Fxy(x,y:real):real;
begin
Fxy := sqrt(x*x + y*y);
end;

Begin
clrscr;
write('Masukan nilai x0 dan y0 : ');readln(x0,y0);
write('Masukan nilai h dan N   : ');readln(h,N);
x:=x0;
for i := 1 to N do
 begin
 x := x0 + h;
 y := y0 + h*Fxy(x0,y0);
 writeln('x = ',x:10:5,'    y = ',y:10:5);
 x0:=x;
 y0:=y;
 end;
readln;
end.

Bye XL. Hello Simpati!

Akhirnya ganti juga.

Selama ini saya pakai XL untuk koneksi internet dari smartphone saya.

Tapi kualitasnya sangat mengecewakan, sinyalnya sering sekali hilang, kalaupun ada sinyal, koneksinya lambaaat bgt..

Entah kenapa saya kok tahan berbulan2 menggunakan XL, padahal hampir setiap saat saya menggerutu dan mengeluh karena sinyal XL yang hampir selalu hilang..

Minggu lalu seorang rekan merekomendasikan Simpati, katanya sinyalnya bagus dan koneksi internet juga relatif lancar.

So tanggal 26 kemarin saya beli perdana Simpati beserta pulsa seharga IDR 55k.

Sebagai perbandingan XL vs Simpati:

  • XL: Paket XmartPlan HotRod 3G+, 1.5GB/30 hari, IDR 49k
  • Simpati: Paket MaxiLoop 2.0GB/30 hari, IDR 49k

Hasilnya? Well, dua hari ini saya sengaja test kualitas data connection nya secara terus menerus, baik menggunakan browser, maps, apps, dan macam2 messenger (Whatsapp, LINE, BBM).

So far saya puas dengan kualitas koneksinya. Secara umum selalu lancar, sinyal juga hampir tidak pernah hilang.

Let’s see apa memang kualitas Simpati akan terus bagus seperti ini.

Bye XL, I’ll never miss you.

XL sebelah kiri (tidak ada sinyal), Simpati sebelah kanan (sinyal kuat)
XL sebelah kiri (tidak ada sinyal), Simpati sebelah kanan (sinyal kuat)

 

Numerical Methods in Pascal: Root Finding by Hybrid Bisection-NR

Program Hybrid; {Bisection - Newton Rhapson Combined}
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Function df(x:real):real;
begin
df:=2*x;
end;

Procedure BacaInput(var a,b:real);
begin
clrscr;
repeat
write('Masukan interval evaluasi : ');readln(a,b);
until(f(a)*f(b) < 0);
end;

Procedure Bisection(var a,b,newrootB:real);
begin
NewRootB := (a+b)/2;
IF (f(a)*f(newrootB) < 0) then
b:=newrootB
else
a := newrootB;
end;

Procedure NewtonRhapson(var b:real;var newrootNR:real);
begin
newrootNR := b - f(b)/df(b);
end;

Procedure TulisOutput(newrootB:real);
begin
writeln('Akar : ',newrootB:10:2);
end;

var a,b,c,d,newrootNR,newrootB : real;
    iterNR,iterBi,iter:integer;

Begin
iterNR := 0;iterBi:=0;
BacaInput(a,b);
c:=a;d:=b;
Repeat
begin
NewtonRhapson(b,newrootNR);
IF (newrootNR >= c) and (newrootNR <= d) then
  begin
  inc(iterNR);
  b := newrootNR;
  NewtonRhapson(b,newrootNR);writeln('Newton Rhapson');
  end
else
  begin
   inc(iterBi);
   writeln('Bisection');
   Bisection(a,b,NewrootB);
   end;
end;
Until (abs(f(newrootNR)) < 1e-6) OR (abs(f(newrootB)) < 1e-6)  OR (iter = 10000);
writeln;
if abs(f(newrootNR)) < abs(f(newrootB)) then
writeln('Akar = ',newrootNR:10:2)
else
writeln('Akar = ',newrootB:10:2);
writeln;
writeln('Iterasi Bisection      : ',iterBi);
writeln('Iterasi Newton Rhapson : ',iterNR);
writeln('Iterasi Total          : ',iterBi + iterNR);
readln;
end.

Numerical Methods in Pascal: Root Finding by Regula False (RF)

Program RegulaFalse;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Procedure BacaInput(var a,b : real);
begin
repeat
write('Masukan batas kiri dan kanan : ');readln(a,b);
until (f(a)*f(b) < 0);
end;

Procedure Proses(a,b:real;var akar:real;var iter : integer);
var err : real;
begin
iter := 0;
repeat
inc(iter);
akar := a - f(a)*( (b-a) / (f(b) - f(a)) );
err := abs(f(akar));
if (f(a) * f(akar) < 0) then
b := akar
else
a := akar;
until (err < 1e-6) OR (iter = 10000);
end;

Procedure TulisOutput(akar:real);
begin
writeln('Akar = ',akar:10:3);
end;

var a,b,akar : real;
    iter : integer;

begin
clrscr;
BacaInput(a,b);
Proses(a,b,akar,iter);
TulisOutput(akar);
writeln('Iterasi = ',iter:10);
readln;
end.

Numerical Methods in Pascal: Root Finding by Newton-Rhapson (NR)

Program NewtonRhapson;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f:=sqr(x) - 9;
end;

Function df(x:real):real;
begin
df:=2*x;
end;

Procedure BacaInput(var x:real);
begin
write('Masukan tebakan awal : ');readln(x)
end;

Procedure Proses(var x:real;var iter :integer);
var err:real;
begin
iter := 0;
repeat
 begin
 inc(iter);
 x := x - f(x)/df(x);
 end;
until(abs(f(x)) < 1e-6);
end;

Procedure TulisOutput(x1:real);
begin
writeln('Akarnya adalah : ',x1:10:2);
end;

var x,x1:real;
    iter : integer;
begin
clrscr;
BacaInput(x);
Proses(x,iter);
TulisOutput(x);
writeln('Iterasi = ',iter:10);
readln;
end.

Numerical Methods in Pascal: Root Finding by Bisection

Program Bisection;
{https://syeilendrapramuditya.wordpress.com}
uses crt;
Function f(x:real):real;
begin
f := sqr(x) - 9;
end;

Procedure BacaInput(var a,b:real);
begin
repeat
write('Masukan batas kiri dan kanan : ');
Readln(a,b);
until (f(a)*f(b) < 0);
end;

Procedure Proses(a,b:real;var NewRoot:real;var iter :integer);

begin
iter:=0;
Repeat
begin
 inc(iter);
 NewRoot:=(a+b)/2;
 IF (f(a)*f(NewRoot) < 0) then
 b := NewRoot
 else
 a:=NewRoot;
 end;
until (abs(f(NewRoot)) < 1e-6) OR (iter = 10000);
end;

Procedure TulisAkar(NewRoot:real);
begin
writeln('Akar dari persamaan adalah : ',NewRoot:10:2);
end;

var a,b,NewRoot : real;
    iter:integer;
Begin
clrscr;
BacaInput(a,b);
Proses(a,b,NewRoot,iter);
TulisAkar(NewRoot);
writeln('Iterasi = ',iter:10);
readln;
end.