newton method

Nov 22nd, 2020
433
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. package com.maxermackov;
2.
3. public class Main {
4.
5.     /*
6.         This calculates the minimum using the newton method iteratively, of the function:
7.         f(x,y) = (x-2)^4 +(x-2)^2 * y^2 +(y+1)^2
8.
9.         Insert x0, y0 and k.
10.      */
11.
12.     public static void main(String[] args) {
13.         System.out.println("n\t|\tX\t\t\t\t\t|\tY\t\t\t\t\t|\tValue");
14.         double x = 5; // x0
15.         double y = 4; // y0
16.         double k = 20;
17.         for (int i = 1; i <= k; i++) {
18.             double[] out = proc(x, y);
19.             if (out[0] == x && out[1] == y) {
20.                 System.out.println("No change after x" + (i - 1));
21.                 break;
22.             }
23.             x = out[0];
24.             y = out[1];
25.             double value = Math.pow(x - 2, 4) + Math.pow(x - 2, 2) * Math.pow(y, 2) + Math.pow(y + 1, 2);
26.             System.out.println("x" + i + "\t|\t" + x + "\t|\t" + y + "\t|\t" + value);
27.         }
28.     }
29.
30.     public static double[] proc(double x, double y) {
31.         double det = 1 / (((2 * Math.pow(x - 2, 2) + 2) * (12 * Math.pow(x - 2, 2) + 2 * Math.pow(y, 2))) - ((-4 * (x - 2) * y) * (-4 * (x - 2) * y)));
32.         double xn = x - (det * (((2 * Math.pow(x - 2, 2) + 2) * (4 * Math.pow(x - 2, 3) + 2 * (x - 2) * Math.pow(y, 2))) + ((-4 * (x - 2) * y) * (2 * Math.pow(x - 2, 2) * y + 2 * (y + 1)))));
33.         double yn = y - (det * (((-4 * (x - 2) * y) * (4 * Math.pow(x - 2, 3) + 2 * (x - 2) * Math.pow(y, 2))) + ((12 * Math.pow(x - 2, 2) + 2 * Math.pow(y, 2)) * (2 * Math.pow(x - 2, 2) * y + 2 * (y + 1)))));
34.         double xy[] = {xn, yn};
35.         return xy;
36.     }
37. }
38.
39.
RAW Paste Data