Mathematica storing output generated from a loop

In summary, the conversation involves using FindRoot to solve a system of three non-linear equations for a variety of parameters and a huge range of parameter values. The goal is to create a table with the data in an easily extractable form for further data manipulations and storage outside of Mathematica. The use of Table and Flatten functions are suggested for achieving this. The conversation also touches on the use of For loops and nested For loops in Mathematica. There is also a discussion on manipulating data from an Excel file and using functions to solve for variables.
  • #1
NRedacted
8
0
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.
 
Physics news on Phys.org
  • #2
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
Hi Bill,

Thanks a lot! That works!

Cheers
NR
 
  • #4
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
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.

Can you please help me with the following code:

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 going to try it right now. Thanks. I'll get back to you if it works or doesn't work.
 
  • #6
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 going to use different data to solve the same equation later and I want the equation to reflect the variables.
Code:
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:
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:
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
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
 

1. How can I store the output generated from a loop in Mathematica?

In order to store the output generated from a loop in Mathematica, you can use the function AppendTo to add the results to a list or use the Table function to create a table of the results.

2. Can I save the output from a loop in a specific format?

Yes, you can use the Export function to save the output from a loop in a specific format, such as CSV, TXT, or PDF.

3. How do I access the stored output from a loop?

To access the stored output from a loop, you can use the name of the list or table that you stored the results in. You can also use indexing to access specific elements of the list or table.

4. Can I store the output from a loop in a variable?

Yes, you can store the output from a loop in a variable by using the Set or SetDelayed functions. These functions allow you to assign a value to a variable.

5. How can I prevent my stored output from being overwritten in each loop iteration?

In order to prevent your stored output from being overwritten in each loop iteration, you can use the AppendTo function with a different list or table name for each iteration. This will ensure that the results are stored separately for each iteration of the loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
984
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
931
  • Programming and Computer Science
Replies
14
Views
624
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Programming and Computer Science
Replies
11
Views
991
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
898
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top