⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sm_2.java

📁 Solving nonlinear equations using Newton s and Transversal methods for function x=exp(-x^2)
💻 JAVA
字号:
/***********************************************
 *   Skaitiniu metodu 2 laboratorinis darbas   *
 *       Netiesiniu uzdaviniu sprendimas       *
 *     Relaksacijos ir kirstiniu metodais      *
 *         Uzduotis - x = exp(-x ^ 2)          *
 *       Autorius - Vaidas Zidanavicius        *
 **********************************************/

import java.io.*;
import java.lang.*;

public class SM_2 {

    public static double G_a, G_b, G_x0, G_x1, G_y0, G_y1;

    public SM_2() {}

    public static double Nuskaitymas_Double() {
        String tmp = "";
	    double skaiciukas = 0;

		try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            tmp = br.readLine();
		} catch (IOException exc) {
            System.out.println("Skaitymo klaida: " + exc);
            System.exit(0);
		}

		try {
            skaiciukas = Double.parseDouble(tmp);
        } catch (NumberFormatException exc) {
            System.out.println("Blogas skaitmuo. Stringas  " + tmp + " yra ignoruotas.");
		}

        return skaiciukas;
    }

    public static int Nuskaitymas_Int() {
        String tmp = "";
		int skaiciukas = 0;

		try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            tmp = br.readLine();
		} catch (IOException exc) {
            System.out.println("Skaitymo klaida: " + exc);
            System.exit(0);
		}

		try {
            skaiciukas = Integer.parseInt(tmp);
        } catch (NumberFormatException exc) {
            System.out.println("Blogas skaitmuo. Stringas  " + tmp + " yra ignoruotas.");
		}

		return skaiciukas;
    }
    
    public static double Funkcija(double x) {
        //double y = ((-1) * Math.cos(x) * Math.cos(x) - x);
        double k = Math.exp((-1) * Math.cos(x) * Math.cos(x)) - x;

        return k;
    }
    
    public static double Funkcija2(double x) {
        //double y = ((-1) * Math.cos(x) * Math.cos(x));
        double k = Math.exp((-1) * Math.cos(x) * Math.cos(x));

        return k;
    }
    
    public static void Relaksacija() {
        double L_x1, L_y1, omega, M, m, Eps;
        int i = 0;
        int j = 0;

        System.out.println("Iveskite intervalo pradzia:");
        G_a = Nuskaitymas_Double();

        System.out.println("Iveskite intervalo gala:");
        G_b = Nuskaitymas_Double();

        System.out.println("Iveskite x_0 reiksme:");
        G_x0 = Nuskaitymas_Double();

        G_y0 = Funkcija2(G_x0);
        //System.out.println("qqqqqqqq = " + Math.exp(-1));
        M = 3;
        m = 0;
        omega = 2 / (2 + M + m);
        Eps = 0.001;
        L_x1 = (((1 - omega) * G_x0) + (omega * G_y0));
        L_y1 = Funkcija(L_x1);
        j = i;
        
        System.out.println("x_" + j + " = " + G_x0);
        j++;

        System.out.println("x_" + j + " = " + L_x1);

        while (!((L_y1 == 0) || ((Math.abs(L_x1 - G_x0)) < Eps))) {
            i++;
            G_x0 = L_x1;
            G_y0 = Funkcija2(G_x0);
            L_x1 = (((1 - omega) * G_x0) + (omega * G_y0));
            L_y1 = Funkcija(L_x1);
            j = i;
            j++;

            System.out.println("x_" + j + " = " + L_x1);
        }

        System.out.println("Relaksacijos metodo sprendinys: " + L_x1);
    }
    
    public static void Kirstine() {
        double Eps = 0.001;
        double L_x2, L_y2;
        int i = 0;
        int j;

        System.out.println("Iveskite x_0 reiksme:");
        G_x0 = Nuskaitymas_Double();

        System.out.println("Iveskite x_1 reiksme:");
        G_x1 = Nuskaitymas_Double();

        G_y0 = Funkcija(G_x0);
        G_y1 = Funkcija(G_x1);
        L_x2 = G_x1 - (G_y1 * ((G_x1 - G_x0) / (G_y1 - G_y0)));
        L_y2 = Funkcija(L_x2);
        j = i;

        System.out.println("x_" + j + " = " + G_x0);
        j++;

        System.out.println("x_" + j + " = " + G_x1);
        j++;

        System.out.println("x_"+j+"="+L_x2);

        while (!((L_y2 == 0) || ((Math.abs(L_x2 - G_x1)) < Eps))) {
            i++;
            G_x0 = G_x1;
            G_x1 = L_x2;
            G_y0 = Funkcija(G_x0);
            G_y1 = Funkcija(G_x1);
            L_x2 = G_x1 - (G_y1 * ((G_x1 - G_x0) / (G_y1 - G_y0)));
            L_y2 = Funkcija(L_x2);
            j = i;
            j = j + 2;

            System.out.println("x_"+j+"="+L_x2);
        }

        System.out.println("Kirstiniu metodo sprendinys: " + L_x2);
    }
    
    public static void main(String[] args) {
    	int meniu;

        G_a = G_b = G_x0 = G_x1 = G_y0 = G_y1 = 0;

        System.out.println("1 - Relaksacijos metodu; 2 - Kirstiniu metodu; 0 - Exit: ");
        meniu = Nuskaitymas_Int();
        //System.out.println("SSS = " + Math.exp((-1) * Math.cos(0.4440595135391112) * Math.cos(0.4440595135391112)));

        while (meniu != 0) {
	        if (meniu == 1){
	            Relaksacija();
	            System.out.println("1 - Relaksacijos metodu; 2 - Kirstiniu metodu; 0 - Exit: ");
        		meniu = Nuskaitymas_Int();
	        }
	        else if (meniu == 2) {
	            Kirstine();
	            System.out.println("1 - Relaksacijos metodu; 2 - Kirstiniu metodu; 0 - Exit: ");
        		meniu = Nuskaitymas_Int();
	        }
	        else {
	            System.out.println("Ivedete bloga skaiciu.");
	            System.out.println("1 - Relaksacijos metodu; 2 - Kirstiniu metodu; 0 - Exit: ");
        		meniu = Nuskaitymas_Int();
	        }
	    }
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -