Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package metodenumerik;
- /**
- * @author Kelompok3_RK_3IA14
- **/
- public class Secant {
- public static void main(String[] args) {
- float xn2 = 2;
- float xn1 = 1;
- float x = 9999;
- float selisih = x - xn1;
- float error = 0;
- String prefix1 = "X";
- String prefix2 = "Selisih";
- String format = "%-14s%s%n";
- System.out.printf(format, prefix1, " | " + prefix2);
- System.out.println("-----------------------------");
- System.out.printf(format, xn2, " | " + 0);
- System.out.printf(format, xn1, " | " + selisih);
- while(Math.abs(selisih) > error){
- x = xn1 - ((fungsi(xn1) * (xn1-xn2)) / (fungsi(xn1) - fungsi(xn2)));
- selisih = x - xn1;
- System.out.printf(format, x, " | " + selisih);
- xn2 = xn1;
- xn1 = x;
- }
- String prefix3 = "Akar Persamaan";
- String prefix4 = "Error";
- float absolute = Math.abs(selisih);
- System.out.println("-----------------------------");
- System.out.printf(format, prefix3, " : " + x);
- System.out.printf(format, prefix4, " : " + absolute);
- }
- public static float fungsi(float x){
- //fungsi ini bisa diganti dengan fungsi apa pun
- float y = (x*x)-(7*x)-7;
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement