2
$\begingroup$

New to the site and Mathematica. I'm trying to use the finite element method to get a temperature profile. So if $q_0=f(T_0,T_1)$, $q_n=f(T_{n-1},T_n)$ and we also know that $T_1-T_0=T_2-T_1=T_3-T_2=\dotso=T_n-T_{n-1}$ We can solve for the $T_i$s. I know there is a way one can create the desired amount of variables by using Table[Subscript[T, n], {n, 0, 999}], but how to use them in the equations? I guess what I'm saying is how to solve a large amount of equations. Let me know if my question is not clear enough, I'll try to explain everything I know.

temp[Conc_, Kcond_, x_, h_] := 
 NSolve[Qrad[Conc] == sigma*T0^4 + Qcond[Kcond, x, T0, T1] && 
   Qcond[Kcond, x, T19, T20] == sigma*T20^4 + Qconv[h, T20] && 
   Qcond[Kcond, x, T0, T1] == Qcond[Kcond, x, T1, T2] && 
   Qcond[Kcond, x, T1, T2] == Qcond[Kcond, x, T2, T3] && 
   Qcond[Kcond, x, T2, T3] == Qcond[Kcond, x, T3, T4] && 
   Qcond[Kcond, x, T3, T4] == Qcond[Kcond, x, T4, T5] && 
   Qcond[Kcond, x, T4, T5] == Qcond[Kcond, x, T5, T6] && 
   Qcond[Kcond, x, T5, T6] == Qcond[Kcond, x, T6, T7] && 
   Qcond[Kcond, x, T6, T7] == Qcond[Kcond, x, T7, T8] && 
   Qcond[Kcond, x, T7, T8] == Qcond[Kcond, x, T8, T9] && 
   Qcond[Kcond, x, T8, T9] == Qcond[Kcond, x, T9, T10] && 
   Qcond[Kcond, x, T9, T10] == Qcond[Kcond, x, T10, T11] && 
   Qcond[Kcond, x, T10, T11] == Qcond[Kcond, x, T11, T12] && 
   Qcond[Kcond, x, T11, T12] == Qcond[Kcond, x, T13, T14] && 
   Qcond[Kcond, x, T13, T14] == Qcond[Kcond, x, T14, T15] && 
   Qcond[Kcond, x, T14, T15] == Qcond[Kcond, x, T15, T16] && 
   Qcond[Kcond, x, T15, T16] == Qcond[Kcond, x, T16, T17] && 
   Qcond[Kcond, x, T16, T17] == Qcond[Kcond, x, T17, T18] && 
   Qcond[Kcond, x, T17, T18] == Qcond[Kcond, x, T18, T19] && 
   Qcond[Kcond, x, T18, T19] == Qcond[Kcond, x, T19, T20] && T0 > 0 &&
    T1 > 0 && T2 > 0 && T3 > 0 && T4 > 0 && T5 > 0 && T6 > 0 && 
   T7 > 0 && T8 > 0 && T9 > 0 && T10 > 0 && T11 > 0 && T12 > 0 && 
   T13 > 0 && T14 > 0 && T15 > 0 && T16 > 0 && T17 > 0 && T18 > 0 && 
   T19 > 0 && T20 > 0, {T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, 
   T11, T12, T13, T14, T15, T16, T17, T18, T19, T20}]
temp[3, 12, 0.01, 250]

Basically this is what I want but instead of typing them all out(which I can't) I was wondering if there is a way to simplify this. Thanks!

$\endgroup$
  • 1
    $\begingroup$ It might help if you specify the equations you want to use. As opposed to asking how to use variables in equations. Additionally, the documentation for Solve and Reduce describe how to solve system of equations in Mathematica. $\endgroup$ – IPoiler Oct 2 '15 at 3:00
  • $\begingroup$ thanks. I think my problem is not how to solve them but how to define the variables in a simpler way. I just can't find any examples for that and since I don't have any programming experience this got me a lot of trouble. $\endgroup$ – Fang Oct 2 '15 at 3:20
  • 1
    $\begingroup$ Your equations seem identical except at the first and last time steps, so why not use Table again to generate the equations? The inequalities for example can be generated by Table[ic>0,{ic,list}] when list=Table[Subscript[T, n], {n, 0, 999}]. $\endgroup$ – IPoiler Oct 2 '15 at 3:29
  • 3
    $\begingroup$ ...Although I don't recommend using Subscripts. I would use T[n] instead. $\endgroup$ – march Oct 2 '15 at 3:31
1
$\begingroup$
temp[Conc_, Kcond_, x_, h_] := NSolve[
  And @@ Join[
    {Qrad[Conc] == sigmaT0^4 + Qcond[Kcond, x, T[0], T[1]], Qcond[Kcond, x, T[19], T[20]] == sigmaT20^4 + Qconv[h, T[20]]}
    , Array[Qcond[Kcond, x, T[#], T[# + 1]] == Qcond[Kcond, x, T[# + 1], T[# + 2]] &, 19, 0]
    , T[#] > 0 & /@ Range[0, 20]
  ]
  , Table[T[k], {k, 0, 20}]
 ]

There are a couple of different ways to create lists automated lists of the type you're looking for. Embedded in the code above are some examples. The most straightforward is the last:

Table[T[k], {k, 0, 20}]
(* {T[0], T[1], T[2], T[3], T[4], T[5], T[6], T[7], T[8], T[9], T[10], T[11], T[12], T[13], T[14], T[15], T[16], T[17], T[18], T[19], T[20]} *)

Two others involve Array (look it up in the documentation) and Map (/@) (look it up in the documentation. In addition, I am using pure functions. For instance, T[#] > 0 &. This is shorthand for

Function[{x}, T[x] > 0]

and it is an object that when you attach [a] to it, it puts a in the spot of # (x):

T[#] > 0 & [a]
Function[{x}, T[x] > 0][a]
(* T[a] > 0 *)
(* T[a] > 0 *)

Take this example and run with it.

$\endgroup$
  • $\begingroup$ Thanks so much for the help. There are a lot of command that I've never seen before so I guess it will take a bit time to understand. But this definitely seems enlightening. $\endgroup$ – Fang Oct 2 '15 at 3:58
1
$\begingroup$

As march pointed out in the comment it would be better to use Array. Then you can define a function like

   f[i_] := Qcond[Kcond, x, T[i], T[i + 1]] == 
   Qcond[Kcond, x, T[i + 1], T[i + 2]]

Use Thread to write the conditional part 1

  c1[n_] := And @@ Thread[Array[f, n, 0]]

For the remaining part again use Thread

  c2[n_] := And @@ Thread[Array[T, n, 0] > 0]

Finaly your variable list

   var = Table[T[i], {i, 1, 20}]

Then your main function simplies to

 temp[Conc_, Kcond_, x_, h_] := 
  NSolve[Qrad[Conc] == sigma*T0^4 + Qcond[Kcond, x, T0, T1] && 
  Qcond[Kcond, x, T19, T20] == sigma*T20^4 + Qconv[h, T20] && 
   c1[19] && c2[19], var]
$\endgroup$
  • $\begingroup$ Again thanks for the help. I probably need to familiarize myself with all these new terms but the idea presented here is really helpful. $\endgroup$ – Fang Oct 2 '15 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.