# 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?

edit retag close merge delete

Is this a case of not declaring symbolic variables with var()?

( 2010-08-26 13:12:34 +0200 )edit

ValueError 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.

( 2010-08-26 13:14:14 +0200 )edit

I'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?

( 2010-08-26 13:28:14 +0200 )edit

See below.

( 2010-08-26 14:13:35 +0200 )edit

Sort by » oldest newest most voted

Thanks; I don't quite understand what's broken, but I think I have a workaround.

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)


(note that sage seems to think g is a function of x and y, even though it shouldn't be)

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)


(this is looking better, but still doesn't work . . . the problem seems to be the integrate command in the definition of h)

sage: plot(h,0,5)
...
ValueError: free variable: y


(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 :)

sage: k = lambda x: h(x).numerical_approx()
sage: plot(k,0,5)


(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)

more

To 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...

( 2010-08-26 15:51:26 +0200 )edit

I agree, it must be a problem with normal. The following works as expected

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)


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?

more

normal(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.

( 2010-08-26 13:59:15 +0200 )edit

I 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 1

( 2010-08-26 14:11:58 +0200 )edit

Oh and before this I did run var('x d') so both of those are declared.

( 2010-08-26 14:13:18 +0200 )edit