MATLAB The area of a parallelogram in R 2 built on vectors

MATLAB

The area of a parallelogram in R 2 , built on vectors v 1 , v 2 , and the volume of a parallelepiped
in R 3 , built on vectors v 1 , v 2 , v 3 , are det A (or abs(det A)), where A = [ v 1 v 2 ] and
A = [ v 1 v 2 v 3 ] , respectively.


You will be given 3 points in R 2 - three vertices of a parallelogram – the 4 th vertex is defined
by the 3 given (or 4 points will be given in R 3 ). The input points, represented by their
position vectors, will be the columns of a 2 × 3 matrix B = [ x 1 x 2 x 3 ] (or a 3 × 4 matrix
B = [ x 1 x 2 x 3 x 4 ] ).


**Create a function in MATLAB:
function D = arevol(B)
that would calculate 2 vectors in R 2 : v 1 , v 2 (or 3 vectors in R 3 : v 1 , v 2 , v 3 ) on which a
parallelogram (or parallelepiped) is built - you should subtract vector x 1 from each other
vector.


For example, in R 2 , if B = [ x 1 x 2 x 3 ] , then v =x 2 x 1 , v =x 3 x 1 (obviously, the 4 th
1 2 vertex x 4 = x 1 + v 1 + v 2 but we do not use it!). And matrix A = [ v 1 v 2 ] .
Output the 2 × 2 matrix A = [ v 1 v 2 ] (or a 3 × 3 matrix A = [ v 1 v 2 v 3 ] ).

Calculate the area D of the parallelogram (or the volume D of the parallelepiped) built on the
vectors formed by the columns of A.


Important Note: It might happen that the area (volume) represented by D is supposed to be a
zero but, due to round off error it doesn’t show as a zero but very small positive number. In
this course we will consider the numbers or, in general, entries of a matrix, whose absolute
values are less than 10 7 to be zeros. In order to archive it, you will need to create, type in the
diary file, and put in your code for arevol the following function (this function will be used
later a lot!):


function B=closetozeroroundoff(A)
[m,n]=size(A);
for i=1:m
for j=1:n
if abs(A(i,j))<10^(-7)
A(i,j) = 0;
end
6end
end
B=A;
In your code for arevol, after you calculated
D=abs(det(A));
put
D=closetozeroroundoff(D);


and then proceed with the conditional statements that are indicated below. This will ensure
that D=0 shows as D=0, not as a number 2.66543210000076e-14, for example.
If D = 0, the function outputs one of the messages:


“The points lie on the same line and no parallelogram can be built”


or


“The points lie in the same plane and no parallelepiped can be built”


If the determinant is not zero, the function displays one of the messages:


“The area of the parallelogram is” and outputs D
or
“The volume of the parallelepiped is” and outputs D
depending on which of the above you have calculated.

Solution

function D = arevol(B)

[m,n]=size(B);
if m==2
v1=B(:,2)-B(:,1);
v2=B(:,3)-B(:,1);
A=[v1,v2];
D=abs(det(A));
D=closetozeroroundoff(D);
if D==0
fprintf(\'The points lie on the same line and no parallelogram can be built\');
else
fprintf(\'The area of the parallelogram is %1f\',D);
end
end
if m==3
v1=B(:,2)-B(:,1);
v2=B(:,3)-B(:,1);
v3=B(:,4)-B(:,1);
A=[v1,v2,v3];
D=abs(det(A));
D=closetozeroroundoff(D);
if D==0
fprintf(\'The points lie on the same line and no parallelepiped can be built\');
else
fprintf(\'The area of the parallelepiped is %1f\',D);
end
end
end

function B=closetozeroroundoff(A)
[m,n]=size(A);
for i=1:m
for j=1:n
if abs(A(i,j))<10^(-7)
A(i,j) = 0;
end
end
end
B=A;

end

MATLAB The area of a parallelogram in R 2 , built on vectors v 1 , v 2 , and the volume of a parallelepiped in R 3 , built on vectors v 1 , v 2 , v 3 , are det
MATLAB The area of a parallelogram in R 2 , built on vectors v 1 , v 2 , and the volume of a parallelepiped in R 3 , built on vectors v 1 , v 2 , v 3 , are det

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site