Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- public static ArrayList<Double> xn = new ArrayList<>();
- public static ArrayList<Double> absXn = new ArrayList<>();
- public static ArrayList<BigDecimal> absXn1 = new ArrayList<>();
- public static double eps = 0;
- public static int rd = 0;
- public static int n = 0;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- eps = sc.nextDouble();
- rd = rd(eps);
- double x = sc.nextDouble();
- xn.add(x);
- x =sc.nextDouble();
- xn.add(x);
- int k = 0;
- System.out.println("k\tXn\t\t|Xn-Xn-1|");
- for(int i = 0;i<100;i++){
- boolean f = false;
- if(i!=0) {
- if (i == 1) {
- f = true;
- absXn.add(Math.abs(xn.get(i)- xn.get(0)));
- } else {
- double s = iteration(xn.get(i- 2), xn.get(i- 1));
- BigDecimal rounded = new BigDecimal(s).setScale(rd,
- RoundingMode.HALF_UP);
- xn.add(Double.parseDouble(String.valueOf(rounded)));
- s = Math.abs(xn.get(i)- xn.get(i- 1));
- if(Math.abs(s) >= 0.001) {
- f = true;
- rounded = new BigDecimal(s).setScale(rd,
- RoundingMode.HALF_UP);
- absXn.add(Double.parseDouble(String.valueOf(rounded)));
- }else{
- rounded = new BigDecimal(s).setScale(rd+1,
- RoundingMode.HALF_UP);
- absXn1.add(rounded);
- k++;
- }
- }
- }
- if (i == 0) {
- System.out.println(i + "\t" + xn.get(i));
- } else {
- if(f) {
- System.out.println(i + "\t" + xn.get(i) + " \t" + absXn.get(i- 1));
- }else {
- System.out.println(i + "\t" + xn.get(i) + "\t" + absXn1.get(k-1));
- }
- }
- if (i != 0) {
- if(f) {
- if (absXn.get(i- 1) <= eps) {
- n =i-1;
- break;
- }
- }else {
- BigDecimal r = new BigDecimal(eps);
- if (r.compareTo(absXn1.get(k-1))==1) {
- n =i-1;
- break;
- }
- }
- }
- }
- System.out.println("x* = " + xn.get(n));
- }
- public static double func(double x){
- return Math.exp(-0.5*Math.pow(x,2.0))-Math.pow(x, 3.0) + 0.2;
- }
- public static double iteration(Double x1, Double x2){
- return x1- (func(x1)/(func(x2)-func(x1))) * (x2-x1);
- }
- public static int rd(double e){
- int k = 0;
- while(e > 0 && e < 1){
- e *= 10;
- k++;
- }
- return k;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement