Message Boards Message Boards

0
|
11947 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

How to turn a flowchart into a Loop in Mathematica

Posted 11 years ago
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
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
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