Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 709
var("x,y") plot_vector_field((sin(x),cos(y)),(x,-3,3),(y,-3,3))
(x, y)
#1 Mathematical functions: two ways to define def mathfunc(x): return 3*x^2
#Parameters and symbolic variables f(x) = 3*x^2
f(3)
27
# define a function of x to the power of 19 g(x) = x^19 g(2)
524288
#2 Symbolic variables
type(x)
<type 'sage.symbolic.expression.Expression'>
# x is the default 'symbolic variable' # Make other sybolic variables? var("a,b,c")
(a, b, c)
type(b)
<type 'sage.symbolic.expression.Expression'>
b = 7
type(b)
<type 'sage.symbolic.expression.Expression'>
#2 Making lists of integers 1 to 5 #syntax: srange(lower, upper, stepsize) srange(5) #default argument values
[0, 1, 2, 3, 4]
#Practice: Making a list with even numbers starting from 2 and ends at 50, inclusive.
#hint: srange(lower, last value to include +b, stepsize), where 0<b<=stepsize srange(2, 53,2)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52]
srange(5)
[0, 1, 2, 3, 4]
#Iterations #use the list as counter or index f(x) = 3*x^2 val = 3 for i in srange(5): val = f(val) val
1144561273430837494885949696427
f(x) = 3*x^2 vals = [1,3,5,7,9] newvals = [] for i in srange(5): newvals.append(f(vals[i])) newvals
[3, 27, 75, 147, 243]
# Exercise : apply a function g(x) = 6*x for 100 times, initial val 9
#Store intermediate values in a list named vals? Hint: the function is always applied on the last element in list
#example 4 f(x) = 2*x #Define the function we want to iterate #Create list of numbers defining how many times to iterate count = srange(0,10) #Create a list to hold the results. The first value is 3. vals = [3] for i in count: a = vals[i] #Grab the last item in the results list b = f(a) # Plug a into the function. Put the output in b. vals.append(b) # Put b into the list of results. list_plot(vals) # Plot the results
def iterate(iter,j): f(x) = 2*x #Define the function we want to iterate #Create list of numbers defining how many times to iterate count = srange(iter) #Create a list to hold the results. The first value is 3. vals = [3] for i in count: a = vals[i] #Grab the last item in the results list b = j(a) # Plug a into the function. Put the output in b. vals.append(b) # Put b into the list of results. return vals # Plot the results g(x) = 3*x list_plot(iterate(5,g))
f(x) = 2*x #Define the function we want to iterate #Create list of numbers defining how many times to iterate count = srange(0,10) #Create a list to hold the results. The first value is 3. vals = [3] for i in count: a = vals[i] #Grab the last item in the results list b = f(a) # Plug a into the function. Put the output in b. vals.append(b) # Put b into the list of results. list_plot(vals) # Plot the results
def my_func(iter, init, g): #f(x) = 2*x vals = [init] count = srange(0,iter) for i in count: a = vals[i] #Grab the last item in the results list b = g(a) # Plug a into the function. Put the output in b. vals.append(b) # Put b into the list of results. return(vals) f(x) = 2*x list_plot(my_func(10, 5,f))
my_func()
︠46242af9-542a-443c-abfa-fa53daf40a1a︠ # (1) simplify? # (2) wrap in function? #inputs... #~0 no input #~1 iteration no. #~2 init #~3 function ︠d11286a4-2553-4437-bd2e-1c9fed4cb77e︠