Group Abstract Group Abstract

Message Boards Message Boards

How to turn a flowchart into a Loop in Mathematica

GROUPS:
Hi All,i would like to ask you guys a question about the Conditionals in Mathematica, specifically about the combination of "If, do, For, etc".. The pic blow shows a simple For loop. How should i deal wtih the Codes for the pic blow? Specially for the part with For or While loop in a If case. Thanks all!
POSTED BY: Ke Zheng
Answer
9 months ago
Check this result make sure it fits all the boundary cases you where expecting:
 T = Ki = 0; i = 0;
 While[i < 5,
   Print["i:", i]; i++;
   m = 1; n = 0.1; tmin = 1;
   While[T < tmin,
     Ki = Ki + m*n;
     T = Ki;
     tmin = T
   ];
  Print["T:", T]
]

i:0
T:0.1
i:1
T:0.2
i:2
T:0.3
i:3
T:0.4
i:4
T:0.5
Equivalent For loop. Notice that the For[init, test, inc, body]  loop evaluates the arguments out of order, like this: init, test, body, inc.
 For[T = Ki = 0; i = 0, i < 5, i++,
   Print["i:", i];
   For[m = 1; n = 0.1; tmin = 1, T < tmin, tmin = T,
     Ki = Ki + m*n; T = Ki
   ];
   Print["T:", T]
 ]
 
 i:0
T:0.1
i:1
T:0.2
i:2
T:0.3
i:3
T:0.4
i:4
T:0.5
POSTED BY: Christopher French
Answer
9 months ago