Iterative Type Programming
- To: mathgroup@smc.vnet.net
- Subject: [mg11120] Iterative Type Programming
- From: Corey & Alicia Beaverson <cbeavers@utk.edu>
- Date: Mon, 23 Feb 1998 21:40:41 -0500
Below is a program that I wrote as the solution to the temperatures at different nodes in a "gridwork" (a homework problem). I am still not as proficient with Mathematica as I would like to be and would like some opinions on a better way to write an iterative type program. One of the requirements was to always use the latest values when solving the equations (Gauss-Seidel). I really feel like I fumbled through this thing! Also, at the time the only way I could get it to converge was by making it iterate until "maxiter" was met, but I wanted to have it iterate until the "tol" was met, any suggestions on how to do this as well? Off[General::spell]; gs := Tn = Array[Tnew, 4]; To = Array[Told, 4]; error = Array[err, 4]; iter = 0; ji = 10; hi = 15; r = 30; s = 35; t = 50; Tnew[1] = 100; Tnew[2] = 900; Tnew[3] = 1.7; Tnew[4] = 30; Told[1] = 0; Told[2] = 0; Told[3] = 0; Told[4] = 0; maxiteration = 100; Tol = 0.001; While[iter < maxiteration, iter++; If[maxerr > Tol, To = Tn]; Tn[[1]] = (ji*hi + To[[2]] + To[[3]])/4; Tn[[2]] = (ji*r + To[[1]] + To[[4]])/4; Tn[[3]] = (ji*s + To[[1]] + To[[4]])/4; Tn[[4]] = (ji*t + To[[2]] + To[[3]])/4]; Do[err[i] = Abs[Told[i] - Tnew[i]], {i, 1, 4}]; maxerr = Max[error]; On[General::spell]; Print[TableForm[N[Tn]]]