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.Mon, 28 May 2012 03:51:26 +0200Variable not foundhttps://ask.sagemath.org/question/9009/variable-not-found/Hi, why does this snippet produces "variable 'a' not found"?
I looked all over and I just can't figure it out.
var('a');
var('x y z');
x = cos(a)^3;
y = sin(a)^3;
z = cos(2*a);
pl1=parametric_plot3d( (cos(t)^3, sin(t)^3, cos(2*t)), (t, 0, 2*pi));
a = pi/4;
pl2=parametric_plot3d( (x + t*(derivative(x)), y + t * (derivative(y)), z + t * (derivative(z))), (t,0,1), texture="red");Mon, 28 May 2012 03:36:57 +0200https://ask.sagemath.org/question/9009/variable-not-found/Answer by benjaminfjones for <p>Hi, why does this snippet produces "variable 'a' not found"?</p>
<p>I looked all over and I just can't figure it out.</p>
<pre><code>var('a');
var('x y z');
x = cos(a)^3;
y = sin(a)^3;
z = cos(2*a);
pl1=parametric_plot3d( (cos(t)^3, sin(t)^3, cos(2*t)), (t, 0, 2*pi));
a = pi/4;
pl2=parametric_plot3d( (x + t*(derivative(x)), y + t * (derivative(y)), z + t * (derivative(z))), (t,0,1), texture="red");
</code></pre>
https://ask.sagemath.org/question/9009/variable-not-found/?answer=13624#post-id-13624This happens because of the way python handles variables, assignment, and references. Try the following:
sage: var("x,a")
(x, a)
sage: x = cos(a)
sage: a = 1/4
sage: print x
cos(a)
sage: print(a)
1/4
In this code, `a` at first refers to a symbolic variable, then the name `a` is reassigned to the number 1/4. When x gets printed you still see the symbolic variable there whose name is `a`.
To get a value for `a` substituted into `x, y, z` try using the .subs method:
sage: var('a')
a
sage: x = cos(a)
sage: x.subs(a=1/4)
cos(1/4)
Mon, 28 May 2012 03:51:26 +0200https://ask.sagemath.org/question/9009/variable-not-found/?answer=13624#post-id-13624Answer by Dima for <p>Hi, why does this snippet produces "variable 'a' not found"?</p>
<p>I looked all over and I just can't figure it out.</p>
<pre><code>var('a');
var('x y z');
x = cos(a)^3;
y = sin(a)^3;
z = cos(2*a);
pl1=parametric_plot3d( (cos(t)^3, sin(t)^3, cos(2*t)), (t, 0, 2*pi));
a = pi/4;
pl2=parametric_plot3d( (x + t*(derivative(x)), y + t * (derivative(y)), z + t * (derivative(z))), (t,0,1), texture="red");
</code></pre>
https://ask.sagemath.org/question/9009/variable-not-found/?answer=13623#post-id-13623you forgot to do
var('t')
You also need to re-so assignments of x, y, and z after you set a to a particular value. I.e.
a = pi/4;
x = cos(a)^3;
y = sin(a)^3;
z = cos(2*a);
pl2=parametric_plot3d( (x + t*(derivative(x)), y + t * (derivative(y)), z + t * (derivative(z))), (t,0,1), texture="red");
works just fine.
Mon, 28 May 2012 03:42:35 +0200https://ask.sagemath.org/question/9009/variable-not-found/?answer=13623#post-id-13623