4
$\begingroup$

The function mat[v1,vTot] has either one solution or three solution, depending on what vTot is. I want to make a table with pairs of points corresponding to the value of mat and its corresponding vTot. Is there a way to arrange the list on that way? Here is an example of what I get below:

p1[x_] := InterpolatingPolynomial[{{0, 0}, {0.25, 2}, {0.6, 4}, {1, 5, 0}, {1.8, 2, 0}, {6, 6}}, x]
p2[x_] := InterpolatingPolynomial[{{0, 0}, {0.25, 2}, {0.6, 4}, {1, 5, 0}, {1.8, 2, 0}, {6, 6}}, x]
en1[v1_] = FullSimplify[Integrate[p1[x], {x, 0, v1}]]; 
en2[v2_] = FullSimplify[Integrate[p2[x], {x, 0, v2}]]; 

enTot[v1_, vTotal_] = en1[v1] + en2[vTotal - v1]; 
vec[v1_, vTotal_] = FullSimplify[D[enTot[v1, vTotal], v1]]; 
mat[v1_, vTotal_] = FullSimplify[D[enTot[v1, vTotal], v1, v1]]; 

solns = v1 /. Table[NSolve[vec[v1, vTotal] == 0 && v1 > 0 && vTotal >= v1, v1, Reals], {vTotal, 0.5, 5, 0.1}]; 
matt = Table[{mat[v1, vTotal] /. NSolve[vec[v1, vTotal] == 0 && v1 > 0 && vTotal >= v1, v1, Reals], vTotal}, {vTotal, 1, 3, 1}]

{{{10.443833014855157}, 1}, {{4.408952530979699, -3.1989511484198374*^-8, 4.408952530959439}, 2}, {{25.671742505141687, -11.865664184452157, 25.671742505076736}, 3}}

What I want the result to look like is the following:

{{10.443833014855157, 1}, {4.408952530979699,2}, {-3.1989511484198374*^-8,2}, {4.408952530959439,2}, {25.671742505141687,3}, {-11.865664184452157,3}, {25.671742505076736,3}}
$\endgroup$
3
  • 2
    $\begingroup$ matt = matt /. {lst_List, n_Integer} :> (Sequence @@ ({#, n} & /@ lst)) $\endgroup$
    – Bob Hanlon
    Commented Jul 11, 2023 at 4:59
  • 2
    $\begingroup$ If the last part is a (integer or real) rather than an just integer, change LHS of the rule to {lst_List, n_?NumericQ} To get info, highlight the unknown command/function/operator in Mathematica and press F1 for help. Read the linked documentation. $\endgroup$
    – Bob Hanlon
    Commented Jul 11, 2023 at 5:14
  • 1
    $\begingroup$ Similar to @kglr: Thread/@matt//Catenate $\endgroup$
    – user1066
    Commented Jul 11, 2023 at 7:54

2 Answers 2

6
$\begingroup$

You can use composition of Splice and Thread

spliceThread = Splice @* Thread;

to post-process matt into the desired form:

Map[spliceThread] @ matt
{{10.4438, 1}, {4.40895, 2}, {-3.19895*10^-8, 2}, {4.40895, 2},  
 {25.6717, 3}, {-11.8657, 3}, {25.6717, 3}}

Alternatively, generate the desired list directly using:

Table[spliceThread @ 
   {mat[v1, vTotal] /. 
     NSolve[vec[v1, vTotal] == 0 && v1 > 0 && vTotal >= v1, v1, Reals], vTotal}, 
  {vTotal, 1, 3, 1}]
{{10.4438, 1}, {4.40895, 2}, {-3.19895*10^-8, 2}, {4.40895, 2},  
 {25.6717, 3}, {-11.8657, 3}, {25.6717, 3}}
$\endgroup$
6
$\begingroup$

step by step:

{{4.408952530979699, -3.1989511484198374*^-8, 4.408952530959439}, 2}

write a function.

f[{x_,y_}] := Map[{#, y}&][x]

then Map it

{{{10.443833014855157}, 1}, {{4.408952530979699, -3.1989511484198374*^-8, 4.408952530959439}, 2}, {{25.671742505141687, -11.865664184452157, 25.671742505076736}, 3}} //
Map[f] //
Apply[Join]

{{10.443833014855157, 1}, {4.408952530979699, 2}, {-3.1989511484198374*^-8, 2}, {4.408952530959439, 2}, {25.671742505141687, 3}, {-11.865664184452157, 3}, {25.671742505076736, 3}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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