Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab1;
- import java.util.Scanner;
- public class Lab1 {
- public static void main(String[] args) {
- double a = 0, b = 2, eps = 0.005;
- int n = 9;
- System.out.println("Алгоритм блочного равномерного поиска - 1");
- System.out.println("Метод касательных - 2");
- System.out.println("Выход - 3");
- int number = 0;
- Scanner sc = new Scanner(System.in);
- while (number != 3) {
- number = sc.nextInt();
- if (number == 1) {
- blockSearch(a, b, eps, n);
- } else if (number == 2) {
- tangent(a, b , eps);
- }
- }
- }
- static int tangent(double a, double b, double eps) {
- double x = 0, y = 0, y1, y2, z, z1, z2;
- int n = 0;
- y1 = function(a);
- y2 = function(b);
- z1 = derivative(a);
- z2 = derivative(b);
- boolean found = false;
- while (b - a > 2 * eps) {
- double c = ((b * z2 - a * z1) - (y2 - y1)) / (z2 - z1);
- y = function(c);
- n++;
- z = derivative(c);
- if (z == 0) {
- x = c;
- found = true;
- break;
- } else if (z < 0) {
- a = c;
- y1 = y;
- z1 = z;
- } else {
- b = c;
- y2 = y;
- z2 = z;
- }
- }
- if (!found) {
- x = (a + b) / 2;
- y = function(x);
- }
- System.out.println("X = " + x);
- System.out.println("Y = " + y);
- System.out.println("n = " + n);
- System.out.println();
- return n;
- }
- static double function(double x) {
- return Math.pow(x, 2) + 8 * Math.exp(0.55 * x);
- }
- static double derivative(double x) {
- return 2 * x + ( 22 * Math.exp(0.55 * x) ) / 5;
- }
- static int blockSearch(double a, double b, double eps, int n) {
- double x = (a + b) / 2;
- double y = function(x);
- double xi = 0, yi = 0, minxi = Double.MIN_VALUE, minyi = Double.MIN_VALUE;
- while (b - a > 2 * eps) {
- minxi = Double.MIN_VALUE;
- minyi = Double.MIN_VALUE;
- for (int i = 1; i <= n; i++) {
- xi = a + i * (b - a) / (n + 1);
- yi = function(xi);
- if (minyi == Double.MIN_VALUE || yi < minyi) {
- minxi = xi;
- minyi = yi;
- }
- }
- a = minxi - (b - a) / (n + 1);
- b = minxi + (b - a) / (n + 1);
- }
- System.out.println("X = " + minxi);
- System.out.println("Y = " + minyi);
- System.out.println("n = " + n);
- return n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement