2
votes

I have to solve a system of non-linear equations for a wide range of parameter space. I'm using FindRoot, which is sensitive to initial start point so I have to do it by hand and by trial and error and plotting, rather than putting the equations in a loop or in a table.

So what I want to do is create a database or a Matrix with a fixed number of columns but variable number of rows so I can keep appending it with new results as and when I solve for them.

Right now I've used something like:

{{{xx, yy}} = {x, y} /. FindRoot[{f1(x,y) == 0,f2(x,y)==0}, {x,a},{y,b}],
g(xx,yy)} >>> "Attempt1.txt"

Where I am solving for two variables and then storing the variables and also a function g(xx,yy) of the variables.

This seems to work for me but the result is not a Matrix any more but the data is stored as some text type thing.

Is there anyway I can get this to stay a matrix or a database where I keep adding rows to it each time I solve for FindRoot by hand? Again, I need to do FindRoot by hand because it is sensitive to the start points and I don't know the good start points without first plotting it.

Thanks a lot

2
I don't understand why this is not a matrix anymore. Consider this Put[{{1, 2}, {3, 4}}, "tmp.mx"]; Get["tmp.mx"] which is exactly the same expression after it is imported again. - halirutan
It's storing everything as one entry. Suppose I did data=Import["temp.mx"] and then I do dimensions[data], it answers 1. I have no idea how to extract the information. When I do data[[1]] it outputs the entire data. I cannot extract individual pieces of information. When I do data[[1,1]] is gives an error. - user1169757
So I had done {x1,x2,x3,x4,x5,x6,x7,x8,x8,x10} >>> "temp.mx" 22 times inside the For loop. Now when I did data=Import["temp.mx","Table"] and then do Dimensions[data] I get 110. What I really wanted was a matrix with 10 columns and 22 rows. Each of my 10 data per row/record is stored as 5 pieces of data, giving a total dimension of 5x22=110. For example, data[[1]]={"{24258.225756005108,", "0.0001254874133927587,", \ "0.10668678000535163,"} and so on. Is there anyway I can get my data in a clean 10x22 matrix where data[[i,j]] represents the jth value for the ith record? Thanks. - user1169757
Oh I neglected to point out that the data was generated in a loop.(I've done this first outside a loop when I had to hand pick the starting points etc. then later I did some calculations in a loop. So I guess my follow up question is for the second loop case) For[i=1,i<23,i++,{f1(i),f2(i),..,f10(i)}>>>"temp.mx"] Now when I try to Import "temp.mx" I get all kinds of things except a 22x10 matrix that I really badly want. thanks. - user1169757

2 Answers

0
votes

Unless I'm not understanding what you're trying to do, this should work

results = {};
results = Append[Flatten[{{xx, yy} = {x, y} /. FindRoot[{f1(x,y) == 0,f2(x,y)==0}, {x,a},{y,b}],g(xx,yy)}],results];

Then every time you're tying to add a line to the matrix results by hand, you would just type

results = Append[Flatten[{{xx, yy} = {x, y} /. FindRoot[{f1(x,y) == 0,f2(x,y)==0}, {x,a},{y,b}],g(xx,yy)}],results];

By the way, to get around the problem of sensitivity to the initial a and b values, you could explore the parameter space in a loop, varying the parameters slowly and using the solution of x and y from the previous loop iteration for your new a and b values each time.

0
votes

What you want to do can be achieved by using Read instead of Get. While Get reads the complete file in one run, Read can be adjusted to extract a single Expression, Byte, Number and many more. So what you should do is open your file and read expression after expression and pack it inside a list.

PutAppend[{{1, 2}, {3, 4}}, "tmp.mx"]
PutAppend[{{5, 6}, {7, 8}}, "tmp.mx"]
PutAppend[{{9, 23}, {11, 12}}, "tmp.mx"]
PutAppend[{{13, 14}, {15, 16}}, "tmp.mx"]

stream = OpenRead["tmp.mx"];
mat = ArrayPad[
   NestWhileList[Read[stream, Expression] &, 
    stream = OpenRead["tmp.mx"], # =!= EndOfFile &], -1];
Close[stream];

And now you have in mat a list containing all lines. The ArrayPad, which cuts off one element at each end is necessary because the first element contains the output of the OpenRead and the last element contains EndOfFile. If you are not familiar with functional constructs like NestWhileList then you can put it in a loop as you like, since it is really just the iterated calls to Read

stream = OpenRead["tmp.mx"];
mat = {};
AppendTo[mat, Read[stream, Expression]];
AppendTo[mat, Read[stream, Expression]];
AppendTo[mat, Read[stream, Expression]];
AppendTo[mat, Read[stream, Expression]];
Close[stream];