# How can I make a list of values "remember" the entered parameters?

I generate a list of values for r using:

s[d_?NumericQ] :=
Reduce[r^3 - 10 r^2 + (25 + 100*d^{2}) r - 4 == 0, r];
Evaluate[(s[#] & /@ Range[0, 1, 0.1])]


I wish to then use these values of r in another function:

0.5 - 0.2 r PlusMinus[Sqrt[0.01 r^2 - d^2]]


I'm not sure how to do this while having the values of r "remember" the value of the parameter d that the r was calculated with. I thought of trying to combine the functions, but I end up solving both parts with each value of d.

One way is to save the d along with the solution at the location it is generated.

So instead of just returning {r1,r2,r3...} solutions, you return {{d,r1},{d,r2},...}

So the data is always together in one list. Then when you run your code 0.5 - 0.2 r PlusMinus[Sqrt[0.01 r^2 - d^2]] now you run it against the list which has both d and r in it.

s[d_?NumericQ] := Module[{res},
res = Last[#] & /@ List @@ Reduce[r^3 - 10 r^2 + (25 + 100*d^2) r - 4 == 0, r];
{d, #} & /@ res
];
rValues = Flatten[s[#] & /@ Range[0, 1, 0.1], 1]


This generates

In the above, the first entry is d and the second is r solution. Now simply do the following, where #[[1]] is the d and #[[2]] is the r in each sublist.

(0.5 - 0.2 #[[2]] PlusMinus[ Sqrt[0.01 #[[2]]^2 - #[[1]]^2]]) & /@ rValues


Another way to do this is as follows:

s[d_?NumericQ] := SolveValues[r^3 - 10 r^2 + (25 + 100*d^{2}) r - 4 == 0, r]
rvalues = Join @@ (Transpose /@ Table[{Array[d &, Length[s[d]]], s[d]}, {d, 0, 1, 0.1}])


The above code generates the same output as the Nasser's code:

Proceeding as @Nasser did, we get:

(0.5 - 0.2 #[[2]]*PlusMinus[Sqrt[0.01 #[[2]]^2 - #[[1]]^2]]) & /@ rvalues


• What if I wanted to plot (d, lambda), where lambda is the final list of items from this post. This wouldn't be a complex plot, instead I want to plot the y-values as the magnitudes of the real and imaginary components of each value of lambda, using color or otherwise to signify the difference. @Nasser
– ξύλο
Commented Dec 28, 2022 at 15:57
• @ξύλο it would be better to make a new question post asking that new question, rather than attempting to modify or completely change this current question post. I think it would make for a great question that you can link to this question from! Then you can get an answer specific to that sort of topic (plotting & such), as well as get an answer specific to the topic of this question! Commented Dec 29, 2022 at 6:57
Clear[s]
s[d_?NumericQ] := Module[{roots, r},
roots = {ToRules[
Reduce[r^3 - 10 r^2 + (25 + 100*d^{2}) r - 4 == 0, r]]};
(*Echo[roots];*)
0.5 - 0.2 r PlusMinus[Sqrt[0.01 r^2 - d^2]] /. roots
]

Evaluate[(s[#] & /@ Range[0, 1, 0.1])]