Ask Your Question
0

Variable not found

asked 2012-05-27 20:36:57 -0500

dnizetic gravatar image

updated 2014-11-10 09:45:23 -0500

tmonteil gravatar image

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");
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2012-05-27 20:51:26 -0500

benjaminfjones gravatar image

This 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)
edit flag offensive delete link more
0

answered 2012-05-27 20:42:35 -0500

updated 2012-05-27 20:53:03 -0500

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-05-27 20:36:57 -0500

Seen: 366 times

Last updated: May 27 '12