Wärmeleitungsgleichung in Java simulieren |
Shizmo
Tripel-As
Dabei seit: 16.10.2015
Beiträge: 174
|
|
Wärmeleitungsgleichung in Java simulieren |
|
Hallo, ich steh hier vor einer für mich unverständlichen Aufgabe und hoffe mir kann das evtl jemand besser erklären.
Simulieren Sie die Wärmeleitungsgleichung.
Es geht vorerst mal nur um Teil1.
Ich hab die Angabe mal als PDF verlinkt: Klick
Das Problem ist die Formel (das Schema):
Ich brauche ein Feld (also ein 2dim-Array[][], die Spalten sind die x-Werte und die Zeilen die y-Werte), z.B. 5x5.
So jetzt ist mein x-Wert 5 und mein y-Wert auch 5. Was macht die Formel jetzt mit den Werten, also mit Alpha wird multipliziert, aber was bedeutet h[x,y,t], wie setz ich das um.
Ich verstehs einfach nicht...
|
|
26.11.2015 21:48 |
|
|
|
x und y hast du durch die Position der Variablen im Array gegeben:
code: |
1:
2:
3:
4:
5:
|
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
//mache was mit heat[x,y], was die Temperatur an (x, y) ist.
}
} |
|
t ist implizit gegeben: Zu Beginn ist t=0. Wenn du einmal die Hitzeverschiebung berechnet hast, ist danach t=1. So erhöht sich das t für jeden Aufruf der Hitzeverteilungsfunktion weiter.
__________________ Syntax Highlighting fürs Board (Link)
|
|
26.11.2015 23:14 |
|
|
Shizmo
Tripel-As
Dabei seit: 16.10.2015
Beiträge: 174
|
|
Okay danke super, Teil1 funktioniert, bin gerade bei Teil2, sollte auch funktionieren, tut es aber nicht, also meine Methode fuer Teil2 sieht so aus:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
|
static double[][] heatSimInsulated (double[][] heat, double alpha){
int lines = heat.length, cols = heat[0].length;
double[][] heatNew = new double[lines][cols];
for(int x = 0; x < lines; x++){
for(int y = 0; y < cols; y++){
//Ecken
if(x == 0 && y == 0){
heatNew[x][y] = heat[x][y] - alpha*(2*heat[x][y]-heat[x+1][y]-heat[x][y+1]);
}
else if(x == 0 && y == cols){
heatNew[x][y] = heat[x][y] - alpha*(2*heat[x][y]-heat[x+1][y]-heat[x][y-1]);
}
else if(x == lines && y == 0){
heatNew[x][y] = heat[x][y] - alpha*(2*heat[x][y]-heat[x-1][y]-heat[x][y+1]);
}
else if(x == lines && y == cols){
heatNew[x][y] = heat[x][y] - alpha*(2*heat[x][y]-heat[x-1][y]-heat[x][y-1]);
}
//Raender
else if(x == 0){
heatNew[x][y] = heat[x][y] - alpha*(3*heat[x][y]-heat[x+1][y]-heat[x][y-1]-heat[x][y+1]);
}
else if(x == lines){
heatNew[x][y] = heat[x][y] - alpha*(3*heat[x][y]-heat[x-1][y]-heat[x][y-1]-heat[x][y+1]);
}
else if(y == 0){
heatNew[x][y] = heat[x][y] - alpha*(3*heat[x][y]-heat[x+1][y]-heat[x-1][y]-heat[x][y+1]);
}
else if(y == cols){
heatNew[x][y] = heat[x][y] - alpha*(3*heat[x][y]-heat[x+1][y]-heat[x-1][y]-heat[x][y-1]);
}
//Mitte
else {
heatNew[x][y] = heat[x][y] - alpha*(4*heat[x][y]-heat[x-1][y]-heat[x+1][y]-heat[x][y-1]-heat[x][y+1]);
}
}
}
return heatNew;
} |
|
Allerdings kommt ein Laufzeitfehler, in dem Fall bei Zeile 20 mit:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5"
Findet wer den Fehler???
|
|
27.11.2015 11:17 |
|
|
Karlito
Kaiser
Dabei seit: 11.04.2011
Beiträge: 1.461
|
|
Koordinaten verdreht? Schau mal in die Schleifenköpfe. x = lines und y = columns?
Gruß,
Karlito
|
|
27.11.2015 11:41 |
|
|
Shizmo
Tripel-As
Dabei seit: 16.10.2015
Beiträge: 174
|
|
Das hab ich mir auch schon gedacht, allerdings steht in der Angabe:
Schreiben Sie eine Methode die in heat das Temperaturfeld h[⋅,⋅,0] auf einer rechteckigen Fläche erhält.
Vielleicht schaut das nur so aus, weil es ja abbricht, da es out of boundaries ist.
|
|
27.11.2015 18:22 |
|
|
Shizmo
Tripel-As
Dabei seit: 16.10.2015
Beiträge: 174
|
|
Zitat: |
Original von eulerscheZahl
Hmm.
Ein weiterer möglicher Testfall: setze Breite oder Höhe auf 1. Da stürzt dein Programm auch ab. |
Jawoll das wars
Vielen Dank, hab einfach noch Speziallfaelle hinzugefuegt:
php: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
|
//Spezialfaelle
if(x == 0 && y == 0 && x == lines-1){
heatNew[x][y] = heat[x][y];
}
else if(x == 0 && y == 0 && y == cols-1){
heatNew[x][y] = heat[x][y];
}
else if(y == 0 && y == cols-1){
heatNew[x][y] = heat[x][y];
}
else if(x == 0 && x == lines-1){
heatNew[x][y] = heat[x][y];
} |
|
Und es laeuft.
Danke fuer den Tipp!!!
|
|
27.11.2015 18:49 |
|
|
|
Als Ausgabe habe ich Programmcode für sage (sagemath.org). Damit werden 3D Plots erstellt, die die Temperaturverteilung farblich darstellen.
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
|
public class Main {
public static void main(String[] args) {
int xMax = 5;
int yMax = 5;
int zMax = 5;
double alpha = 0.5;
double tempInit = 100;
double[][][] temp = new double[xMax + 1][yMax + 1][zMax + 1];
int[][] offset = { { 0, 0, 1 }, { 0, 0, -1 }, { 0, 1, 0 }, { 0, -1, 0 }, { 1, 0, 0 }, { -1, 0, 0 } }; //definieren, welche Felder benachbart sind
temp[0][3][0] = tempInit; //Heizung
for (int t = 0; t <= 500; t++) {
double[][][] tempNew = new double[xMax + 1][yMax + 1][zMax + 1];
for (int x = 0; x <= xMax; x++) {
for (int y = 0; y <= yMax; y++) {
for (int z = 0; z <= zMax; z++) {
double neighborCount = 0;
double neighborTemp = 0;
for (int i = 0; i < 6; i++) {
int x_ = x + offset[i][0];
int y_ = y + offset[i][1];
int z_ = z + offset[i][2];
//prüfen, ob Nachbarn innerhalb des Raums sind
if (x_ >= 0 && x_ <= xMax && y_ >= 0 && y_ <= yMax && z_ >= 0 && z_ <= zMax) {
neighborCount++;
neighborTemp += temp[x_][y_][z_];
}
}
tempNew[x][y][z] = temp[x][y][z] - alpha / neighborCount * (neighborCount * temp[x][y][z] - neighborTemp);
}
}
}
tempNew[0][3][0] = tempInit; //Heizung verliert nicht an Temperatur
temp = tempNew;
// Ausgabe
StringBuilder sb = new StringBuilder();
sb.append("p = point3d([])\n");
for (int x = 0; x <= xMax; x++) {
for (int y = 0; y <= yMax; y++) {
for (int z = 0; z <= zMax; z++) {
sb.append("p += point3d([" + x + "," + y + "," + z + "], color=(" + temp[x][y][z] / tempInit + ",0," + (1 - temp[x][y][z] / tempInit) + "))\n");
}
}
}
sb.append("p.show()");
if (t % 100 == 0) //alle 100 Schritte die Temperaturverteilung ausgeben
System.out.println(sb.toString());
}
}
} |
|
__________________ Syntax Highlighting fürs Board (Link)
|
|
03.06.2016 06:35 |
|
|
|