热传导方程的数值模拟

时间:2022-01-24 18:56:55
 1 public class HeatConduction {
2 public double[][] conduction(double x, double t,int i,int j) {//i,j表示x,t分别分割的离散点
3 double [][] XT = new double[i][j];
4 double dx=x/(i-1),dt=t/(j-1);
5 double c=dt/(dx*dx);
6 for (int k = 0; k < i; k++) {//u(x,0)的初值
7 XT[k][0]=k*dx*(x-k*dx)/x/x;
8 }
9 for (int k = 0; k < j; k++) {//u(0,t)的初值
10 XT[0][k]=0;
11 }
12 for (int k = 0; k < j; k++) {//u(x,t)的初值
13 XT[i-1][k]=0;
14 }
15 for (int k = 1; k < j; k++) {
16 for (int l = 1; l < i-1; l++) {
17 XT[l][k]=c*(XT[l-1][k-1]+XT[l+1][k-1])+(1-2*c)*XT[l][k-1];
18 }
19 }
20 return XT;
21 }
22
23 public static void main(String[] args) {
24 HeatConduction heatConduction=new HeatConduction();
25 double[][] XT = heatConduction.conduction(1.0, 1.0, 10, 500);
26 for (int j = 0; j < XT[0].length; j++) {
27 for (int i = 0; i < XT.length; i++) {
28 if (i==XT.length-1) {
29 System.out.println(XT[i][j]);
30 }else {
31 System.out.print(XT[i][j]+" ");
32 }
33 }
34 }
35 }
36 }

 ut=uxx     (0<x<l)  (0<t<  )

u(0,t)=0, u(l,t)=0

u(x,0)=x(l-x)/l2