| Download
All published worksheets from http://sagenb.org
Project: sagenb.org published worksheets
Views: 168731Image: 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
Multiplying two vectors automatically does the dot product in Sage.
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
(1, 2, 3)
(-1, 0, 5)
(0, 2, 8)
(-2, -2, 2)
sqrt(14)
3.74165738677394
sqrt(26)
2*sqrt(17)
2*sqrt(3)
14
14
14
arccos(1/26*sqrt(14)*sqrt(26))
0.746898593069036
180*arccos(1/26*sqrt(14)*sqrt(26))/pi
42.7941371071149
[1]
[2]
[3]