diff --git a/src/Main.java b/src/Main.java index 75c0aba..135e736 100644 --- a/src/Main.java +++ b/src/Main.java @@ -17,7 +17,7 @@ import javax.swing.border.EmptyBorder; */ public class Main extends JFrame { - private static final int NUM_OF_LABS = 5; + private static final int NUM_OF_LABS = 6; public static void main(String[] args) { try { @@ -72,7 +72,9 @@ public class Main extends JFrame { Class cls = Class.forName("com.nummethods.lr"+num+".LR_"+num); Method meth = cls.getMethod("main", String[].class); meth.invoke(null, (Object) null); - } catch (Exception cne) {} + } catch (Exception cne) { + cne.printStackTrace(); + } } }); return button; diff --git a/src/com/nummethods/lr6/LR_6.java b/src/com/nummethods/lr6/LR_6.java new file mode 100644 index 0000000..4019643 --- /dev/null +++ b/src/com/nummethods/lr6/LR_6.java @@ -0,0 +1,74 @@ +package com.nummethods.lr6; + +import com.nummethods.lr4.Function; + +/** + * + * @author aNNiMON + */ +public class LR_6 { + + private final Function func = var10; + + private static final Function var2 = new Function() { + + @Override + public double f(double x) { + return Math.cos(x) * Math.cos(x) - (x * x); + } + }; + + private static final Function var3 = new Function() { + + @Override + public double f(double x) { + return Math.pow(x, 7) - Math.pow(x, 5) - x + 3; + } + }; + + private static final Function var10 = new Function() { + + @Override + public double f(double x) { + return x - Math.pow(Math.E, -x); + } + }; + + public static void main(String[] args) { + new LR_6().calc(); + } + + private final double a = -10, b = 10; + private final double h = 0.001, epsilon = 0.0001; + + public void calc() { + double result = calcBisection(a, b); + System.out.println("Метод Бисекций: x = " + result); + System.out.println("0 = " + func.f(result)); + + result = calcNewton( (a + b) / 2 ); + System.out.println("Метод Ньютона: x = " + result); + System.out.println("0 = " + func.f(result)); + } + + /** ---- Метод Бисекций ---- */ + private double calcBisection(double a, double b) { + double x = (a + b) / 2; + if (func.f(x) == 0) return x; + else if ((b - a) > epsilon) { + if (func.f(a) * func.f(x) < 0) return calcBisection(a, x); + else return calcBisection(x, b); + } + return (a + b) / 2; + } + + /** ---- Метод Ньютона ---- */ + private double calcNewton(double x) { + double a = x - (func.f(x) / derivate(x)); + return (Math.abs(func.f(a)) > epsilon) ? calcNewton(a) : x; + } + + private double derivate(double x) { + return (func.f(x + h) - func.f(x - h)) / (2 * h); + } +}