Home > Commands -> var


EXAMPLES ON MY SAGE PAGE - Variable Names vs. Object Names


  • Variables are names that have not been assigned a value.
    • Objects names are names that have been assigned a value.
  • Variables must be declared using the command var (see x in example 1).
    • Object names do not have to be declared (see h in example 1)

YouTube Video - pdf

Syntax: var('x') or var('theta rho z') where x or theta, rho and z are (sample) names that will be used to define function arguments or constants.

     After declaring a variable name, the name can be used to define a function.


Example 1 - Create a function $f(x)=x^2+3x-2$ and an object h with value 1. Create/print a list of values of f(x) for x=0 to 5 step h.
var('x')
f(x)=x^2+3*x-2
h=1.
list_f=[f(j) for j in [0..5,step=h] ]
print list_f
Result:
[-2.00000000000000, 2.00000000000000, 8.00000000000000, 16.0000000000000, 26.0000000000000, 38.0000000000000]

Example 2
#Look carefully at this. x is the variable in the function. w is just a "variable name". We need to declare both.
#Here we define a explicit (regular) function f(x) with one independent variable x, a constant w and then evaluate it at x=4.
var('x w')
f(x)=w*x*sqrt(x)
a=f(4)
print a
Result:
8*w

Keywords: variable, object, name, object name, declare