My output for my "c" values is given in two sets of curly brackets, how can I remove these? I need to be able to take a "c" value and test whether it is positive or negative. Also how can I speed up my code? Thank you!
Description of what I am doing: Hello, I am running a for loop to solve equations with some specified parameters (α, γ, Fa, Fb,...) and two others (μ, g) varying on a matrix of values (μGMat) and then taking the solved "c" values and building a matrix for them called myCaValss and myCbValss.
What I want: (I would also want to make a version of my code where it only takes positive values of cA and cB and only builds a matrix of positive values. but this is not urgent.)
μGMat={{0.01,0.01},{0.02,0.01},{0.03,0.01},{0.04,0.01},{0.05,0.01},{0.06,0.01},{0.07,0.01},{0.08,0.01},{0.09,0.01},{0.1,0.01},{0.01,0.02},{0.02,0.02},{0.03,0.02},{0.04,0.02},{0.05,0.02},{0.06,0.02},{0.07,0.02},{0.08,0.02},{0.09,0.02},{0.1,0.02},{0.01,0.03},{0.02,0.03},{0.03,0.03},{0.04,0.03},{0.05,0.03},{0.06,0.03},{0.07,0.03},{0.08,0.03},{0.09,0.03},{0.1,0.03},{0.01,0.04},{0.02,0.04},{0.03,0.04},{0.04,0.04},{0.05,0.04},{0.06,0.04},{0.07,0.04},{0.08,0.04},{0.09,0.04},{0.1,0.04},{0.01,0.05},{0.02,0.05},{0.03,0.05},{0.04,0.05},{0.05,0.05},{0.06,0.05},{0.07,0.05},{0.08,0.05},{0.09,0.05},{0.1,0.05},{0.01,0.06},{0.02,0.06},{0.03,0.06},{0.04,0.06},{0.05,0.06},{0.06,0.06},{0.07,0.06},{0.08,0.06},{0.09,0.06},{0.1,0.06},{0.01,0.07},{0.02,0.07},{0.03,0.07},{0.04,0.07},{0.05,0.07},{0.06,0.07},{0.07,0.07},{0.08,0.07},{0.09,0.07},{0.1,0.07},{0.01,0.08},{0.02,0.08},{0.03,0.08},{0.04,0.08},{0.05,0.08},{0.06,0.08},{0.07,0.08},{0.08,0.08},{0.09,0.08},{0.1,0.08},{0.01,0.09},{0.02,0.09},{0.03,0.09},{0.04,0.09},{0.05,0.09},{0.06,0.09},{0.07,0.09},{0.08,0.09},{0.09,0.09},{0.1,0.09},{0.01,0.1},{0.02,0.1},{0.03,0.1},{0.04,0.1},{0.05,0.1},{0.06,0.1},{0.07,0.1},{0.08,0.1},{0.09,0.1},{0.1,0.1}};
α = 0.25;
γ = 0;
xm = 148;
x0 = 145;
Fa = 6;
Fb = 6;
myCaValss={};
myCbValss = {};
For[i=1,i<101,i++,
cA[i_]:=NSolve[Reduce[1+R*c==R*Exp[-M]*(1-S*Exp[(1+R*c)*M]/(R*c+1+S))]/.{R->(π*α^2*Fa)/Part[μGMat,i,1],S-> γ/Part[μGMat,i,1],M->Part[μGMat,i,1](xm-x0)/Part[μGMat,i,2]},c,Reals] ;
cB[i_]:=NSolve[Reduce[1+R*c==R*Exp[-M]*(1-S*Exp[(1+R*c)*M]/(R*c+1+S))]/.{R->(π*α^2*Fb)/Part[μGMat,i,1],S->γ/Part[μGMat,i,1],M->Part[μGMat,i,1](xm-x0)/Part[μGMat,i,2]},c,Reals];
AppendTo[myCaValss,{Part[μGMat,i,1],Part[μGMat,i,2],cA[i]}];
AppendTo[myCbValss,{Part[μGMat,i,1],Part[μGMat,i,2],cB[i]}]]
MatrixForm[myCaValss]
MatrixForm[myCbValss]
This gives me an output of :
(0.01 0.01 {{c->0.0412988}}
0.02 0.01 {{c->-0.0169765},{c->-0.0144978}}
0.03 0.01 {{c->-0.0254648},{c->-0.0253414}}
0.04 0.01 {{c->-0.0339531},{c->-0.0339469}}
0.05 0.01 {{c->-0.0424413},{c->-0.042441}}
0.06 0.01 {{c->-0.0509296},{c->-0.0509296}}
0.07 0.01 {{c->-0.0594178},{c->-0.0594178}}
0.08 0.01 {}
0.09 0.01 {}
0.1 0.01 {{c->-0.0848826},{c->-0.0848826}}
0.01 0.02 {{c->-0.00848826},{c->0.214642}}
0.02 0.02 {{c->-0.0169765},{c->0.0328105}}
0.03 0.02 {{c->-0.0254648},{c->-0.0143558}}
0.04 0.02 {{c->-0.0339531},{c->-0.0314743}}
0.05 0.02 {{c->-0.0424413},{c->-0.0418882}}
0.06 0.02 {{c->-0.0509296},{c->-0.0508062}}
0.07 0.02 {{c->-0.0594178},{c->-0.0593903}}
0.08 0.02 {{c->-0.0679061},{c->-0.0679}}
0.09 0.02 {{c->-0.0763944},{c->-0.076393}}
0.1 0.02 {{c->-0.0848826},{c->-0.0848823}}
0.01 0.03 {{c->-0.00848826},{c->0.359391}}
0.02 0.03 {{c->0.118359}}
0.03 0.03 {{c->0.0243223}}
0.04 0.03 {{c->-0.0339531},{c->-0.0156374}}
0.05 0.03 {{c->-0.0424413},{c->-0.0357034}}
0.06 0.03 {{c->-0.0509296},{c->-0.0484508}}
0.07 0.03 {{c->-0.0594178},{c->-0.058506}}
0.08 0.03 {{c->-0.0679061},{c->-0.0675706}}
0.09 0.03 {{c->-0.0763944},{c->-0.076271}}
0.1 0.03 {{c->-0.0848826},{c->-0.0848372}}
0.01 0.04 {{c->-0.00848826},{c->0.463878}}
0.02 0.04 {{c->-0.0169765},{c->0.206154}}
0.03 0.04 {{c->-0.0254648},{c->0.0799344}}
0.04 0.04 {{c->-0.0339531},{c->0.015834}}
0.05 0.04 {{c->-0.0424413},{c->-0.0189236}}
0.06 0.04 {{c->-0.0509296},{c->-0.0398206}}
0.07 0.04 {{c->-0.0594178},{c->-0.0541703}}
0.08 0.04 {{c->-0.0679061},{c->-0.0654274}}
0.09 0.04 {{c->-0.0763944},{c->-0.0752235}}
0.1 0.04 {{c->-0.0848826},{c->-0.0843296}}
0.01 0.05 {{c->-0.00848826},{c->0.540323}}
0.02 0.05 {{c->-0.0169765},{c->0.284218}}
0.03 0.05 {{c->-0.0254648},{c->0.139834}}
0.04 0.05 {{c->0.0567649}}
0.05 0.05 {{c->-0.0424413},{c->0.00734575}}
0.06 0.05 {{c->-0.0509296},{c->-0.0236059}}
0.07 0.05 {{c->-0.0594178},{c->-0.0444223}}
0.08 0.05 {{c->-0.0596764}}
0.09 0.05 {{c->-0.0763944},{c->-0.0718778}}
0.1 0.05 {{c->-0.0848826},{c->-0.0824039}}
0.01 0.06 {{c->-0.00848826},{c->0.598042}}
0.02 0.06 {{c->-0.0169765},{c->0.350903}}
0.03 0.06 {{c->-0.0254648},{c->0.197665}}
0.04 0.06 {{c->-0.0339531},{c->0.101382}}
0.05 0.06 {{c->0.0396437}}
0.06 0.06 {{c->-0.0509296},{c->-0.00114251}}
0.07 0.06 {{c->-0.0594178},{c->-0.0292205}}
0.08 0.06 {{c->-0.0495905}}
0.09 0.06 {{c->-0.0763944},{c->-0.0652854}}
0.1 0.06 {{c->-0.0848826},{c->-0.0781447}}
0.01 0.07 {{c->-0.00848826},{c->0.642951}}
0.02 0.07 {{c->-0.0169765},{c->0.407396}}
0.03 0.07 {{c->-0.0254648},{c->0.250988}}
0.04 0.07 {{c->-0.0339531},{c->0.146139}}
0.05 0.07 {{c->-0.0424413},{c->0.0748778}}
0.06 0.07 {{c->0.0254967}}
0.07 0.07 {{c->-0.0594178},{c->-0.00963078}}
0.08 0.07 {{c->-0.0679061},{c->-0.0354729}}
0.09 0.07 {{c->-0.0552661}}
0.1 0.07 {{c->-0.0848826},{c->-0.0711188}}
0.01 0.08 {{c->-0.00848826},{c->0.678801}}
0.02 0.08 {{c->-0.0169765},{c->0.45539}}
0.03 0.08 {{c->0.299188}}
0.04 0.08 {{c->-0.0339531},{c->0.189177}}
0.05 0.08 {{c->-0.0424413},{c->0.110914}}
0.06 0.08 {{c->0.0544696}}
0.07 0.08 {{c->0.0130219}}
0.08 0.08 {{c->-0.0679061},{c->-0.018119}}
0.09 0.08 {{c->-0.0763944},{c->-0.0421763}}
0.1 0.08 {{c->-0.0848826},{c->-0.0613649}}
0.01 0.09 {{c->-0.00848826},{c->0.708043}}
0.02 0.09 {{c->-0.0169765},{c->0.496441}}
0.03 0.09 {{c->-0.0254648},{c->0.342415}}
0.04 0.09 {{c->-0.0339531},{c->0.229644}}
0.05 0.09 {{c->0.146434}}
0.06 0.09 {{c->-0.0509296},{c->0.0844057}}
0.07 0.09 {{c->-0.0594178},{c->0.0375541}}
0.08 0.09 {{c->-0.0679061},{c->0.00157734}}
0.09 0.09 {{c->-0.0763944},{c->-0.0266073}}
0.1 0.09 {{c->-0.0848826},{c->-0.0492086}}
0.01 0.1 {{c->-0.00848826},{c->0.73233}}
0.02 0.1 {{c->-0.0169765},{c->0.531835}}
0.03 0.1 {{c->-0.0254648},{c->0.381105}}
0.04 0.1 {{c->-0.0339531},{c->0.267241}}
0.05 0.1 {{c->0.180689}}
0.06 0.1 {{c->-0.0509296},{c->0.114369}}
0.07 0.1 {{c->-0.0594178},{c->0.0630386}}
0.08 0.1 {{c->0.0228118}}
0.09 0.1 {{c->-0.0763944},{c->-0.00918886}}
0.1 0.1 {{c->-0.0848826},{c->-0.0350956}}
)
Where the "c" values are in two sets of curly brackets. How can I fix this? My code also takes a good half hour to run, is there any way to speed it up? Thank you.
AppendTo
, useFlatten
, e.gAppendTo[myCaValss, Flatten[{Part[\[Mu]GMat, i, 1], Part[\[Mu]GMat, i, 2], cA[i]}]];
Also if you don't want the rules likec->0.118359
but just want numbers, then addc /.
in front of yourNSolve
's. $\endgroup$NSolve
-ing. You probably don't need theReduce
in there - you could move it out. $\endgroup$