Message Boards Message Boards

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

How to include calculations in a loop (Do,Nest,Table,...others)?

Posted 11 years ago
Since I am not very familiar how to make calculations within a loop I look for possibilities (Do,Nest,Table,others?) for that example:
Intilial t is:
d[0]=7;
Then:
1. Calculate a
a=d[t]+2;

2. Find root of b:
b = Exp[x^2] - a == x Sqrt[\[Pi]]
FRoo = FindRoot[b, {x, 3}]
x /. FRoo

3. use x for the calculation of c:
c = 2 X Sqrt[t]
c /. {X -> x}

4. use x for the calculation of d:
d = 2 X Sqrt[2t]
d /. {X -> x}

Do steps 1 to 4 for t from 0 to 100. I need c, d to plot over t afterwards. 

I really appreciate your help and your answer will be a valuable template for further calculations of this type to me.
POSTED BY: Matt Shukoff
3 Replies
Posted 11 years ago
Here is one, of many, ways that you might organize this.

Since your example wants each result to depend on the value of the previous result NestList seems like a good choice.
Since you have three variables, c,d and t that you are interested in start with initial value of {0, 7, 0} to represent {c0,d0,t0}.
Write a function, call it f, that will be given {prevc,prevd,prevt} each time and it must calculate {newc,newd,newt} as a result.
f might start as f[{c_,d_,t_}]:=... Note those { } around c,d,t declare f is going to be given a vector of 3 elements as an argument
and it also must return a vector of the three new elements.
Note that you cannot change the value of c,d or t inside f, you can only calculate new values from these.
You might use Module to construct f so that you can have local variables and multiple statements separated by semicolons inside.
Then you use something like list=NestList[f, {0,7,0}, 100].
That should return a result like {{c0,d0,t0},{c1,d1,t1},{c2,d2,t2}...{c100,d100,t100}}.
Then c=list[[All,1]];d=list[[All,2]]; will extract your c list and d list and you can try ListPlot[ c] or even ListPlot[{c,d}].

Does this give you enough hints that you can see how to structure your solution?

Note that x/.FRoo only gives you the result of x when the value of FRoo is substituted. It does not then store this new value in x.
This is a somewhat common misunderstanding that new users sometimes fall into.
If you want to store a new value in x then you need to do something like x=x/.FRoo where there is both a replacement and assignment,
but remember as I wrote above, you cannot change the values of parameters of a function inside that function.

Note that it is possible to pack your entire problem inside the NestList without defining a separate function, and many do that,
but for new users I think it is less likely to result in confusion and errors if you write a separate function and concentrate all
your attention on getting that function right instead of trying to shove complicated funny notation inside the NestList itself.
POSTED BY: Bill Simpson
Posted 11 years ago
This gives me a very good idea how to construct that code! I also appreciate your remarks on details to consider.
Thank you very much! 
POSTED BY: Matt Shukoff
Posted 11 years ago
This was cross posted to mathematica.stackexchange.com

http://mathematica.stackexchange.com/q/38633/4330
POSTED BY: Jacob Akkerboom
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