Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 5216
reset
<function reset at 0x7f80c5c78050>
#Useful commands #Dot product var('a b c d'); u=vector((a,b)); v=vector((c,d))# Syntax for defining vectors u, v u.dot_product(v)# syntax for the dot product <u,v>
(a, b, c, d) a*c + b*d
norm(vector((3,4)))# magnitude,or length, of the vector
5
#Alternatively,the norm of a vector v can be found "from scratch" as (v.dot_product(v))^0.5
arrow((3,4),(3+2,4-1),figsize=[3,2])# Syntax for plotting a vector
search_doc("normal") norm
Use this link to search: normal
<function norm at 0x7fa59fc20668>
#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: ? vec1=[1,5];V=vector(vec1);type(V) def angle_and_proj(vec1,vec2):#vec1 and u=vector(vec1) v=vector(vec2) c=N(len(u));l2=N(len(v)) #norms of u and v temp=N(u.dot_product(v))# dot product <u,v> converted to the decimal form angle=N(u.dot_product(v)) #Complete coding the formula for the angle b/w two vectors projection=N(temp/12) # Complete coding the formula for projection of u on the direction of v return(angle,projection)
<type 'sage.modules.vector_integer_dense.Vector_integer_dense'>
#Example: angle, projection=angle_and_proj([3,0],[-3,5]);angle
-9.00000000000000
#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 bas(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 three_motions(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 three_motions([3,5],.8,vector([3,4]),0)
(6, 9)
#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 x = X*cos(t)-Y*sin(t) y = X*sin(t)+Y*cos(t)
(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
#Manual solution of the equation (A-C)*sin(2*t)=2*B*cos(2*t): cot(2*t)=(A-C)/2*B.