数值分析-雅可比迭代法,高斯-雅可比迭代法,超松弛迭代法。

最近上数值分析实验课,将做实验用到的代码送给需要帮助的同学。
前两种迭代方法基于计算机数值方法(第二版)(施吉林)212页例题
雅可比迭代法:
code:

 import java.util.Random;
class Yabis{
    public static void main(String[] args) {
        Random random=new Random();
        double x1=random.nextDouble();
        double x2=random.nextDouble();
        double x3=random.nextDouble();
        double temp1,temp2,temp3;
        int i=1;
        System.out.println("雅可比迭代法:x1,x2,x3的初始值是:"+x1+","+x2+","+x3);
        while(i<=10){
            System.out.println("第"+i+"次迭代");
            temp1=x1;
            x1=(1.0/8)*(20+3*x2-2*x3);
            System.out.println(x1);
            temp2=x2;
            x2=(1.0/11)*(33-4*temp1+x3);
            System.out.println(x2);
            temp3=x3;
            x3=(1.0/4)*(12-2*temp1-temp2);
            System.out.println(x3);
            i++;
        }      
    }
}

高斯-雅可比迭代法:
code:

import java.util.Random;
class Gaosi{
    public static void main(String[] args) {
        Random random=new Random();
        double x1=random.nextDouble();
        double x2=random.nextDouble();
        double x3=random.nextDouble();
        int i=1;
        System.out.println("高斯-雅可比迭代法:x1,x2,x3的初始值是:"+x1+","+x2+","+x3);
        while(i<=10){
            System.out.println("第"+i+"次迭代");
            x1=(1.0/8)*(20+3*x2-2*x3);
            System.out.println(x1);
            x2=(1.0/11)*(33-4*x1+x3);
            System.out.println(x2);
            x3=(1.0/4)*(12-2*x1-x2);
            System.out.println(x3);
            i++;
        }      
    }
}

超松弛迭代法:
基于给定偶数阶方阵。
code:

import java.util.Random;
class Chaosongchi{
    public static void main(String[] args) {
        Random random=new Random();
        double x1=random.nextDouble();
        double x2=random.nextDouble();
        double x3=random.nextDouble();
        double x4=random.nextDouble();
        double x5=random.nextDouble();
        double x6=random.nextDouble();
        double temp1,temp2,temp3,temp4,temp5,temp6;
        double timo1,timo2,timo3,timo4,timo5,timo6;
        timo1=x1;
        timo2=x2;
        timo3=x3;
        timo4=x4;
        timo5=x5;
        timo6=x6;
        int i=1;
        double $=0;
        System.out.println("超松弛迭代法:x1,x2,x3的初始值是:"+x1+","+x2+","+x3+","+x4+","+x5+","+x6);
        for($=0;$<2;$+=0.1){
            System.out.println("当$的值为:"+$);
            i=1;
            temp1=timo1;
            temp2=timo2;
            temp3=timo3;
            temp4=timo4;
            temp5=timo5;
            temp6=timo6;
        while(i<=10){
            System.out.println("第"+i+"次迭代");
            x1=(1.0/3)*(2.5+($*temp2+(1-$)*x2)-0.5*($*temp6+(1-$)*x6));
            System.out.println(x1);
            x2=(1.0/3)*(1.5+($*temp1+(1-$)*x1)+($*temp3+(1-$)*x3)-0.5*($*temp5+(1-$)*x5));
            System.out.println(x2);
            x3=(1.0/3)*(1+($*temp2+(1-$)*x2)+($*temp4+(1-$)*x4));
            System.out.println(x3);
            x4=(1.0/3)*(1+($*temp3+(1-$)*x3)+($*temp5+(1-$)*x5));
            System.out.println(x4);
            x5=(1.0/3)*(1.5-0.5*($*temp2+(1-$)*x2)+($*temp4+(1-$)*x4)+($*temp6+(1-$)*x6));
            System.out.println(x5);
            x6=(1.0/3)*(2.5-0.5*($*temp1+(1-$)*x1)+($*temp5+(1-$)*x5));
            System.out.println(x6);
            i++;
        }
    }      
    }
}

你可能感兴趣的:(数值分析)