首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用jacobi算法实现拉普拉斯方程

用jacobi算法实现拉普拉斯方程
EN

Stack Overflow用户
提问于 2010-06-03 19:19:12
回答 1查看 2.1K关注 0票数 0

该算法遍历2D NxN数组,使每个元素都是其周围4个相邻元素(左、右、上、下)的平均值。

NxN数组最初全为零,并用全为1的边距括起来,如下例所示。1永远不会变,0会逐渐增加。

代码语言:javascript
复制
             1 1 1 1 1 1 1 1
             1 0 0 0 0 0 0 1
             1 0 0 0 0 0 0 1
             1 0 0 0 0 0 0 1
             1 0 0 0 0 0 0 1
             1 0 0 0 0 0 0 1
             1 0 0 0 0 0 0 1
             1 1 1 1 1 1 1 1

我已经实现了下面的代码,并且得到了数组索引越界异常。请指正我。

代码语言:javascript
复制
my code :
       public class Main {
static int NO_OF_THREADS =8;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
      Jacobi jacobi = new Jacobi(NO_OF_THREADS);
      jacobi.initialize();
      jacobi.create_threads();
    }

  }//end of Main class

  public class Jacobi {
int ROWS=1000,COLS=1000;
private int i;
private int upper=100;//prevents buffer overflow
private int lower=99;//prevents buffer overflow
private int j;
private double[][] jacobi=new double[ROWS][COLS];
private int NO_OF_THREADS;


public Jacobi(int k)
{
    NO_OF_THREADS=k;
}

public  void initialize() {
        for(i=1;i<=upper;i++)
           {
           for(j=1;j<=upper;j++)
                 {

                       if((i==1)||(i==upper)||(j==1)||(j==upper)){
                      jacobi[i][j]=1.0;
                       }

                   else
                          jacobi[i][j]=0.0;


                 }

             }

    }
              public double[][] getJacobi()
              {
                    return jacobi;
              }

              public void create_threads()
              {
                   theThread[] threads=new theThread[NO_OF_THREADS];
                   for(int k=1;k<=NO_OF_THREADS;k++)
                   {
                       threads[k]=new theThread();
                       threads[k].start();
                    }
               }
               //Inner class of Jacobi

             class theThread extends Thread {

               @Override
              public void run()
              {
                   for(int q=2;q<=lower;q++)
                   {

                     System.out.println("The ID of this thread is: "+getName());
                     for(int j=2;j<=lower;j++)
                      {
                        synchronized(Jacobi.this)
                         {

                        jacobi[q][j]=(jacobi[q-1][j]+jacobi[q+1][j]+jacobi[q] [j-1]+jacobi[q][j+1])/4;
                         }//end of synchronized
              }//end of inner for loop


          }//end of for loop
     }
}//end of theThread class
}//end of jacobi class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-03 19:26:23

在队伍中

代码语言:javascript
复制
int ROWS,COLS=1000;

我觉得你想把它改成

代码语言:javascript
复制
int ROWS=1000, COLS=1000;

否则ROWS设置不正确...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2965402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档