MATLAB exercise for average of dot products of unit vectors not matching textbook's calculus result.

50 Views Asked by At

I'm working through Strang's Introduction to Linear Algebra. Problem Set 1.2, #34 states,

Using $v=randn(3,1)$ in MATLAB, create a random unit vector $u=\frac{v}{||v||}$. Using $V=randn(3,30)$ create 30 more random unit vectors $U_j$. What is the average size of the dot products $|u\cdot U_j|$? In calculus, the average is $\int_0^\pi |\cos\theta| d\theta / \pi = 2/\pi$.

Here's my program:

v=randn(3,1)
u=v/norm(v)
sum=0
k=30
for i=1:k
    V=randn(3,1)
    U=V/norm(V)
    dot_product=abs(dot(u,U));
    sum+=dot_product;
end
sum/k

I am getting an average near $1/2$, not $2/\pi$. Same tendency as I increase k tenfold or hundredfold.

Sample output of my program below. The unit vectors look correct. Is there something I'm misunderstanding?

v =

  -0.9049
   0.4694
  -1.0034

u =

  -0.6326
   0.3281
  -0.7015

sum = 0
k = 30
V =

   0.443107
   0.064042
   0.274244

U =

   0.8440
   0.1220
   0.5223

...
...
...

V =

  -0.6647
   0.5873
  -0.9252

U =

  -0.5186
   0.4582
  -0.7219

ans = 0.5125
1

There are 1 best solutions below

1
On BEST ANSWER

The integral looks very much like the 2D version where the angle between the two vectors is uniformly distributed between $0$ and $\pi$. Indeed if you do the Matlab exercise with "rand(2,1)" you get close to $2/\pi$.

Looks like an error in the book.