3
$\begingroup$

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.

$\endgroup$

3 Answers 3

5
$\begingroup$

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

Mathematica graphics

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

Mathematica graphics

$\endgroup$
4
$\begingroup$

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:

enter image description here

Proceeding as @Nasser did, we get:

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

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ 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 $\endgroup$
    – ξύλο
    Commented Dec 28, 2022 at 15:57
  • 2
    $\begingroup$ @ξύλο 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! $\endgroup$ Commented Dec 29, 2022 at 6:57
1
$\begingroup$
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])]
$\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.