Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168728
Image: ubuntu2004
# Create a bunch of variables with the same prefix # Example: # makevars('x',3) # creates variables x1, x2, x3 in the global namespace. # Return value: a tuple containing the variables. def makevars(prefix, n): return var(' '.join([prefix+str(i) for i in range(n)]))
x = makevars('x',5); x
(x0, x1, x2, x3, x4)
# Defining an expression: fexpr = sum(sin(v)^2 for v in x); fexpr
sin(x4)^2 + sin(x3)^2 + sin(x2)^2 + sin(x1)^2 + sin(x0)^2
# Defining a function f(tuple(x)) = sum( sum(sin(v)-cos(w) for w in x if w!=v) for v in x); f
(x0, x1, x2, x3, x4) |--> 4*sin(x4) - 4*cos(x4) + 4*sin(x3) - 4*cos(x3) + 4*sin(x2) - 4*cos(x2) + 4*sin(x1) - 4*cos(x1) + 4*sin(x0) - 4*cos(x0)
# Computing partial derivatives, for example: diff(fexpr,x1)
2*cos(x1)*sin(x1)
# Some computations with the function print f(0,0,0,0,x4) print diff(f(2,-1,x2,4,x4),x4) plot (f(2,-1,3,x3,0),-2*pi,2*pi)
4 sin(x4) - 4 cos(x4) - 16 4 sin(x4) + 4 cos(x4)