Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5215
7667789 #Probelem set 3 #Exercise 1. Make a CAS function angle_and_proj(u,v) that takes two list of length 2 each, makes the lists into 2D vectors and returns #• the angle between the vectors (in radians) #• the projection of the first vector on the direction of the second vector #Test your code on example of your choice. |#//Input:lists vec1 and vec2 of length 2 #//Output: ? vec=[2,-4];V=vector(vec);type(V) def angle_and_proj(vec1,vec2):#vec1 and u=vector(vec1) v=vector(vec2) l1=N(len(u);l2=N(len(v)) #norms of u and v temp=N(u.dot_product(v).n())# dot product <u,v> converted to the decimal form ####temp=N(?) angle=N(arccos(temp/N(l1*l2))) #Complete coding the formula for the angle b/w two vectors ####angel= N(arccos..) proj=N(temp/l2) # Complete coding the formula for projection of u on the direction of v return angel,proj #example a1,proj1=angle_and_proj([3,0],[-2,3])
7667789
*** WARNING: Code contains non-ascii characters *** Error in lines 2-2 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "<string>", line 1 |#//Input:lists vec1 and vec2 of length 2 ^ SyntaxError: invalid syntax
#Exercise 2(a). Make a CAS function pt_line_distance(a, b, c, pt) that takes coefficients #a, b, c of the general equation of a line and coordinates pt = [x0, y0] of a point and returns the distance from the point to the line. #(b) Make up an example and test your code using the example. Plot in one figure (i) the point, (ii) #the line, and (iii) a vector perpendicular to the line with its tail located at the given point. #//Imput: ? #//Output:distance from point to line defined by the equation ? def pt_line_distance(a,b,c,pt): if b==0: return abs(pt[0]+c/d) else: n=vector([a,b]).norm()# length of a normal vector to the given line dist=(abs(a*pt[0]+b*pt[0]+c))/n #Encode the distance formula return dist pt_line_distance(1,2.3,-1,[1,1.])
0.917070056253235
# Execise 2(b): Plotting #//Input:? #//Output:? def ex2_plot(a,b,c,pt): line_plot=plot((-a/b)*x-(c/b),pt[0]-3.,pt[0]+3.,figsize=[4,3])# the range for x can be chosen differently pt_plot=point(pt,size=30,color='green',figsize=[4,3]) n_plot=arrow((pt[0],pt[1]),(pt[0]+a,pt[1]+b)) G=line_plot+pt_plot+n_plot G.show() ex2_plot(1,2.3,-1,[1,1.])
#Exercise 3: Make a CAS function three_motions(pt, t, V, param) that takes coordinates pt of a point, angle t, in radians, vector V,and parameter p. Your function should return a plot of the given point and its image under the following transformations: #(a) translation defined by the vector V if p = 0; #(b) rotation defined by the angle t if p = 1; #(c) reflection of the point in the y-axis if p = 2; #Test you code for specific transformations of each of the three kinds of motions. # Make this function w/out my template
def rigid_motion(pt,t,v,p): if p==0: pt_image=(pt[0]+v[0],pt[1]+v[1]) elif p==1: a=cos(t);b=sin(t) pt_image=(a*pt[0]+b*pt[1],-b*pt[1]+a*pt[0] else: pt_image=(-pt[0],pt[1]) return pt_image rigid_motion([1,3],.6,vector([7,9]),0)
Error in lines 1-9 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags), namespace, locals) File "<string>", line 7 else: ^ SyntaxError: invalid syntax
#exercise 4: show that if matrix A a=1; b=1 #matrix addition A_z = matrix([[a,b],[-b,a]]) c=2; d=2 #matrix multiplication A_w = matrix([[c,d],[-d,c]]) print(A_z+A_w) print(b+d) print(A_z*A_w) print(b*d)
[ 3 3] [-3 3] 3 [ 0 4] [-4 0] 2
#exercise 5 #exercise 6: in the email ##Exercise 7 (demo). Use CAS to substitute the expressions x = X*cos(t)-Y*sin(t), y = X*sin(t)+Y*cos(t) into the equation A*x^2+2*B*x*y+C*y^2=1. Find the coefficient of X*Y. var('A B C x y t X Y') expr=A*x^2+2*B*x*y+C*y^2
(A, B, C, x, y, t, X, Y)
expr1=expr.subs(x = X*cos(t)-Y*sin(t), y = X*sin(t)+Y*cos(t))
expr2=expand(expr1);expr2
A*X^2*cos(t)^2 + 2*B*X*Y*cos(t)^2 + C*Y^2*cos(t)^2 + 2*B*X^2*cos(t)*sin(t) - 2*A*X*Y*cos(t)*sin(t) + 2*C*X*Y*cos(t)*sin(t) - 2*B*Y^2*cos(t)*sin(t) + C*X^2*sin(t)^2 - 2*B*X*Y*sin(t)^2 + A*Y^2*sin(t)^2
expr3=(expr2.coefficient(X,n=1))/2;expr3
B*Y*cos(t)^2 - A*Y*cos(t)*sin(t) + C*Y*cos(t)*sin(t) - B*Y*sin(t)^2
expr3.coefficient(Y,n=1)
B*cos(t)^2 - A*cos(t)*sin(t) + C*cos(t)*sin(t) - B*sin(t)^2
#exercise 8: in the email sD