[java] Fungsi1
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import java.util.Scanner;
- import java.util.InputMismatchException;
- public class Fungsi1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double lowerBound, upperBound, tolerance;
- int maxIterations;
- try {
- System.out.println("Mencari akar persamaan Dengan Metode Bagi dua/ Biseksi");
- System.out.print("\nMasukkan batas bawah: ");
- lowerBound = scanner.nextDouble();
- System.out.print("Masukkan batas atas: ");
- upperBound = scanner.nextDouble();
- System.out.println("\nNilai fungsi pada batas bawah f(" + String.format("%.4f", lowerBound) + ") = " + String.format("%.4f", function(lowerBound)));
- System.out.println("Nilai fungsi pada batas atas f(" + String.format("%.4f", upperBound) + ") = " + String.format("%.4f", function(upperBound)));
- System.out.print("\nMasukkan toleransi error: ");
- tolerance = scanner.nextDouble();
- System.out.print("Masukkan jumlah iterasi maksimum: ");
- maxIterations = scanner.nextInt();
- } catch (InputMismatchException e) {
- System.out.println("Input tidak valid. Masukkan angka.");
- return;
- }
- double root = findRoot(lowerBound, upperBound, tolerance, maxIterations);
- if (!Double.isNaN(root)) {
- System.out.printf("\nAkar persamaan x + exp(x) adalah: %.4f\n", root);
- } else {
- System.out.println("\nTidak dapat menemukan akar dalam batas yang ditentukan.");
- }
- }
- private static double findRoot(double lowerBound, double upperBound, double tolerance, int maxIterations) {
- double root = Double.NaN;
- double error = upperBound - lowerBound;
- System.out.println("\nIterasi xb xc xa f(xb) f(xc) f(xa) error");
- for (int i = 1; i <= maxIterations; i++) {
- double mid = (lowerBound + upperBound) / 2.0;
- double fLowerBound = function(lowerBound);
- double fMid = function(mid);
- System.out.printf("%7d %.4f %.4f %.4f %.4f %.4f %.4f %.4f%n", i, lowerBound, mid, upperBound, fLowerBound, fMid, function(upperBound), error);
- if (Math.abs(fMid) < tolerance) {
- root = mid;
- break;
- } else if (fLowerBound * fMid > 0) {
- lowerBound = mid;
- } else {
- upperBound = mid;
- }
- error = upperBound - lowerBound;
- }
- return root;
- }
- private static double function(double x) {
- return x + Math.exp(x);
- }
- }
Editor
You can edit this paste and save as new: