Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package metoda_gaussa.seidla;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class Metoda_gaussaSeidla {
- static double h = 0.00001;
- static double funkcja(double x, double y) {
- // Math.pow(y, 3) + Math.pow(x, 2) - 7 * x * y - 3 * x + 9;
- return 10*x*x+12*x*y+10*y*y;
- //return Math.pow(x,2) * Math.pow(y,2);
- }
- static double pochodna_x(double x, double y) {
- return (funkcja(x + h, y) - funkcja(x, y)) / h;
- }
- static double pochodna_y(double x, double y) {
- return (funkcja(x, y + h) - funkcja(x, y)) / h;
- }
- static double pochodna_x2(double x, double y) {
- return pochodna_x(pochodna_x(x, y), y);
- }
- static double pochodna_y2(double x, double y) {
- return pochodna_y(x, pochodna_y(x, y));
- }
- static double pochodna_xy(double x, double y) {
- return (funkcja(x + h, y + h) - funkcja(x + h, y) - funkcja(x, y + h) + funkcja(x,y)) / Math.pow(h, 2);
- }
- static double pochodna_x3(double x, double y) {
- return pochodna_x2(pochodna_x(x, y), y);
- }
- static double pochodna_y3(double x, double y) {
- return pochodna_y2(x, pochodna_y(x,y));
- }
- static double metoda_stycznych(double XY, String xy, double epsilon) {
- double a = -100, b = 100;
- double x, y;
- int iter = 0;
- ArrayList<Double> xn = new ArrayList<>();
- xn.add(100.0);
- ArrayList<Double> yn = new ArrayList<>();
- yn.add(100.0);
- if(xy == "x") {
- y = XY;
- x = 0;
- if((pochodna_x(a, y) >= 0 && pochodna_x3(b, y) >= 0) || (pochodna_x(a, y) < 0 && pochodna_x(b, y) < 0)) {
- xn.add(a);
- } else {
- xn.add(b);
- }
- while(Math.abs(pochodna_x(xn.get(iter+1), y)) >= epsilon || Math.abs(xn.get(iter+1) - xn.get(iter)) >= epsilon) {
- xn.add(xn.get(iter) - (pochodna_x(xn.get(iter), y) / pochodna_x2(xn.get(iter), y)));
- System.out.println("xn1 = " + xn.get(iter));
- iter++;
- }
- }
- if(xy == "y") {
- x = XY;
- y = 0;
- if((pochodna_y(a, y) >= 0 && pochodna_y3(b, y) >= 0) || (pochodna_y(a, y) < 0 && pochodna_y3(b, y) < 0)) {
- yn.add(a);
- } else {
- yn.add(b);
- }
- while(Math.abs(pochodna_y(x, yn.get(iter + 1))) >= epsilon || Math.abs(yn.get(iter+1) - yn.get(iter)) >= epsilon) {
- yn.add(yn.get(iter) - (pochodna_y(x, yn.get(iter)) / pochodna_y2(x, yn.get(iter))));
- System.out.println("yn1 = " + yn.get(iter));
- iter++;
- }
- }
- if(xy == "x") return xn.get(iter);
- else return yn.get(iter);
- }
- public static void main(String[] args) {
- double x = 10, y = 10;
- double EPSILON = 0.001;
- double epsilon = 0.000001;
- int iter = 1;
- while(Math.abs(pochodna_xy(x,y)) > EPSILON) {
- System.out.println("x=" + x);
- x = metoda_stycznych(y, "x", epsilon);
- System.out.println("x=" + x);
- y = metoda_stycznych(x, "y", epsilon);
- System.out.println(x + " | " + y);
- }
- System.out.println(x + " | " + y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement