Problem 1.13, PHY 240

Nasser Abbasi , Feb 2, 2002.

 

The source code

 

% nma_orthog - Program that accepts a pair of three

% dimensional vectors and outputs a unit vector that is

% orthogonal to the input vectors

 

% HW3, 1.13, PHY 240, San Jose State Univ. Feb 2, 2002.

 

clear all; help nma_orthog;

 

%

% My review notes:

% on dot product:

%

% The dot product between 2 vectors A, B is defined as

% |A| |B| cos PHI, where PHI is the angle between the

% vectors, and |X| means the length of the vector X.

%

% since COS 90 is zero, then if the dot product is zero, then

% this means the vectors are orthogonal. (assuming the

% length of the vector is both not zero).

%

% dot product for vectors A and B, assuming x,y,z

% coordinates is

%

% A(x) B(x) + A(y) B(y) + A(z) B(z)

%

% on CROSS product:

% the CROSS product of two vectors, is a vector that is

% orthogonal the plane of other two vectors, and whose

% length is the length of the two vectors multiplied.

% in geometry, the CROSS product is seen as:

%

% A x B = |A| |B| sin(PHI)

%

% where PHI is the angle between the two vectors.

%

% CROSS product is also defined as:

%

% A x B = ( a1 i + a2 j + a3 k) x  (b1 i + b2 j + b3 k)

% where a1, a2, a3 are the x,y,z coordinates for A, and

% similarly b1,b2,b3 for B.

%

% Using these rules: (for right handed rule):

%

%  i x j = k

%  j x k = i

%  k x i = j

%  and

%  j x i = -k

%  k x j = -i

%  ix k = -j

%

% Using these, we find:

%

%  A x B = (a2b3 - a3b2)i + (a3b1 - a1b3)j + (a1b2 - a2b1)k

%

% in determinant form, cross product between A and B is

%

%   | i    j    k|       

%   | a1  a2   a3| 

%   | b1  b2   b3|

%

%

% So, I'll use the above equation to find AxB.

% finally, to obtain the UNIT vector, we divid by the

% length of the vector, i.e. 

%

%  C = A X B

%  Unit vector in direction of C is  C / |C|

%

% Where the length of C is SQRT( c1^2 + c2^2 + c3^3)

% where c1,c2,c3 are the coordinates of the vector C.

%

 

%* Initialize the vectors A and B

 

% Notice, No checking done on the input by the user, assumes

% valid input for a vector.

%

 

A = input('Enter the first vector A: ');

B = input('Enter the second vector B: ');

 

% Now apply the cross product rule

C(1)= A(2)*B(3) - A(3)*B(2);

C(2)= A(3)*B(1) - A(1)*B(3);

C(3)= A(1)*B(2) - A(2)*B(1);

 

S=sprintf('\nCross product vector C=AxB is [%g %g %g]\n', C(1),C(2),C(3) );

disp(S);

 

if( C(1)==0 & C(2)==0 & C(3) == 0 )   

    disp('Vectors A and B are parallel, cross product vector C is zero, please try again');

else   

    length = sqrt( C(1)^2 + C(2)^2 + C(3)^2 );

    C = C ./ length;

    S=sprintf('Length of C is %g, Unit vector orthogonal to plane of A and B is [%g %g %g]\n', length, C(1),C(2),C(3) );

    disp(S);

end

 

%

%

 

 

 

Example runs

 

Example 1

 

» nma_orthog

 

  nma_orthog - Program that accepts a pair of three

  dimensional vectors and outputs a unit vector that is

  orthogonal to the input vectors

 

Enter the first vector A: [1 0 0]

Enter the second vector B: [4 0 0]

 

Cross product vector C=AxB is [0 0 0]

 

Vectors A and B are parallel, cross product vector C is zero, please try again

 

 

 

Example 2

 

» nma_orthog

 

  nma_orthog - Program that accepts a pair of three

  dimensional vectors and outputs a unit vector that is

  orthogonal to the input vectors

 

Enter the first vector A: [1 1 0]

Enter the second vector B: [0 0 1]

 

Cross product vector C=AxB is [1 -1 0]

 

Length of C is 1.41421, Unit vector orthogonal to plane of A and B is [0.707107  -0.707107  0]

 

 

Example 3

 

»

»

» nma_orthog

 

  nma_orthog - Program that accepts a pair of three

  dimensional vectors and outputs a unit vector that is

  orthogonal to the input vectors

 

Enter the first vector A: [1 1 1]

Enter the second vector B: [1 -2 1]

 

Cross product vector C=AxB is [3 0 -3]

 

Length of C is 4.24264, Unit vector orthogonal to plane of A and B is [0.707107  0  -0.707107]

 

 

Example 4

 

»

» nma_orthog

 

  nma_orthog - Program that accepts a pair of three

  dimensional vectors and outputs a unit vector that is

  orthogonal to the input vectors

 

Enter the first vector A: [1 2 3]

Enter the second vector B: [3 2 1]

 

Cross product vector C=AxB is [-4 8 -4]

 

Length of C is 9.79796, Unit vector orthogonal to plane of A and B is [-0.408248  0.816497  -0.408248]

 

»