# Mathematica storing output generated from a loop

1. Jan 28, 2012

### NRedacted

Hi,

I am running FindRoot on three variable system of three non-linear equations for a variety of parameters (four) and a huge range of parameter values. A simplified example is as follows:

one parameter and one variable:

I want to solve the equation f(x,a)==0, where 'x' is the variable and 'a' is a parameter, for values of a \in (0,10) and eventually like to create a table like this:

a Root of f
1 x1
2 x2
. .
. .
10 x10

I want this table of data to be in an easily extractable form so I can do many kinds of data manipulations with it (plot it, combined it with other data, run statistical tests, perform matrix operations etc etc) and I would also like to be able to store it outside of mathematica in an excel or some other data file.

Right now when I ran a loop over "i", I used {a_i,x_i}>>> "datafile" command to store the various bits of info after each run of the FindRoot inside my For loop. But I am having great difficulty extracting the data from my datafile. Apparently each entry is stored as one point and not as an array and I don't know how to get the data out. Actually, I'd appreciate it if you guys could also tell me how to get the data out of datafile into some array or matrix type structure.

Thanks a lot.

2. Jan 29, 2012

### Bill Simpson

Will this do?

In[1]:= tbl=Reap[
For[a=1,a<=10,a++,
Sow[Flatten[{a,x/.Solve[x+2a-1==4,x]}]]
]
][[2,1]]

Out[1]= {{1,3},{2,1},{3,-1},{4,-3},{5,-5},{6,-7},{7,-9},{8,-11},{9,-13},{10,-15}}

In[2]:= tbl>>"datafile.nb"

In[3]:= Export["tbl.csv",tbl]

Out[3]= tbl.csv

That will save in a format that << will pull back into Mathematica easily, the list can be manipulated for further calculations and a csv file can be read into Excel.

3. Jan 29, 2012

### NRedacted

Hi Bill,

Thanks a lot! That works!!

Cheers
NR

4. Jan 31, 2012

### Bill Simpson

I understand that there is sometimes an overwhelming compulsion to use "For."

Often in Mathematica using Table can be simpler. Compare this with the previous:

tbl = Table[Flatten[{a,x/.Solve[x+2a-1==4, x]}],{a,0,10}]

You can also insert ,{b,5,30} just before that closing ] to accomplish nested "For", etc.

5. Feb 1, 2012

### NRedacted

Hi Bill,

Thanks. I've tried to use the For loop because I understand it's syntax and I can translate the algorithm in my mind into the syntax for mathematica. The table command seems a lot more mysterious to me.

Ok, I will use the table since that seems the efficient thing to do.

I have three variables, wi, xi and yi on whom I have some 30 observations each, so i=1 to 30, stored in an excel file.

I want to use that data to solve an equation f(wi,xi,yi,zi)==0 for 30 values of zi. In addition, I want to calculate 10 other variables A1i(wi,xi,yi,zi),A2i(wi,xi,yi,zi),A3i(wi,xi,yi,zi)..A10i(wi,xi,yi,zi) for each vector (wi,xi,yi,zi), i=1,30. and I want to output everything.

So if I were to use table and flatten, would it work like this?

tbl = Table[Flatten[{w[],x[],y[],z[]/.FindRoot[f(w[],x[],y[],z[])==0, {z[],b[]}],A1[],A2[],...,A10[]}],{i,1,30}]

Where I have defined A1i etc outside the table command using A1i:=h(wi,xi,yi,zi) etc? I'm gonna try it right now. Thanks. I'll get back to you if it works or doesn't work.

6. Feb 1, 2012

### NRedacted

Hi Bill,

I can't get it to work. I'm getting alll tripped up here. This is a simplified version of my code, I've removed most of the variables and cut out most of the equation.:

First I load the data from excel into arrays named: CRCFT61525H1R,CRCFT61525H0S and CRCFT61525H0D

Then I copy the same data into variables with shorter names because I'm gonna use different data to solve the same equation later and I want the equation to reflect the variables.
Code (Text):

H1RR = CRCFT61525H1R;
H0SS = CRCFT61525H0S;
H0DD = CRCFT61525H0D;
Now I will define 10 further variables as functions of H1RR,H0SS,H0DD (variables for which I already have data) and H1SS (the fourth variable for which I have to solve for). Only 2 shown here.

Code (Text):
L0 := (CF  H0DD^Subscript[k, 2] - CR  H0DD^Subscript[k, 2] +
F  H0DD^Subscript[k, 2] + CR  H0SS^Subscript[k, 2] -
H0DD  H0SS^Subscript[k, 2] - H0DD^Subscript[k, 2]  H1RR +
H0SS^Subscript[k, 2]  H1RR - CF  H1RR^Subscript[k, 2] -
F  H1RR^Subscript[k, 2] + H0DD  H1RR^Subscript[k, 2])/(
H0DD^Subscript[k, 2]  H0SS^Subscript[k, 1] -
H0DD^Subscript[k, 1]  H0SS^Subscript[k, 2] -
H0DD^Subscript[k, 2]  H1RR^Subscript[k, 1] +
H0SS^Subscript[k, 2]  H1RR^Subscript[k, 1] +
H0DD^Subscript[k, 1]  H1RR^Subscript[k, 2] -
H0SS^Subscript[k, 1]  H1RR^Subscript[k, 2])
L1 := ((CF + F - H1RR) (H1SS^Subscript[k, 2] - \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$2$$]]))/(
H1RR^Subscript[k, 2] H1SS^Subscript[k, 1] -
H1RR^Subscript[k, 1] H1SS^Subscript[k, 2] - H1RR^Subscript[k, 2] \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$1$$]] + H1SS^Subscript[k, 2] \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$1$$]] + H1RR^Subscript[k, 1] \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$2$$]] - H1SS^Subscript[k, 1] \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$2$$]])
Finally I'll solve an equation to get values for H1SS.

This is not the actual equation but an arbitrary 20% of the equation, just so you can see what I am doing and also get a feel for what the equation looks like.
Code (Text):
Table[Flatten[{H0SS[[i]], H0DD[[i]], H1RR[[i]],
x /. FindRoot[(1/Subscript[k, 1]
x^(-Subscript[k, 1] + Subscript[k, 2]) Subscript[k,
2] ((F x^(-1 + Subscript[k, 1]) Subscript[k, 1])/(
x^(-1 + Subscript[k, 1] + Subscript[k, 2]) Subscript[k,
1] - x^(-1 + Subscript[k, 1] + Subscript[k, 2])
Subscript[k,
2]) - (x^(-1 + Subscript[k,
1]) (1 -
T) (F  H1RR[[i]]^Subscript[k, 2] x^Subscript[k, 1] -
F  H1RR[[i]]^Subscript[k, 1] x^Subscript[k, 2]
H1RR[[i]]^Subscript[k, 1] \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$2$$]] - x^Subscript[k, 1] \!
\*SubsuperscriptBox[$$H$$, $$0$$,
SubscriptBox[$$k$$, $$2$$]]) (x^(-1 + Subscript[k, 1] + Subscript[k,
2]) Subscript[k, 1] -
x^(-1 + Subscript[k, 1] + Subscript[k, 2]) Subscript[
k, 2]))))*( H1RR[[i]])^(Subscript[k, 1]) + ((
F  H0SS[[i]]^Subscript[k, 1] Subscript[k, 1])/(
H0SS[[i]]^(Subscript[k, 1] + Subscript[k, 2]) Subscript[k,
1] - H0SS[[i]]^(Subscript[k, 1] + Subscript[k, 2])
Subscript[k, 2]) + (
H0SS[[i]]^Subscript[k,
1] (-CF  H0DD[[i]]^Subscript[k, 2]  H0SS[[i]]^Subscript[k,
1] + CR  H0DD[[i]]^Subscript[k, 2]  H0SS[[i]]^
Subscript[k, 1] +
CF  H0DD[[i]]^Subscript[k, 1]  H0SS[[i]]^Subscript[k,
2] - CR  H0DD[[i]]^Subscript[k, 1]  H0SS[[i]]^
Subscript[k, 2] +
H0DD[[i]]^Subscript[k, 2]  H0SS[[i]]^Subscript[k, 1]
H1RR[[i]] -
H0DD[[i]]^Subscript[k, 1]  H0SS[[i]]^Subscript[k, 2]
H1RR[[i]] -
F  H0DD[[i]]^Subscript[k, 2]  H1RR[[i]]^Subscript[k,
1] - CF  H0SS[[i]]^Subscript[k, 2]  H1RR[[i]]^
Subscript[k, 1] +
H0DD[[i]]  H0SS[[i]]^Subscript[k, 2]  H1RR[[i]]^
Subscript[k, 1] +
F  H0DD[[i]]^Subscript[k, 1]  H1RR[[i]]^Subscript[k,
2] + CF  H0SS[[i]]^Subscript[k, 1]  H1RR[[i]]^
Subscript[k, 2] -
H0DD[[i]]  H0SS[[i]]^Subscript[k, 1]  H1RR[[i]]^
Subscript[k, 2]) (1 - T) Subscript[k,
1])/((- H0DD[[i]]^Subscript[k, 2]  H0SS[[i]]^Subscript[k,
1] + H0DD[[i]]^Subscript[k, 1]  H0SS[[i]]^Subscript[k,
2] +
H0DD[[i]]^Subscript[k, 2]  H1RR[[i]]^Subscript[k, 1] -
H0SS[[i]]^Subscript[k, 2]  H1RR[[i]]^Subscript[k, 1] -
H0DD[[i]]^Subscript[k, 1]  H1RR[[i]]^Subscript[k, 2] +
H0SS[[i]]^Subscript[k, 1]  H1RR[[i]]^Subscript[k, 2]) (
H0SS[[i]]^(Subscript[k, 1] + Subscript[k, 2]) Subscript[
k, 1] -
H0SS[[i]]^(Subscript[k, 1] + Subscript[k, 2]) Subscript[
k, 2]))) == 0, {x, 238}]}], {i, 1, 22}]

Once I get 22 values for x which I'll store into H1SS, I would evaluate expressions L0, L1 etc I defined earlier.

Unfortunately my findroot is not working inside the table and I'm getting some symbols for output. I would really appreciate your help here. Thanks.

7. Feb 1, 2012

### NRedacted

Hi Bill,

I was able to get the FindRoot inside the table to work. Unfortunately, it is very sensitive to the start values I give to the findroot and I will need to give different start values for different data. Probably, I'll create another array of start values which I think might work and feed that also into the table.

Thanks
NR