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, 26 Aug 2010 15:51:26 +0200Plot error: a free variable?https://ask.sagemath.org/question/7641/plot-error-a-free-variable/This should be a rather simple question, and I think I am just not understanding the error.
I have a function say `f(x,y)= x*y^3*normal(y,1,2)` where normal() is the normal pdf equation (x,mean,standard deviation).
So I want to integrate across the pdf and then plot x. What I thought I would do was:
g = f.integral(y,0,Infinity)
plot(g,-10,10)
But that give the error: ValueError: free variable: x
What am I missing?
Thu, 26 Aug 2010 13:09:11 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/Comment by willmwade for <p>This should be a rather simple question, and I think I am just not understanding the error.</p>
<p>I have a function say <code>f(x,y)= x*y^3*normal(y,1,2)</code> where normal() is the normal pdf equation (x,mean,standard deviation).</p>
<p>So I want to integrate across the pdf and then plot x. What I thought I would do was:</p>
<pre><code>g = f.integral(y,0,Infinity)
plot(g,-10,10)
</code></pre>
<p>But that give the error: ValueError: free variable: x</p>
<p>What am I missing?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22834#post-id-22834See below.Thu, 26 Aug 2010 14:13:35 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22834#post-id-22834Comment by kcrisman for <p>This should be a rather simple question, and I think I am just not understanding the error.</p>
<p>I have a function say <code>f(x,y)= x*y^3*normal(y,1,2)</code> where normal() is the normal pdf equation (x,mean,standard deviation).</p>
<p>So I want to integrate across the pdf and then plot x. What I thought I would do was:</p>
<pre><code>g = f.integral(y,0,Infinity)
plot(g,-10,10)
</code></pre>
<p>But that give the error: ValueError: free variable: x</p>
<p>What am I missing?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22839#post-id-22839I'd like to try to help, but I don't find `normal` as a builtin function. Do you import it, or have you defined it yourself? The error comes from Sage trying to use `fast_float` to evaluate your function and apparently finding two variables somehow. Maybe post your whole session?Thu, 26 Aug 2010 13:28:14 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22839#post-id-22839Comment by ccanonc for <p>This should be a rather simple question, and I think I am just not understanding the error.</p>
<p>I have a function say <code>f(x,y)= x*y^3*normal(y,1,2)</code> where normal() is the normal pdf equation (x,mean,standard deviation).</p>
<p>So I want to integrate across the pdf and then plot x. What I thought I would do was:</p>
<pre><code>g = f.integral(y,0,Infinity)
plot(g,-10,10)
</code></pre>
<p>But that give the error: ValueError: free variable: x</p>
<p>What am I missing?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22841#post-id-22841ValueError is documented in python as an exception thrown when the domain of the passed argument doesn't match the function domain if I read the docs correctly.Thu, 26 Aug 2010 13:14:14 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22841#post-id-22841Comment by ccanonc for <p>This should be a rather simple question, and I think I am just not understanding the error.</p>
<p>I have a function say <code>f(x,y)= x*y^3*normal(y,1,2)</code> where normal() is the normal pdf equation (x,mean,standard deviation).</p>
<p>So I want to integrate across the pdf and then plot x. What I thought I would do was:</p>
<pre><code>g = f.integral(y,0,Infinity)
plot(g,-10,10)
</code></pre>
<p>But that give the error: ValueError: free variable: x</p>
<p>What am I missing?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22842#post-id-22842Is this a case of not declaring symbolic variables with var()?Thu, 26 Aug 2010 13:12:34 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22842#post-id-22842Answer by niles for <p>This should be a rather simple question, and I think I am just not understanding the error.</p>
<p>I have a function say <code>f(x,y)= x*y^3*normal(y,1,2)</code> where normal() is the normal pdf equation (x,mean,standard deviation).</p>
<p>So I want to integrate across the pdf and then plot x. What I thought I would do was:</p>
<pre><code>g = f.integral(y,0,Infinity)
plot(g,-10,10)
</code></pre>
<p>But that give the error: ValueError: free variable: x</p>
<p>What am I missing?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?answer=11561#post-id-11561I agree, it must be a problem with `normal`. The following works as expected
<pre>
sage: f(x,y)= x*y^3
sage: f
(x, y) |--> x*y^3
sage: g = f.integral(y,0,7)
sage: g
(x, y) |--> 2401/4*x
sage: plot(g,-5,5)
</pre>
Where as this returns the error you've seen:
sage: f(x,y,z)= x*y^3+z
sage: g = f.integral(y,0,7)
sage: plot(g,-5,5)
...
ValueError: free variable: z
If you try to simply integrate the `normal` function, do you get a number, or another function of `x`?Thu, 26 Aug 2010 13:41:18 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?answer=11561#post-id-11561Comment by willmwade for <p>I agree, it must be a problem with <code>normal</code>. The following works as expected</p>
<pre>sage: f(x,y)= x*y^3
sage: f
(x, y) |--> x*y^3
sage: g = f.integral(y,0,7)
sage: g
(x, y) |--> 2401/4*x
sage: plot(g,-5,5)
</pre>
<p>Where as this returns the error you've seen:</p>
<pre><code>sage: f(x,y,z)= x*y^3+z
sage: g = f.integral(y,0,7)
sage: plot(g,-5,5)
...
ValueError: free variable: z
</code></pre>
<p>If you try to simply integrate the <code>normal</code> function, do you get a number, or another function of <code>x</code>?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22835#post-id-22835Oh and before this I did run var('x d') so both of those are declared.Thu, 26 Aug 2010 14:13:18 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22835#post-id-22835Comment by willmwade for <p>I agree, it must be a problem with <code>normal</code>. The following works as expected</p>
<pre>sage: f(x,y)= x*y^3
sage: f
(x, y) |--> x*y^3
sage: g = f.integral(y,0,7)
sage: g
(x, y) |--> 2401/4*x
sage: plot(g,-5,5)
</pre>
<p>Where as this returns the error you've seen:</p>
<pre><code>sage: f(x,y,z)= x*y^3+z
sage: g = f.integral(y,0,7)
sage: plot(g,-5,5)
...
ValueError: free variable: z
</code></pre>
<p>If you try to simply integrate the <code>normal</code> function, do you get a number, or another function of <code>x</code>?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22836#post-id-22836I only have trouble with it when I want to integrate with the same variable as is in normal() and having that variable outside of normal().
Example:
x/(d+1)*normal(d,2.85,0.61)
the integral(normal(d,2.85,0.61),d,0,Infinity)= just under 1Thu, 26 Aug 2010 14:11:58 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22836#post-id-22836Comment by willmwade for <p>I agree, it must be a problem with <code>normal</code>. The following works as expected</p>
<pre>sage: f(x,y)= x*y^3
sage: f
(x, y) |--> x*y^3
sage: g = f.integral(y,0,7)
sage: g
(x, y) |--> 2401/4*x
sage: plot(g,-5,5)
</pre>
<p>Where as this returns the error you've seen:</p>
<pre><code>sage: f(x,y,z)= x*y^3+z
sage: g = f.integral(y,0,7)
sage: plot(g,-5,5)
...
ValueError: free variable: z
</code></pre>
<p>If you try to simply integrate the <code>normal</code> function, do you get a number, or another function of <code>x</code>?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22837#post-id-22837normal(x,av,sd)=(1/(sd*sqrt(2*pi)))*exp(-(x-av)^2/(2*sd^2))
If I integrate this alone at say the standard normal (x,0,1) or any other mean and standard deviation I get a number just like you would expect. It is just the normal distribution.
Thu, 26 Aug 2010 13:59:15 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22837#post-id-22837Answer by niles for <p>This should be a rather simple question, and I think I am just not understanding the error.</p>
<p>I have a function say <code>f(x,y)= x*y^3*normal(y,1,2)</code> where normal() is the normal pdf equation (x,mean,standard deviation).</p>
<p>So I want to integrate across the pdf and then plot x. What I thought I would do was:</p>
<pre><code>g = f.integral(y,0,Infinity)
plot(g,-10,10)
</code></pre>
<p>But that give the error: ValueError: free variable: x</p>
<p>What am I missing?</p>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?answer=11562#post-id-11562Thanks; I don't quite understand what's broken, but I think I have a workaround.
<pre>
sage: normal(x,av,sd)=((1/(sd*sqrt(2*pi)))*exp(-(x-av)^2/(2*sd^2)))
sage: f(x,y)= x*y^3*normal(y,1,2)
sage: g = f.integrate(y,-2,2)
sage: g
(x, y) |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)
</pre>
(note that sage seems to think `g` is a function of `x` and `y`, even though it shouldn't be)
<pre>
sage: h(x) = f.integrate(y,-2,2)
sage: h
x |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)
</pre>
(this is looking better, but still doesn't work . . . the problem seems to be the `integrate` command in the definition of `h`)
<pre>
sage: plot(h,0,5)
...
ValueError: free variable: y
</pre>
(the first way I could think of to get Sage to evaluate the integral was to use the `numerical_approx` method; maybe there's a better way, but this works :)
<pre>
sage: k = lambda x: h(x).numerical_approx()
sage: plot(k,0,5)
</pre>
(graph is shown!)
Note that, unfortunately, the following does not work:
k(x) = h(x).numerical_approx()
...
TypeError: cannot evaluate symbolic expression numerically
Unless someone knows better, I'm inclined to think this should be filed as a Trac ticket.
EDIT: after writing all this, I think the problem is the "symbolic expression" `integrate(...)`. The following works as expected:
sage: k(x) = 1/4*sqrt(2)*x*(integrate(y^3*e^(-1/8*(y - 1)^2), y, -2,2).numerical_approx())/sqrt(pi)
sage: plot(k,0,5)
Thu, 26 Aug 2010 14:44:16 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?answer=11562#post-id-11562Comment by kcrisman for <p>Thanks; I don't quite understand what's broken, but I think I have a workaround.</p>
<pre>sage: normal(x,av,sd)=((1/(sd*sqrt(2*pi)))*exp(-(x-av)^2/(2*sd^2)))
sage: f(x,y)= x*y^3*normal(y,1,2)
sage: g = f.integrate(y,-2,2)
sage: g
(x, y) |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)
</pre>
<p>(note that sage seems to think <code>g</code> is a function of <code>x</code> and <code>y</code>, even though it shouldn't be)</p>
<pre>sage: h(x) = f.integrate(y,-2,2)
sage: h
x |--> 1/4*sqrt(2)*x*integrate(y^3*e^(-1/8*(y - 1)^2), y, -2, 2)/sqrt(pi)
</pre>
<p>(this is looking better, but still doesn't work . . . the problem seems to be the <code>integrate</code> command in the definition of <code>h</code>)</p>
<pre>sage: plot(h,0,5)
...
ValueError: free variable: y
</pre>
<p>(the first way I could think of to get Sage to evaluate the integral was to use the <code>numerical_approx</code> method; maybe there's a better way, but this works :)</p>
<pre>sage: k = lambda x: h(x).numerical_approx()
sage: plot(k,0,5)
</pre>
<p>(graph is shown!)</p>
<p>Note that, unfortunately, the following does not work:</p>
<pre><code>k(x) = h(x).numerical_approx()
...
TypeError: cannot evaluate symbolic expression numerically
</code></pre>
<p>Unless someone knows better, I'm inclined to think this should be filed as a Trac ticket.</p>
<p>EDIT: after writing all this, I think the problem is the "symbolic expression" <code>integrate(...)</code>. The following works as expected:</p>
<pre><code>sage: k(x) = 1/4*sqrt(2)*x*(integrate(y^3*e^(-1/8*(y - 1)^2), y, -2,2).numerical_approx())/sqrt(pi)
sage: plot(k,0,5)
</code></pre>
https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22832#post-id-22832To some extent this is a design decision - what variables should an integral of a function have as variables? (Arguments is probably the better word.) For indefinite ones, and derivatives, we keep them, and so since this had a symbolic evaluation, it's still a function of both variables... hmmm...Thu, 26 Aug 2010 15:51:26 +0200https://ask.sagemath.org/question/7641/plot-error-a-free-variable/?comment=22832#post-id-22832