Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.maxermackov;
- public class Main {
- /*
- This calculates the minimum using the newton method iteratively, of the function:
- f(x,y) = (x-2)^4 +(x-2)^2 * y^2 +(y+1)^2
- Insert x0, y0 and k.
- */
- public static void main(String[] args) {
- System.out.println("n\t|\tX\t\t\t\t\t|\tY\t\t\t\t\t|\tValue");
- double x = 5; // x0
- double y = 4; // y0
- double k = 20;
- for (int i = 1; i <= k; i++) {
- double[] out = proc(x, y);
- if (out[0] == x && out[1] == y) {
- System.out.println("No change after x" + (i - 1));
- break;
- }
- x = out[0];
- y = out[1];
- double value = Math.pow(x - 2, 4) + Math.pow(x - 2, 2) * Math.pow(y, 2) + Math.pow(y + 1, 2);
- System.out.println("x" + i + "\t|\t" + x + "\t|\t" + y + "\t|\t" + value);
- }
- }
- public static double[] proc(double x, double y) {
- 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)));
- 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)))));
- 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)))));
- double xy[] = {xn, yn};
- return xy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement