       Re: Clean up code to run faster

• To: mathgroup at smc.vnet.net
• Subject: [mg122432] Re: Clean up code to run faster
• From: Ray Koopman <koopman at sfu.ca>
• Date: Fri, 28 Oct 2011 05:36:05 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <j7u4rb\$t67\$1@smc.vnet.net>

```"Memoize" your function definitions: instead  of

T[0 , t_] := ... ;
T[m_, t_] := ... ;
T[5 , t_] := ... ;

use

T[0 , t_] := T[0,t] = ... ;
T[m_, t_] := T[m,t] = ... ;
T[5 , t_] := T[5,t] = ... ;

Then the table generation is virtually instantaneous.

On Oct 22, 3:10 am, Michelle Maul <michellemaul... at gmail.com> wrote:
> Hello Mathematica friends,
>
> I am new to Mathematica and I have learned that it is a steep learning
> curve, I've spent a week trying to program an example problem from a
> heat transfer book.  I need it to solve for several hundred time steps
> and my computer is only allowing me to do about 15 in an hour of
> solving.  How can I clean up my code and what are some general tips
> that you can give me for the future when I need to do more hard core
> iteration problems and stuff?  PS, I'm a mechanical engineer so I'm
> not very good at understanding how Mathematica thinks in computer
>
> I am solving for the temperatures on the inside and outside of the
> wall while the sun is heating the outside at certain times of the day.
>
> << AutomaticUnits`;
>
> masonry= {WallThickness -> 1. Foot, \[Alpha] -> 4.78*10^-6 Foot^2/
> Second,
>    k -> 0.4 BTU/(Hour*Foot*Fahrenheit), \[Kappa] -> 0.77,
>    Subscript[h, out] -> .7 BTU/(Hour*Foot^2*Fahrenheit),
>    Subscript[T, in] -> 70 Fahrenheit,
>    Subscript[h, in] ->
>     1.8 BTU/(Hour*Foot^2*Fahrenheit), \[CapitalDelta]x -> .2 Foot,
>    Subscript[T, out, initial] -> 30 Fahrenheit};
>
> Subscript[q, solar] =
>   Piecewise[{{114 BTU/(Hour*Foot^2), 0 <= x <= 12}, {242 BTU/
> (Hour*Foot^2),
>      12 < x <= 24}, {178 BTU/(Hour*Foot^2), 24 < x <= 36}}];
> Subscript[T, out] =
>   Piecewise[{{33 Fahrenheit, 0 <= x <= 12}, {43 Fahrenheit,
>      12 < x <= 24}, {45 Fahrenheit, 24 < x <= 36}, {37 Fahrenheit,
>      36 < x <= 48}, {32 Fahrenheit, 48 < x <= 60}, {27 Fahrenheit,
>      60 < x <= 72}, {26 Fahrenheit, 72 < x <= 84}, {25 Fahrenheit,
>      84 < x <= 96}}];
>
> Plot[{Subscript[q, solar], Subscript[T, out]}, {x, 0, 96}]
>
> Nodes = (WallThickness/\[CapitalDelta]x + 1) /.
>    masonry;  (*Number of Nodes for analysis*)
>
> *Nodes will be numbered from 0 to 5 with Node 0 on the inside surface
> of the wall and Node 5 on the exterior surface of the wall*
>
> Initial Conditions are found by the assumption that the temperature
> varies linearly inside the wall.
>
> Temperatures are given in the format:  T[node# , time step]   (ex
> T[0,1] is the interior node at time i=1)
>
> tempstep = (Subscript[T, in] - Subscript[T, out, initial])/(Nodes -
> 1) /.
>    masonry; (* Temperature change between each node *)
>
> T[0, 0] = Subscript[T, in] /.masonry; T[1, 0] = T[0, 0] - tempstep;
> T[2, 0] = T[1, 0] - tempstep; T[3, 0] = T[2, 0] - tempstep;
> T[4, 0] = T[3, 0] - tempstep; T[5, 0] = T[4, 0] - tempstep;
>
> EQUATIONS
>
> (* Node 0 has convection, can use Eqn 5-51 from book *)
>
> T[0, t_] := (1 - 2 \[Tau] - (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k) T[
>       0, t - 1] +
>     2 \[Tau] T[1, t - 1] + (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k*
>      Subscript[T, in] /.masonry;
>
> (* Nodes 1-4 take the more general form, use Eqn 5-47 *)
>
> T[m_, t_] := \[Tau] (T[m - 1, t - 1] + T[m + 1, t - 1]) + (1 - 2 \
> [Tau]) T[m,
>       t - 1] /.masonry;
>
> (* Node 5 is exposed to convection and heat flux outside *)
>
> T[5, t_] := (1 - 2 \[Tau] -
>        2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/k) T[5, t - 1] +
>     2 \[Tau] T[4, t - 1] +
>     2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/
>      k*(Subscript[T, out] /. {x -> t}) +
>     2 \[Tau] (\[Kappa] *(Subscript[q, solar] /. {x -> t}) \
> [CapitalDelta]x)/
>      k /.masonry;
>
> \[CapitalDelta]t = .25 Hour;
>
> \[Tau] = 0.10755
>
> T[0, t_] := (1 - 2 \[Tau] - (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k) T[
>       0, t - 1] +
>     2 \[Tau] T[1, t - 1] + (2 \[Tau] Subscript[h, in] \
> [CapitalDelta]x)/k*
>      Subscript[T, in] /.masonry;
>
> T[m_, t_] := \[Tau] (T[m - 1, t - 1] + T[m + 1, t - 1]) + (1 - 2 \
> [Tau]) T[m,
>       t - 1] /.masonry;
>
> T[5, t_] := (1 - 2 \[Tau] -
>        2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/k) T[5, t - 1] +
>     2 \[Tau] T[4, t - 1] +
>     2 \[Tau] (Subscript[h, out] \[CapitalDelta]x)/
>      k*(Subscript[T, out] /. {x -> t}) +
>     2 \[Tau] (\[Kappa] *(Subscript[q, solar] /. {x -> t}) \
> [CapitalDelta]x)/
>      k /.masonry;
>
> Transpose[Table[T[n, x], {n, 0, 5}, {x, 0, 100}]] // MatrixForm
>
> Thank you for taking the time to look at this

```

• Prev by Date: Re: bug ?
• Next by Date: Re: Parameter replacement with known parameters
• Previous by thread: Clean up code to run faster
• Next by thread: Re: Clean up code to run faster