Anda di halaman 1dari 1

tugassodiq2.

java 24/Jun/2010
1 import java.text.DecimalFormat;
2 class sodiq2
3 {
4 /*menghitung akar f(x)=3e^x-2x^2 + 5 dengan metoda bagi dua
5 */
6 static double fx(double x)
7 {
8 double f_x;
9 double e=2.718281828459;
10 f_x = (3*Math.pow(e,x))-(2*Math.pow(x,2))+5;
11 return f_x;
12 }
13 public static void main(String args[])
14 {
15 int i; //untuk menghitung banyaknya iterasi
16 DecimalFormat df1=new DecimalFormat("##0.000000");
17 double e1=0.000001; //batas selang setelah iterasi
18 double e2=0.0000001; //batas nilai f(x) mendekati nol
19 double fa,fb,fc;
20 fc=1000;//nilai fungsi f(x) pada titik a,b,c
21 double a,b,c;//variabel untuk selang a,b dan c sebagai titik tengah
22 a=-1.7;b=-1.6;c=0;//nilai selang awal a dan b
23 i=0;
24 System.out.println("----------------------------------------------------------");
25 System.out.println("i\t\ta\t\t\t\tb\t\t\tc\t\t\tf(c)");
26 System.out.println("----------------------------------------------------------");
27 while((Math.abs(a-b)>e1)||(fc>e2))
28 {
29 c=(a+b)/2;
30 fa=fx(a);fx(b);fc=fx(c);
31 System.out.println(i+" "+df1.format(a)+"\t\t"+ df1.format(b)+"\t\t" +
df1.format(c)+"\t\t"+df1.format(fc));
32 if((fa*fc)<0)
33 {
34 //selang baru a-c
35 b=c;
36 }
37 else
38 {
39 //selang baru b-c
40 a=c;
41 }
42 i++;
43 }
44 System.out.println("----------------------------------------------------------");
45 System.out.println("Hampiran akar = " + df1.format(c));
46 }
47 }

1 of 1

Anda mungkin juga menyukai