Mathematica Mathematica storing output generated from a loop

AI Thread Summary
The discussion revolves around solving a system of non-linear equations using Mathematica's FindRoot function, specifically for multiple parameters and variables. The user aims to generate a table of roots for varying parameter values and export this data for further manipulation in Excel. Initially, they faced challenges in extracting data from a file, which was resolved by using the Export function to save the data in CSV format. The conversation highlights the preference for using the Table function over For loops for efficiency and simplicity in Mathematica. The user also seeks assistance in applying this method to a more complex scenario involving three variables and multiple observations stored in Excel. They aim to compute additional variables based on the results of the FindRoot function and express their frustration with the sensitivity of FindRoot to starting values. Ultimately, the user plans to create an array of starting values to improve the robustness of their calculations.
NRedacted
Messages
7
Reaction score
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
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.
 
Hi Bill,

Thanks a lot! That works!

Cheers
NR
 
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.
 
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.
 
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.
 
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
 

Similar threads

Replies
6
Views
4K
Replies
4
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
4K
Replies
2
Views
2K
Replies
1
Views
2K
Back
Top