Untitled

1118 days ago by pub

Wikipages: Functions, Functions Math, Functions Programming

Functions Math are built-in cos(x) or user-defined f(x)=3x^2 or r(t)=vector((sin(t),cos(t),t))

Functions Programming are subroutines or procedures that you can use to process your data repeatedly.

Some people consider commands to be functions; I do not. That is, I consider vector() to be a command and it is listed and explained there.

Functions Programming

#Here is a Programming Function. It requires two parameters from the calling code and returns one. #It is using two built-in math functions; random() and int(x) def random_between(j,k): a=int(random()*(k-j+1))+j return a
#Here is a Function for Programming. It requires two parameters from the calling code and returns two. def count_if(list1,p): list_2=[x for x in list1 if x<=p] c=len(list_2) return list_2, c

Calling/Using Programming Functions

#This code calls the programming function random_between. #It should give you different answers every time you evaluate. p=random_between(1,100) print p
 82 82
#This code calls the programming function random_between 10 times and creates a list. #Then it calls the programming function count_if. m=1; n=100; p=74 list1=[ random_between(m,n) for j in range(10) ] print list1 list2,c=count_if(list1,p) print list2 print c
 [84, 76, 68, 76, 52, 36, 100, 44, 5, 99] [68, 52, 36, 44, 5] 5 [84, 76, 68, 76, 52, 36, 100, 44, 5, 99] [68, 52, 36, 44, 5] 5

Math Functions

#Remember: to define (or use) a math function, you must first declare your variables. #Here we define a explicit (regular) function f(x) with one independent variable x and then evaluate it at x=4. var('x') f(x)=3*x*sqrt(x) a=f(4) print a
 24 24
#Look carefully at this. x is the variable in the function. w is a "variable name". So we need to declare both. #If w has been valued in code above, then we do not need to declare w and of course its value would be used to calculate a. var('x w') f(x)=w*x*sqrt(x) a=f(4) print a
 8*w 8*w
#Remember: to define (or use) a math function, you must first declare your variables. #Of course you do not need to declare a variable more than once in your whole program. We are doing it here so that you can copy code directly. #Here we define a explicit (regular) function f(x) with one independent variable x and make a list of x-values and another list of the corresponding y=f(x) values. var('x') f(x)=x^2*cos(x) x_s=[j/2 for j in range(11)] y_s=[f(x_s[j]) for j in range (11)] print x_s print y_s
 [0, 1/2, 1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5] [0, 1/4*cos(1/2), cos(1), 9/4*cos(3/2), 4*cos(2), 25/4*cos(5/2), 9*cos(3), 49/4*cos(7/2), 16*cos(4), 81/4*cos(9/2), 25*cos(5)] [0, 1/2, 1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5] [0, 1/4*cos(1/2), cos(1), 9/4*cos(3/2), 4*cos(2), 25/4*cos(5/2), 9*cos(3), 49/4*cos(7/2), 16*cos(4), 81/4*cos(9/2), 25*cos(5)]
#Here we plot the points using the commands point(), sum() and show() pts=[point((x_s[j],y_s[j]), size=20) for j in range(11)] show(sum(pts),figsize=4)
#When you plot a function, you still must declare your variables. var('x') plot(x^2*cos(x),(x,0,5), figsize=4)
#Here we are plotting an implicit function in 3d so we need to declare x, y and z. #Click and drag in the image to rotate or right-click for options. #Change the mins and maxs in the ranges to get the whole elliptical sphere. var('x y z') vs=-2;vb=2 implicit_plot3d(x^2+y^2/4+z^2/9==1,(x,vs,vb), (y,vs,vb), (z,vs,vb), figsize=4, opacity=.5)
#Important: Notice that you just write r and NOT r(t)! Notice the double parenthesis around for the vector. #This vector-parametric function has two components so is 2d. var ('t') r=vector((cos(t),sin(t))) #Here we split the plot into 2 pieces so we could see the direction of the curve. C1=parametric_plot(r,(t,0,pi), figsize=4, color="hotpink") C2=parametric_plot(r,(t,pi,2*pi), figsize=4, color=(0,.5,0)) show(C1+C2)
#Important: Notice that you just write r and NOT r(t)! Notice the double parenthesis around for the vector. #This vector-parametric function has three components so is 3d. var('t') r=vector((cos(t),sin(t),t)) parametric_plot(r,(t,0,2*pi), thickness=5, color="green")
#Here we make the elements of our lists into decimals. The command float() only works on a number. x_sf=[float(x_s[j]) for j in range(11)] y_sf=[float(y_s[j]) for j in range(11)] print x_sf print y_sf
 [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] [0.0, 0.21939564047259319, 0.54030230586813977, 0.15915870375233154, -1.6645873461885696, -5.0071475971683359, -8.9099324694040085, -11.471594419312256, -10.458297933817791, -4.2686149384732888, 7.0915546365806561] [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] [0.0, 0.21939564047259319, 0.54030230586813977, 0.15915870375233154, -1.6645873461885696, -5.0071475971683359, -8.9099324694040085, -11.471594419312256, -10.458297933817791, -4.2686149384732888, 7.0915546365806561]
#Don't forget that all code within function must be indented including return def random_between_bad(j,k): a=int(random()*(k-j+1))+j return a
 Traceback (click to the left of this block for traceback) ... SyntaxError: 'return' outside function Traceback (most recent call last): File "", line 1, in File "_sage_input_16.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("I0Rvbid0IGZvcmdldCB0aGF0IGFsbCBjb2RlIHdpdGhpbiBmdW5jdGlvbiBtdXN0IGJlIGluZGVudGVkIGluY2x1ZGluZyByZXR1cm4KZGVmIHJhbmRvbV9iZXR3ZWVuX2JhZChqLGspOgogICAgYT1pbnQocmFuZG9tKCkqKGstaisxKSkragpyZXR1cm4gYQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in File "/tmp/tmp31G7q8/___code___.py", line 5 return a SyntaxError: 'return' outside function