Metode Numerik dengan Menggunakan Processing


Metode Bagi Dua

Listing Program :
import java.text.DecimalFormat;

                /*menghitung akar f(x)=3e^x-2x^2 + 5 dengan metoda bagi dua
                 */
                 static double fx(double x)
                 {
                                double f_x;
                                double e=2.718281828459;
                                f_x = (3*Math.pow(e,x))-(2*Math.pow(x,2))+5;
                                return f_x;
                 }
                 public static void main(String args[])
                 {
                                int i; //untuk menghitung banyaknya iterasi
                                DecimalFormat df1=new DecimalFormat("##0.000000");
                                double e1=0.000001; //batas selang setelah iterasi
                                double e2=0.0000001; //batas nilai f(x) mendekati nol
                                double fa,fb,fc;
                                fc=1000;//nilai fungsi f(x) pada titik a,b,c
                                double a,b,c;//variabel untuk selang a,b dan c sebagai titik tengah
                                a=-1.7;b=-1.6;c=0;//nilai selang awal a dan b
                                i=0;
                                System.out.println("----------------------------------");
                                System.out.println("i\t\ta\t\t\t\tb\t\t\tc\t\t\tf(c)");
                                System.out.println("----------------------------------");
                                while((Math.abs(a-b)>e1)||(fc>e2))
                                {
                                                c=(a+b)/2;
                                                fa=fx(a);fx(b);fc=fx(c);
                                                System.out.println(i+"  "+df1.format(a)+"\t\t"+ df1.format(b)+"\t\t" + df1.format(c)+"\t\t"+df1.format(fc));
                                                if((fa*fc)<0)
                                                {
                                                                //selang baru a-c
                                                                b=c;
                                                }
                                                else
                                                {
                                                                //selang baru b-c
                                                                a=c;
                                                }
                                                i++;
                                }
                                System.out.println("----------------------------------");
                                System.out.println("Hampiran akar = " + df1.format(c));
}

Metode Biseksi

Listing Program :
public static void main(String[]args){
 double x1=-1;
 double x2=3;
 double xt;
 double y1,y2,yt;
 int iterasi=33;

 for(int i=1;i<=iterasi;i++){
 y1=f(x1);
                y2=f(x2);

                xt=(x1+x2)/2;
 yt=f(xt);

 if(y1==0||y2==0){
 break;
 }

 if(y1*yt<0){
 x1=x1;
                                x2=xt;
 }
                                else if(y2*yt<0){
 x1=xt;
 x2=x2;
 }
                                else{
 System.out.println("Tidak terdapat akar " + "dalam interval ["+x1+", "+x2+"]");
                break;
 }

 System.out.println("Iterasi ke-"+i+"\txt = "+xt);
 }
}
 static double f(double x){
 return Math.pow(x,3)+4*Math.pow(x,2)-10;
 }

Metode Regula Falsi

Listing Program :
import java.text.DecimalFormat;

                /*menghitung akar f(x)=3e^x-2x^2 + 5 dengan metoda regula falsi pada selang a=-2 dan b=-1*/
                 public static double fx(double x)
                 {
                                double f_x;
                                double e=2.718281828459;
                                f_x = (3*Math.pow(e,x))-(2*Math.pow(x,2))+5;
                                return f_x;
                 }
                 public static void main(String args[])
                 {
                                int i; //untuk menghitung banyaknya iterasi
                                DecimalFormat df1=new DecimalFormat("##0.000000");
                                double e1=0.000001; //batas lebar selang setelah iterasi
                                double e2=0.0000001; //batas nilai f(x) mendekati nol
                                double fa,fb,fc;
                                fc=1000;//nilai fungsi f(x) pada titik a,b,c
                                double a,b,c;//variabel untuk selang a,b dan c sebagai titik tengah
                                a=-2;b=-1;c=0;//nilai selang awal a dan b
                                i=0;
                                System.out.println("---------------------------------");
                                System.out.println("i\t\ta\t\t\t\tb\t\t\tc\t\t\tf(c)");
                                System.out.println("---------------------------------");
                                while((Math.abs(a-b)>e1))
                                {
                                                fa=fx(a);fb=fx(b);
                                                c=b-(fb*(b-a)/(fb-fa));
                                                fc=fx(c);
                                                System.out.println(i+"  "+df1.format(a)+"\t\t"+ df1.format(b)+"\t\t" + df1.format(c)+"\t\t"+df1.format(fc));
                                                if(Math.abs(fc)<e2)
                                                {
                                                                //akar adalah c
                                                                a=c;b=c;
                                                }
                                                else
                                                {              if(fa*fc<0)b=c;
                                                                else
                                                                                a=c;       
                                                }
                                                i++;
                                }
                                System.out.println("---------------------------------");
                                System.out.println("Hampiran akar = " + df1.format(c));
                 }


Metode Newton-Raphson

Listing Program :
public static void main(String[]args){
 double x=3;
 double y,t;
 int iterasi=33;

 for(int i=1;i<=iterasi;i++){
 y=Math.pow(x,3)+4*Math.pow(x,2)-10;
 t=3*Math.pow(x,2)+8*x;
 x=x-y/t;

 if(y==0) break;
 System.out.println("Iterasi ke-"+i+"\tx= "+x);
 }
}

Metode Secant

Listing Program :
public static void main(String[]args)
{
double a=0.5;
double b=1;
double fa;
double fb;
double c;
double lebar=0;

for(int i=0; i<60; i++)
{
//rumus untuk secant
fa=Math.pow(Math.E,a)-5*Math.pow(a,2);
fb=Math.pow(Math.E,b)-5*Math.pow(b,2);

c=b-(fb*(b-a)/(fb-fa));

lebar=c-a;
a=b;
b=c;
if(fa==fb)
{
break;
}

       //mencetak hasil pencarian akar
System.out.println("Iterasi ke- "+i+"\tNilai tengah : "+c +"\tlebar :"+lebar +"\tGalat:"+fb);
}
}


1 komentar:

Lukas Setiawan mengatakan...

Bagi anda yg lagi belajar metode numerik & butuh aplikasi gratis, silahkan download di http://www.2shared.com/file/n5zbfLYq/Numerical_Methods_by_Lukas_Set.html atau http://upload.ugm.ac.id/503Numerical Methods by Lukas Setiawan STMIK-Indonesia Mandiri.exe

Posting Komentar