 This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi, I'm new to sage and hope that anybody can help me.

I tried to plot two functions X1 and X2 which both depend on several parameters (k, j0, j1, jMax, a0, aMax, a1, v0, v1, vMax, x). I chose a value for every variable except for vMax because I wantet to plot X1 and X2 as a function of vMax. So I typed

plot(X1(k=5, j0=2, j1=4, jMax=10, a0=5, aMax=20, a1=3, v0=10, v1=100, vMax=vMax, x=2000), (vMax, 0, 200))


and it worked just fine. I wanted to do exactely the same with funktion X2, but then I got an value error:

sage: plot(X2(k=5, j0=2, j1=4, jMax=10, a0=5, aMax=20, a1=3, v0=10, v1=100, vMax=vMax, x=2000), (vMax, 0, 200))
Traceback (click to the left of this block for traceback)
...


I don't know, why vMax is not found, as the function is definitely dependent of it. Moreover, with function X1 it just worked as expected!

Yeah, sure, I should add some more details.

I constructed X1 and X2 recursively as they are the way X, which is the integral of the acceleration a, which is the integral of the velocity v and so on. In the end, I get

X1(k, j0, j1, a0, aMax, a1, v0, v1, vMax, x) = -1/24*(j0 -
jMax)^4/k^3 + 1/6*(j0 - jMax)^3*j0/k^3 + 1/24*jMax^4/k^3 - 1/2*a0*(j0 -
jMax)^2/k^2 - 1/2*aMax*jMax^2/k^2 + (j0 - jMax)*v0/k - jMax*vMax*/k + x +
1/48*((2*a0 - 2*aMax + jMax^2/k - (j0^2 - jMax^2)/k)^3*k^2/jMax^2 +
24*(2*a0 - 2*aMax + jMax^2/k - (j0^2 - jMax^2)/k)*k^2*v0/jMax -
3*(2*a0*k^2 - (j0^2 - jMax^2)*k)*(2*a0 - 2*aMax + jMax^2/k - (j0^2 -
jMax^2)/k)^2/jMax^2 + 4*(2*j0^3 - 3*j0^2*jMax + jMax^3 - 6*(a0*j0 -
a0*jMax)*k)*(2*a0 - 2*aMax + jMax^2/k - (j0^2 - jMax^2)/k)/jMax)/k^2 -
1/48*((2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)^3*k^2/jMax^2 -
24*(2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)*k^2*vMax/jMax -
3*(jMax^2*k - 2*aMax*k^2)*(2*a1 - 2*aMax + jMax^2/k - (j1^2 -
jMax^2)/k)^2/jMax^2 + 4*(jMax^3 - 6*aMax*jMax*k)*(2*a1 - 2*aMax +
jMax^2/k - (j1^2 - jMax^2)/k)/jMax)/k^2 - 1/24*((j1 + jMax)^4*jMax/k -
4*(j1 + jMax)^3*jMax^2/k + 24*(j1 + jMax)*jMax*k*vMax + 6*(2*a1*jMax*k^2
- (j1^2*jMax - jMax^3)*k)*(j1 + jMax)^2/k^2 - (3*j1^4 - 6*j1^2*jMax^2 ...
edit retag close merge delete

1

You shoud provide more details on what you typed, in particular on how you constructed X1, X2.

Sort by » oldest newest most voted

The symbolic variable jMax is not defined. You can either define it separately:

sage: var('jMax')

sage: X2(k, j0, j1, a0, aMax, a1, v0, v1, vMax, x) = -1/24*jMax^4/k^3 +1/2*aMax*jMax^2/k^2 + jMax*vMax/k + 1/48*((2*a1 - 2*aMax + jMax^2/k -(j1^2 - jMax^2)/k)^3*k^2/jMax^2 - 24*(2*a1 - 2*aMax + jMax^2/k - (j1^2 -jMax^2)/k)*k^2*vMax/jMax - 3*(jMax^2*k - 2*aMax*k^2)*(2*a1 - 2*aMax +jMax^2/k - (j1^2 - jMax^2)/k)^2/jMax^2 + 4*(jMax^3 -6*aMax*jMax*k)*(2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)/jMax)/k^2+ 1/24*((j1 + jMax)^4*jMax/k - 4*(j1 + jMax)^3*jMax^2/k + 24*(j1 +jMax)*jMax*k*vMax + 6*(2*a1*jMax*k^2 - (j1^2*jMax - jMax^3)*k)*(j1 +jMax)^2/k^2 - (3*j1^4 - 6*j1^2*jMax^2 + 4*jMax^4 + 12*(a1^2 -aMax^2)*k^2 - 12*(a1*j1^2 - (a1 - aMax)*jMax^2)*k)*(j1 +jMax)/k)/(jMax*k^2)

sage: X2
(k, j0, j1, a0, aMax, a1, v0, v1, vMax, x) |--> -1/24*jMax^4/k^3 + 1/2*aMax*jMax^2/k^2 + jMax*vMax/k + 1/48*((2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)^3*k^2/jMax^2 - 24*(2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)*k^2*vMax/jMax - 3*(jMax^2*k - 2*aMax*k^2)*(2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)^2/jMax^2 + 4*(jMax^3 - 6*aMax*jMax*k)*(2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k)/jMax)/k^2 + 1/24*((j1 + jMax)^4*jMax/k - 4*(j1 + jMax)^3*jMax^2/k + 24*(j1 + jMax)*jMax*k*vMax + 6*(2*a1*jMax*k^2 - (j1^2*jMax - jMax^3)*k)*(j1 + jMax)^2/k^2 - (3*j1^4 - 6*j1^2*jMax^2 + 4*jMax^4 + 12*(a1^2 - aMax^2)*k^2 - 12*(a1*j1^2 - (a1 - aMax)*jMax^2)*k)*(j1 + jMax)/k)/(jMax*k^2)


Or add it as an internal variable of your function X2 (it will be automatically injected in the namespace):

sage: X2(k, j0, j1, jMax, a0, aMax, a1, v0, v1, vMax, x) = -1/24*jMax^4/k^3 +1/2*aMax*jMax^2/k^2 + jMax*vMax/k + 1/48*((2*a1 - 2*aMax + jMax^2/k -(j1^2 - jMax^2)/k)^3*k^2/jMax^2 - 24*(2*a1 - 2*aMax + jMax^2/k - (j1^2 -jMax^2)/k)*k^2*vMax/jMax - 3*(jMax^2*k - 2*aMax*k^2)*(2*a1 - 2*aMax +jMax^2/k - (j1^2 - jMax^2)/k)^2/jMax^2 + 4*(jMax^3 -6*aMax*jMax*k)*(2*a1 - 2*aMax + jMax^2/k - (j1^2 - jMax^2)/k ...
more

Okay, thanks! I thought I had defined jMax, but apperently not. Now it works! :)

I have exactely the same problem! Even if in my worksheet the variable x is defined before (like sage: x = var('x') ), the command to plot my function turns into that ValueError... :(

This error can occur when you use simultaneous assignments. This returns error.

@interact
def _(a=-3, b=3,c=(-10,10),f=y^2*sin(y^c)):
show(plot(f,(x,a,b)))


This does not:

@interact
def _(a=-3, b=3,c=(-10,10),f=y^2*sin(y^c)):
f=y^2*sin(y^c))
show(plot(f,(x,a,b)))

more