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.Sat, 11 Jan 2014 21:22:52 +0100How to solve for volumehttps://ask.sagemath.org/question/10903/how-to-solve-for-volume/I have a function that looks like this:
def f1(x,y):
return max(0, a * (r - x*x-y*y))
**a** and **r** are constants, with **r** being the radius. Given the radius, how do I get sage to tell me what **a** needs to be so that the volume of the function is 1?
For example, solution for radius of 1.5 is 0.28294212105225836:
r = 1.5
a = 0.28294212105225836
dblquad(lambda x, y: f1(x,y), -r, r, lambda x: -r, lambda y: r)
(1.0000000008272796, 4.581186036178006e-09)Fri, 10 Jan 2014 22:14:43 +0100https://ask.sagemath.org/question/10903/how-to-solve-for-volume/Comment by kcrisman for <p>I have a function that looks like this:</p>
<pre><code>def f1(x,y):
return max(0, a * (r - x*x-y*y))
</code></pre>
<p><strong>a</strong> and <strong>r</strong> are constants, with <strong>r</strong> being the radius. Given the radius, how do I get sage to tell me what <strong>a</strong> needs to be so that the volume of the function is 1?</p>
<p>For example, solution for radius of 1.5 is 0.28294212105225836:</p>
<pre><code>r = 1.5
a = 0.28294212105225836
dblquad(lambda x, y: f1(x,y), -r, r, lambda x: -r, lambda y: r)
(1.0000000008272796, 4.581186036178006e-09)
</code></pre>
https://ask.sagemath.org/question/10903/how-to-solve-for-volume/?comment=16459#post-id-16459Just a brief note - if you leave a and r symbolic, you will probably want `max_symbolic`; if `a` and `r` always will have numeric values this isn't a problem (though I assume for your case it may be symbolic).Fri, 10 Jan 2014 22:31:34 +0100https://ask.sagemath.org/question/10903/how-to-solve-for-volume/?comment=16459#post-id-16459Comment by Lunatic for <p>I have a function that looks like this:</p>
<pre><code>def f1(x,y):
return max(0, a * (r - x*x-y*y))
</code></pre>
<p><strong>a</strong> and <strong>r</strong> are constants, with <strong>r</strong> being the radius. Given the radius, how do I get sage to tell me what <strong>a</strong> needs to be so that the volume of the function is 1?</p>
<p>For example, solution for radius of 1.5 is 0.28294212105225836:</p>
<pre><code>r = 1.5
a = 0.28294212105225836
dblquad(lambda x, y: f1(x,y), -r, r, lambda x: -r, lambda y: r)
(1.0000000008272796, 4.581186036178006e-09)
</code></pre>
https://ask.sagemath.org/question/10903/how-to-solve-for-volume/?comment=16458#post-id-16458yes, I know about max_symbolic. I've also tried getting sage to solve this symbolically and numerically, but I don't know what I'm doing wrong. Fri, 10 Jan 2014 22:40:40 +0100https://ask.sagemath.org/question/10903/how-to-solve-for-volume/?comment=16458#post-id-16458Answer by kcrisman for <p>I have a function that looks like this:</p>
<pre><code>def f1(x,y):
return max(0, a * (r - x*x-y*y))
</code></pre>
<p><strong>a</strong> and <strong>r</strong> are constants, with <strong>r</strong> being the radius. Given the radius, how do I get sage to tell me what <strong>a</strong> needs to be so that the volume of the function is 1?</p>
<p>For example, solution for radius of 1.5 is 0.28294212105225836:</p>
<pre><code>r = 1.5
a = 0.28294212105225836
dblquad(lambda x, y: f1(x,y), -r, r, lambda x: -r, lambda y: r)
(1.0000000008272796, 4.581186036178006e-09)
</code></pre>
https://ask.sagemath.org/question/10903/how-to-solve-for-volume/?answer=15911#post-id-15911My first thought would be to switch your integral to polar coordinates so you don't need the `max` bit at all.
sage: r = 1.5
sage: var('rad,theta')
(rad, theta)
sage: var('a')
a
sage: integrate(integrate(a*(r - rad^2)*rad,theta,0, 2*pi),rad,0,sqrt(r))
1.125*pi*a
sage: solve(_==1,a)
[a == 8/9/pi]
sage: n(_[0].rhs())
0.282942121052258
I think I did the change of coordinates right - $r dr d\theta$, right?
Now, this doesn't answer the more general question of how to do this with a max and a double numerical integral, where I would suggest using `find_root` - or just using Scipy in general, perhaps, since you are already using it, if you really do not need the symbolic capabilities; the syntax may end up more natural, who knows?Sat, 11 Jan 2014 21:22:52 +0100https://ask.sagemath.org/question/10903/how-to-solve-for-volume/?answer=15911#post-id-15911