0
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ When you do the AppendTo, use Flatten, e.g AppendTo[myCaValss, Flatten[{Part[\[Mu]GMat, i, 1], Part[\[Mu]GMat, i, 2], cA[i]}]]; Also if you don't want the rules like c->0.118359 but just want numbers, then add c /. in front of your NSolve's. $\endgroup$
    – flinty
    Jun 23, 2020 at 11:45
  • 2
    $\begingroup$ The poor performance is because you're repeatedly reducing and NSolve-ing. You probably don't need the Reduce in there - you could move it out. $\endgroup$
    – flinty
    Jun 23, 2020 at 11:48

1 Answer 1

3
$\begingroup$

This should take care of some of the performance issues. You were doing Reduce in the NSolve unnecessarily and defining functions in the loop. Also you can use Table or better ParallelTable to build up the lists which is more efficient than using AppendTo here. It now takes < 10 seconds on my machine. Also, sometimes there are no solutions, or only one solution given by NSolve which is why the MatrixForm's below do not produce a proper matrix. Let me know if this is a problem:

μ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;
eqn = Reduce[1 + R*c == R*Exp[-M]*(1 - S*Exp[(1 + R*c)*M]/(R*c + 1 + S))];
cFn[i_,Fn_] := NSolve[eqn /. {
     R -> (π*α^2*Fn)/μGMat[[i, 1]], 
     S -> γ/μGMat[[i, 1]], 
     M -> μGMat[[i, 1]] (xm - x0)/μGMat[[i, 2]]},
     c, Reals];
myCaValss = ParallelTable[Join[μGMat[[i, 1 ;; 2]], Flatten@cFn[i,Fa]], {i, 100}];
myCbValss = ParallelTable[Join[μGMat[[i, 1 ;; 2]], Flatten@cFn[i,Fb]], {i, 100}];
MatrixForm[myCaValss]
MatrixForm[myCbValss]

This will allow you to get the first positive c solution for each row, discarding negative c and rows with zero solutions:

getPositiveC[sol_] := If[Length[sol] == 0, Missing, 
  SelectFirst[Flatten[sol][[All, 2]], # > 0 &]]

myCaValss = 
  Select[ParallelTable[
    Join[μGMat[[i, 1 ;; 2]], {getPositiveC@Flatten@cFn[i, Fa]}], {i, 100}], 
   NumericQ[#[[3]]] &];

myCbValss = 
  Select[ParallelTable[
    Join[μGMat[[i, 1 ;; 2]], {getPositiveC@Flatten@cFn[i, Fb]}], {i, 100}], 
   NumericQ[#[[3]]] &];

MatrixForm[myCaValss]
MatrixForm[myCbValss]
$\endgroup$
2
  • $\begingroup$ Hi flinty, thank you ! Question: Does the getPositive only look through the "first" c value if there are two? I am getting a lot of errors when i run the getPositive. I will eventually need to use each of the positive constants (including any second positive constants) to plug into an equation and make a table of rBA and rAB values ( my equations, with cA being the constant in rBA and cB being the constant in rAB). Is there an easy way to make it a nice matrix? $\endgroup$ Jun 23, 2020 at 14:48
  • $\begingroup$ @elcharlosmaster yes, it uses SelectFirst. The first one should be the smaller of the two as NSolve would have sorted the solutions when you obtained them. You can't make a nice matrix if some rows have two solutions while others have a single solution. $\endgroup$
    – flinty
    Jun 23, 2020 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.