Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168731
Image: ubuntu2004

Here we show how to do the following octave/matlab code in Sage:

%initialize 2 vectors in R3 
a=[1 2 3]
b=[-1 0 5]

%vector sums
c=a+b
d=b-a

%vector magnitudes
lenA = norm(a)
lenB = norm(b)
lenC = norm(c)
lenD = norm(d)

%scalar dot product
e=a*b'
f=sum(a.*b)
g=dot(a,b)
theta1 = acos(e/lenA/lenB)
theta2 = theta1*180/pi
a=vector([1,2,3]) b=vector([-1,0,5]) c=a+b d=b-a lenA=a.norm() lenB=norm(b) lenC=norm(c) lenD=norm(d)

Multiplying two vectors automatically does the dot product in Sage.

e=a*b f=sum(ai*bi for ai,bi in zip(a,b)) g=a.dot_product(b)
theta1=arccos(e/lenA/lenB) theta2=theta1*180/pi

The output is:

SAMPLE OUTPUT Octave file (vector1.txt): 
a =

  1   2   3

b =

 -1   0   5

c =

  0   2   8

d =

 -2  -2   2

lenA =  3.7417
lenB =  5.0990
lenC =  8.2462
lenD =  3.4641
e =  14
f =  14
g =  14
theta1 =  0.74690
theta2 =  42.794
a
(1, 2, 3)
b
(-1, 0, 5)
c
(0, 2, 8)
d
(-2, -2, 2)
lenA
sqrt(14)
lenA.n()
3.74165738677394
lenB
sqrt(26)
lenC
2*sqrt(17)
lenD
2*sqrt(3)
e
14
f
14
g
14
theta1
arccos(1/26*sqrt(14)*sqrt(26))
theta1.n()
0.746898593069036
theta2
180*arccos(1/26*sqrt(14)*sqrt(26))/pi
theta2.n()
42.7941371071149
(a.plot(rgbcolor='red')+b.plot(rgbcolor='blue')+c.plot(rgbcolor='green')+d.plot(rgbcolor='orange')).show(aspect_ratio=1)
h=a.transpose();h
[1] [2] [3]