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

📄 linearequations2.java

📁 原创的线性方程组求解程序
💻 JAVA
字号:
/**
 * @(#)LinearEquations.java
 * @xiong 
 * @version 1.00 2008/7/2
 */

import java.util.Scanner;
import java.io.*;

public class LinearEquations2   
{
	private int n;
	private double matrix[][];
	private double x[];
	
	public LinearEquations2() //Constructor
	{
		Scanner input = new Scanner(System.in);	
		System.out.print("Enter the number of linear equations: ");
		n = input.nextInt();
		matrix = new double[n][n+1];
		x=new double[n];
		//System.out.println(n);
		//System.out.println(matrix[n-1][n]);	
	}
	
	public void inputAugmentedMatrix() //Input Augmented Matrix
	{
		System.out.println();
        for(int k=0;k<n;k++)
        {
            String str="";   
        	InputStreamReader stdin = new InputStreamReader(System.in);  //entered by keyboard   
        	BufferedReader bufin = new BufferedReader(stdin);  
        	StringBuffer buffer = new StringBuffer();    
        	try   
        	{   
            	System.out.printf ("Please input line %d of augmented matrix(Coefficients should be separated by blanks!): ",k+1);   
     			str = bufin.readLine(); 
     			buffer.append(" "+str);  
      			//	System.out.println   ("Line you input is:   "+str);   
        	}   
        	catch(IOException E)   
        	{   
            	System.out.println("I/O errors!!!");   
        	} 
        	String[] temp = buffer.toString().replaceFirst(" ","").split("\\s+"); 
        	if(temp.length!=n+1)
        		System.out.println("Number of coefficients is wrong!");
	    	//int[] number = new int[temp.length]; 
	    	for(int i=0;i<temp.length;i++)
	    	{ 
				try
				{ 
					matrix[k][i] = Double.parseDouble(temp[i]); 
		    		//System.out.printf("matrix[%d][%d]=%f  ",k,i,matrix[k][i]); 
				}
				catch(Exception e)
				{ 
					System.out.println("Some illegal characters exist in your input!"); 
				}
	    	}
	    	System.out.println();
        }          
	}
	
	public void gaussSeidel()  // Gauss-Seidel iteration algorithm
	{
		double y[]=new double[n]; //accessorial array
		int k=0; //iteration times
		int flag; //indicate variable,flag==1 indicates that iteration has finished
		double precision;
		
		Scanner input1 = new Scanner(System.in);	
		System.out.print("Enter precision of iteration: ");
		precision= input1.nextDouble();
		
		//Input the initial values of iteration
		String str="";   
        InputStreamReader stdin = new InputStreamReader(System.in);    
        BufferedReader bufin = new BufferedReader(stdin);  
        StringBuffer buffer = new StringBuffer();    
        try   
        {   
            System.out.print("\nEnter the initial values of iteration:");   
     		str = bufin.readLine(); 
     		buffer.append(" "+str);    
        }   
        catch(IOException E)   
        {   
            System.out.println("I/O errors!!!");   
        } 
        String[] temp = buffer.toString().replaceFirst(" ","").split("\\s+"); 
        if(temp.length!=n)
        	System.out.println("Number of values is wrong!");
        else
        	System.out.println("\nInitial values of iteration are:");
	    for(int i=0;i<temp.length;i++)
	    { 
			try
			{ 
				x[i] = Double.parseDouble(temp[i]); 
		    	System.out.printf("x[%d]=%f  ",i+1,x[i]); 
			}
			catch(Exception e)
			{ 
				System.out.println("Some illegal characters exist in your input!"); 
			}
	    }
	    System.out.println();
	    System.out.println();
	    System.out.println("Begin gauss-seidel iteration:\n");
	      
	    do
	    {
	    	for(int i=0;i<n;i++)
	    	{
	    		y[i]=matrix[i][n];
	    		for(int j=0;j<i;j++)
	    		{	    			
	    			y[i]-=matrix[i][j]*y[j];
	    	    }
	    	    for(int j=i+1;j<n;j++)
	    		{	    			
	    			y[i]-=matrix[i][j]*x[j];
	    	    }
	    		y[i]/=matrix[i][i];
	    	}
	    	
	    	k++; 
	    	flag=1;
	    	
	    	for(int i=0;i<n;i++)
	    	{
	    		if(Math.abs(y[i]-x[i])>precision)
	    			flag=0;
	    		x[i]=y[i];
	    		System.out.printf("x[%d]=%f ",i+1,x[i]);
	    	}
	    	System.out.println();
	    	System.out.printf("Gauss-seidel %d times!\n\n",k); 
	    }while(flag==0);
		
	}
	
	public void outputResults()
	{
		System.out.println("\nFinal result of linear equations is: "); 
		for(int i=0;i<n;i++)
			System.out.printf("x[%d]=%f ",i+1,x[i]);
		System.out.println();
	}
	
}

⌨️ 快捷键说明

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