Message Boards Message Boards

0
|
4146 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

A certain kind of loop

Posted 10 years ago

Dear community,

I wrote a simulation in Mathematica. Many processes in the this simulation are dependent one parameter, lets call it omega.

The simulation includes:

A checking part dependent on omega. An integration depending on omega. (this is dependent on the previous result) A plot of the integration. (this is dependent on the previous result)

Now I want to use a loop so the whole thing is done for different omega. Sort of like : Run the whole simulation for one omega, then for the next omega, then for the next omega and so on. How do I do that as the command Do for example does not work here.

In short it should look something like that For omega between a and b in steps of c, do: -Step 1 -Step 2 -Step 3

Thanks a lot.

POSTED BY: Marcel Duda
3 Replies

Hi Marcel,

I'm not entirely sure what you mean by "does not give me a proper output", but I'll try to guess.

First of all, this loop will evaluate for omega=0, 1, and 2, because that's how you specified your iterator. Take a look at the Documentation article for Do to see all the different ways for iterator, it's really quite flexible, so you should be able to find a syntax that suits you. If you want omega=0 and 1, you will need {omega,0,1} at the end of the loop.

If you're missing something like a printed output, well, you're not asking Mathematica to print anything: each of your assignments to the si ends with a semicolon, which means that they're all part of a CompoundExpression. It also means that you're suppressing output. This is the best way to write a Do loop with several statements inside it, so you did this correctly. Mathematica will now assign values to the si, but since they have the same name in each cycle of the loop, they will be overwritten, and I'm guessing that you don't that to happen. You could try the following:

Do[
     s1[omega] = Boole[Table[test1[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi,di}]];
     s2[omega] = Boole[Table[test2[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi,di}]];
     s3[omega] = Boole[Table[test3[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi,di}]];
     s4[omega] = Boole[Table[test4[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi,di}]];
, {omega, 0, 2}]

And now you can ask Mathematica for any of these values:

s1[0]
s3[1]

...and so on.

Also, I'm just assuming that all the symbols in your code (testi, s, t, u, k, di, imax, and so forth) have sensible values assigned to them before the loop is evaluated.

I hope that was at least somewhat helpful to you.

~ Bianca

POSTED BY: Bianca Eifert
Posted 10 years ago

How come, this does not give me a proper output:

Do[s1 = Boole[
   Table[test1[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi, 
     di}]];
 s2 = Boole[
   Table[test2[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi, 
     di}]];
 s3 = Boole[
   Table[test3[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi, 
     di}]];
 s4 = Boole[
   Table[test4[omega, s[i], t[i], u[i]], {i, 1, imax}, {k, 0, 2 Pi, 
     di}]];, {omega, 0, 2}]

I would imagine that if I use a Do loop that he will evaluate these four thingies for omega=0 and 1. I am really specific about this because this is only part of the my code and there are many things following.

POSTED BY: Marcel Duda
Posted 10 years ago

This is a small example that has a checking part, an Integrate part and a plotting part all in a Do loop, it may be a basis on what you are looking for.

Do[If[n <= 5 && n >= 2, i = Integrate[Sin[n \[Theta]], \[Theta]]; 
  Print[n, "  ", 
   PolarPlot[i, {\[Theta], 0, 2 \[Pi]}, ImageSize -> 300, 
    PlotStyle -> Thick, 
    ColorFunction -> Function[{x, y, t, r}, Hue[r]]]]], {n, 1, 10}]

Paul.

POSTED BY: Paul Cleary
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