Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MetodaGaussaSeidla {
- static double h=0.00001;
- static double xn,x1;
- static double epsilon=0.0001;
- static double[] gradient = new double[2];
- static double funkcja(double x, double y){
- return 10*x*x+12*x*y+10*y*y;
- }
- static double dfdx(double x, double y){
- //return (1/h)*(funkcja(x+h,y) - funkcja(x,y));
- return 20*x+12*y;
- }
- static double dfdy(double x, double y){
- //return (1/h)*(funkcja(x,y+h) - funkcja(x,y));
- return 12*x+20*y;
- }
- static double d2fdx2(double x, double y){
- //return (1/Math.pow(h,2))*((funkcja(x+h,y)-2*funkcja(x,y)+funkcja(x-h,y)));
- return 20;
- }
- static double d2fdy2(double x, double y){
- //return (1/Math.pow(h,2))*(funkcja(x,y+h)-2*funkcja(x,y)+funkcja(x,y-h));
- return 20;
- }
- static double d3fdx3(double x, double y){
- //return (1/Math.pow(h,3))*(funkcja(x+3*h,y)-3*funkcja(x+2*h,y)+3*funkcja(x+h,y)-funkcja(x,y));
- return 0;
- }
- static double d3fdy3(double x, double y){
- //return (1/Math.pow(h,3))*(funkcja(x,y+3*h)-3*funkcja(x,y+2*h)+3*funkcja(x,y+h)-funkcja(x,y));
- return 0;
- }
- static double MetodaStycznychPoX(double a, double b,double x){
- int licznik = 0;
- licznik++;
- if(dfdx(a,x) * d3fdx3(a,x) >= 0){
- x1 = a;
- }
- else{
- x1 = b;
- }
- while(dfdx(x1,x) >= epsilon){
- licznik++;
- xn = x1;
- x1 = xn - ( dfdx(xn,x) / d2fdx2(xn,x) );
- }
- //System.out.println("Iteracje = " + licznik);
- System.out.println("x1 = " + x1);
- return x1;
- }
- static double MetodaStycznychPoY(double a, double b,double y){
- int licznik = 0;
- //licznik++;
- if(dfdy(a,y) * d3fdy3(a,y) >= 0){
- x1 = a;
- }
- else{
- x1 = b;
- }
- while(dfdy(x1,y) >= epsilon ){
- licznik++;
- xn = x1;
- x1 = xn - ( dfdy(xn,y) / d2fdy2(xn,y) );
- }
- //System.out.println("Metoda Stycznych x1 = " + x1);
- //System.out.println("Iteracje = " + licznik);
- return x1;
- }
- public static void liczGaussaSeidla(double x0, double y0, double epsilon){
- int licznik = 0;
- double x;
- double y;
- y = MetodaStycznychPoY(-100,100,y0);
- x = MetodaStycznychPoX(-100,100,x0);
- //gradient[1] = MetodaStycznychPoY(-100,100,10);
- //gradient[0] = MetodaStycznychPoX(-100,100,10);
- //System.out.print(MetodaStycznychPoY(-100,100,10));
- while(Math.abs(funkcja(x,y)) >= epsilon){
- x = MetodaStycznychPoY(-100,100,x);
- y = MetodaStycznychPoX(-100,100,y);
- licznik++;
- System.out.println(x);
- System.out.println(y);
- }
- //System.out.println(x);
- //System.out.println(y);
- }
- public static void main(String[] args) {
- //System.out.print(dfdy(-100,10));
- liczGaussaSeidla(10, 10, 0.0001);
- //double x,y;
- //x = MetodaStycznychPoX(-100,100,10);
- //y = MetodaStycznychPoX(-100,100,x);
- //System.out.println(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement