ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 06 Oct 2022 15:19:44 +0200Storing the positive solution only of a quadratic in a variablehttps://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/Hi everyone, I am quite new to Sagemath, and I have the following problem:
DPi_R_D_R_AI = (Pistar_R_D - Pistar_R_AI).simplify_full()
Is a quadratic function of c (or, a parabola).
I need to find the positive roots only, and I know that the only positive root is the one with the "+" sign (it is a parametric function), and I need to store this root in a variable.
Problem: when I try
c_prof = solve(DPi_R_D_R_AI == 0,c)[0].rhs()
Sagemath only displays the negative root! I tried to have a look at [this post](https://ask.sagemath.org/question/9209/how-to-get-positive-solution-only-of-a-quadratic-equation/), but I don't have a "number" I can exploit: c>=0 means nothing for Sagemath...
How should I approach this?
Thanks for the attention,
MatteoThu, 06 Oct 2022 10:08:46 +0200https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/Answer by Boyko_Bu for <p>Hi everyone, I am quite new to Sagemath, and I have the following problem:</p>
<pre><code>DPi_R_D_R_AI = (Pistar_R_D - Pistar_R_AI).simplify_full()
</code></pre>
<p>Is a quadratic function of c (or, a parabola).</p>
<p>I need to find the positive roots only, and I know that the only positive root is the one with the "+" sign (it is a parametric function), and I need to store this root in a variable.</p>
<p>Problem: when I try</p>
<pre><code>c_prof = solve(DPi_R_D_R_AI == 0,c)[0].rhs()
</code></pre>
<p>Sagemath only displays the negative root! I tried to have a look at <a href="https://ask.sagemath.org/question/9209/how-to-get-positive-solution-only-of-a-quadratic-equation/">this post</a>, but I don't have a "number" I can exploit: c>=0 means nothing for Sagemath...</p>
<p>How should I approach this?</p>
<p>Thanks for the attention,</p>
<p>Matteo</p>
https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/?answer=64325#post-id-64325Ok, I randomly tried this:
c_prof = solve(DPi_R_D_R_AI == 0,c)[1].rhs()
And it works! Can I ask you, then, why is it the case?
Thanks,
MatteoThu, 06 Oct 2022 10:13:18 +0200https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/?answer=64325#post-id-64325Comment by Boyko_Bu for <p>Ok, I randomly tried this:</p>
<pre><code>c_prof = solve(DPi_R_D_R_AI == 0,c)[1].rhs()
</code></pre>
<p>And it works! Can I ask you, then, why is it the case?</p>
<p>Thanks,</p>
<p>Matteo</p>
https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/?comment=64331#post-id-64331ok, so basically the [i] refers to the ith element of the list? Thanks a lot btw!Thu, 06 Oct 2022 15:19:44 +0200https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/?comment=64331#post-id-64331Comment by rburing for <p>Ok, I randomly tried this:</p>
<pre><code>c_prof = solve(DPi_R_D_R_AI == 0,c)[1].rhs()
</code></pre>
<p>And it works! Can I ask you, then, why is it the case?</p>
<p>Thanks,</p>
<p>Matteo</p>
https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/?comment=64326#post-id-64326If `DPi_R_D_R_AI` is a quadratic in `c` with two (symbolically) distinct roots then `solve(DPi_R_D_R_AI == 0,c)` is a list with two elements (try evaluating/printing it) and `solve(DPi_R_D_R_AI == 0,c)[1].rhs()` selects the right-hand side of one of them (while the same expression with `[0]` selects the right-hand side of the other).Thu, 06 Oct 2022 11:29:39 +0200https://ask.sagemath.org/question/64324/storing-the-positive-solution-only-of-a-quadratic-in-a-variable/?comment=64326#post-id-64326