Message Boards Message Boards

Solving for Temperature Variables Using Loops

Posted 9 years ago

I'm currently having trouble creating a code that solves for 9 temperature variables using 60 loops/iterations. Can anyone help me detect what is wrong with my code?

(*Known variables*)

alpha = 3.91*(10^-6);
dt = 10;
dx = 0.1;
dy = 0.1;
k = 120;
h1 = 10;
h2 = 100;
Tinf1 = 15;
Tinf2 = 15;
c = 3.069*(10^7);
a = 0;

Ti1 = 800;
Ti2 = 800;
Ti3 = 800;
Ti4 = 800;
Ti5 = 800;
Ti6 = 800;
Ti7 = 800;
Ti8 = 800;
Ti9 = 800;

(*Define functions*)

Do [
 solutions =
  NSolve[
   {T1 == ((((h1*(dx/2)*(Tinf1 - Ti1)) + (h2*(dy/2)*(Tinf2 - 
                Ti1)) + (k*(dy/2)*((T2i - Ti1)/dx)) + (k*(dx/
                2)*((Ti4 - Ti1)/dy)))*(dt) - a + 
         Ti1)/(c*(dx/2)*(dy/2))),
    T2 == ((((h1*
              dx*(Tinf1 - Ti2)) + (k*(dy/2)*((Ti1 - Ti2)/
                dx)) + (k*(dy/2)*((Ti3 - Ti2)/dx)) + (k*
              dx*((Ti5 - Ti2)/dy)))*(dt) - a + Ti2)/(c*dx*(dy/2))),
    T3 == ((((h1*
              dx*(Tinf1 - Ti3)) + (k*(dy/2)*((Ti2 - Ti3)/
                dx)) + (k*(dy/2)*((Ti2 - Ti3)/dx)) + (k*
              dx*((Ti6 - Ti3)/dy)))*(dt) - a + Ti3)/(c*dx*(dy/2))),
    T4 == ((((h2*
              dy*(Tinf2 - Ti4)) + (k*(dx/2)*((Ti1 - Ti4)/
                dy)) + (k*(dx/2)*((Ti7 - Ti4)/dy)) + (k*
              dy*((Ti5 - Ti4)/dy)))*(dt) - a + Ti4)/(c*(dx/2)*dy)),
    T5 == ((((k*dx*((Ti2 - Ti5)/dy)) + (k*dy*((Ti4 - Ti5)/dx)) + (k*
              dy*((Ti6 - Ti5)/dx)) + (k*dx*((Ti8 - Ti5)/dy)))*(dt) - 
         a + Ti5)/(c*dx*dy)),
    T6 == ((((k*dx*((Ti3 - Ti6)/dy)) + (k*dy*((Ti5 - Ti6)/dx)) + (k*
              dy*((Ti5 - Ti6)/dx)) + (k*dx*((Ti9 - Ti6)/dy)))*(dt) - 
         a + Ti6)/(c*dx*dy)),
    T7 == ((((h2*(dy/2)*(Tinf2 - Ti7)) + (k*(dx/2)*((Ti4 - Ti7)/
                dy)) + (k*(dy/2)*((Ti8 - Ti7)/dx)) + (h2*(dx/
                2)*(Tinf2 - Ti7)))*(dt) - a + 
         Ti7)/(c*(dx/2)*(dy/2))),
    T8 == ((((h2*dx*(Tinf2 - Ti8)) + (k*
              dx*((Ti5 - Ti8)/dy)) + (k*(dy/2)*((Ti7 - Ti8)/
                dx)) + (k*(dy/2)*((Ti9 - Ti8)/dx)))*(dt) - a + 
         Ti8)/(c*dx*(dy/2))),
    T9 == ((((h2*
              dx*(Tinf2 - Ti9)) + (k*(dy/2)*((Ti8 - Ti9)/
                dx)) + (k*(dy/2)*((Ti8 - Ti9)/dx)) + (k*
              dx*((Ti6 - Ti9)/dy)))*(dt) - a + Ti9)/(c*dx*(dy/2)))},
   {T1, T2, T3, T4, T5, T6, T7, T8, T9}];

 Ti1 = T1 /. solutions;
 Ti2 = T2 /. solutions;
 Ti3 = T3 /. solutions;
 Ti4 = T4 /. solutions;
 Ti5 = T5 /. solutions;
 Ti6 = T6 /. solutions;
 Ti7 = T7 /. solutions;
 Ti8 = T8 /. solutions;
 Ti9 = T9 /. solutions;
 , {j, 60}]

Print[T1 /. solutions];
Print[T2 /. solutions];
Print[T3 /. solutions];
Print[T4 /. solutions];
Print[T5 /. solutions];
Print[T6 /. solutions];
Print[T7 /. solutions];
Print[T8 /. solutions];
Print[T9 /. solutions];
POSTED BY: Katrine Rubia

Hello Katrine,

I probably comes from the Ti2 misspelled as T2i in the equation of T1==... I found it by running your code with the Print section inside the loop for more visibility. The first iteration gave a numerical output + a remaining variable T2i. When you correct the spelling, the loop executes immediately.

Best regards

Christian NĂ©el

POSTED BY: Christian Neel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract