Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a function named points2vector which outputs a 3D Cartesian vector giv

ID: 3722865 • Letter: 1

Question

1. Create a function named points2vector which outputs a 3D Cartesian vector given two 3D Cartesian coordinates. The function should:

• input: 2 variables of size (1,3) defining the points

• output: 1 variable of size (1,3) defining the Cartesian vector

2. Create a function named vectorMagnitude which inputs a 3D Cartesian vector and determines its magnitude:

• input: 1 variable of size (1,3) defining the Cartesian vector

• output: 1 variable of size (1,1) defining the vector magnitude

3. Create a function named vector2unit which inputs a 3D Cartesian vector and converts it into a unit vector. The function should:

• input: 1 variable of size (1,3) defining the Cartesian vector

• output: 1 variable of size (1,3) defining the Cartesian unit vector

• must use the function vectorMagnitude

4. Create a function named points2unit which converts two 3D points into a 3D Cartesian unit vector. The function should:

• input: 2 variables of size (1,3) defining the coordinate points

• output: 1 variable of size (1,3) defining the Cartesian unit vector

• must use the functions point2vector and vector2unit

5. Create a program which tests the function points2unit, using a “for” loop. Complete the following chart:

The program must

• use the points2unit function You cannot use the norm() or sum() functions. In addition, none of the functions should use the input() function.

Case p_head p_tail unit_vector 1 (5,1,3) (9,0,0) 2 (2,5,7) (4,-2,3) 3 (8,6, 3) (5,5,3) 4 (9,-1,-1) (6,-8,3) 5 (3,6,2) (8,1,7) 5 (1,1,3) (5,4,3)

Explanation / Answer

Here are the codes functions that you require -

function [v] = points2vector(p1, p2)

% Given two cartesian coordinates outputs a cartesian vector joining

% them

v = p2 - p1;

end

function [n] = vectorMagnitude(v)

% Given a vector, outputs its magnitude

n = sqrt(v(1).^2 + v(2).^2 + v(3).^2);

end

function [u] = vector2unit(v)

% Given a vector, outputs corresponding unit vector

u = v ./ vectorMagnitude(v);

end

function [u] = points2unit(p1, p2)

% Given two cartesian coordinate, outputs a unit vector in the direction of

% the vector joining them

v = points2vector(p1, p2);

u = vector2unit(v);

end

------

Here is the script to use them for the question -

p_head = [5,1,3; 2,5,7; 8,6,sqrt(3); 9,-1,1; 3,6,2; 1,1,3];

p_tail = [9,0,0; 4,-2,3; 5,5,3; 6,-8,3; 8,1,7; 5,4,3];

for i = 1:6

unit_vector = points2unit(p_head(i,:), p_tail(i,:));

disp(unit_vector);

end

--------

Here are the unit vectors that I got -

Case 1 - (0.7845 -0.1961 -0.5883)

Case 2 - (0.2408 -0.8427 -0.4815)

Case 3- (-0.8805 -0.2935 0.3722)

Case 4 - (-0.3810 -0.8890 0.2540)

Case 5 - (0.5774 -0.5774 0.5774)

Case 6 - (0.8000 0.6000 0)