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

Hey guys, So I have to write and MATLAB code and I have no idea where to start.

ID: 2995334 • Letter: H

Question

Hey guys, So I have to write and MATLAB code and I have no idea where to start. I was wondering if any of you could help me out and write the code out?

The matlab function should have the following input/output form:

[Area, Perimeter, Vertices] = getRectangle(x1, y1, x2, y2)

Where:

x1: Is the x-coordinate of the bottom left corner of the rectangle

y1: Is the y-coordinate of the bottom left corner of the rectangle

x2: Is the x-coordinate of the upper right corner of the rectangle

y2: Is the y-coordinate of the upper right corner of the rectangle

Area: is the single number/scalar area inside the rectangle

Perimeter: is the single number/scalar length of the perimeter of the rectangle

Vertices: is a 2-column, 4-row array containing the x and y locations of the rectangle vertices

Explanation / Answer

Following is the matlab function, save the follwing code as getRectangle.m


function [Area, Perimeter, Vertices] = getRectangle(x1, y1, x2, y2)

% [Area, Perimeter, Vertices] = getRectangle(x1, y1, x2, y2)
%
% Where:
%
% x1: Is the x-coordinate of the bottom left corner of the rectangle
%
% y1: Is the y-coordinate of the bottom left corner of the rectangle
%
% x2: Is the x-coordinate of the upper right corner of the rectangle
%
% y2: Is the y-coordinate of the upper right corner of the rectangle
%
% Area: is the single number/scalar area inside the rectangle
%
% Perimeter: is the single number/scalar length of the perimeter of the rectangle
%
% Vertices: is a 2-column, 4-row array containing the x and y locations of the rectangle vertices

Area = abs((x2-x1)*(y2-y1));
Perimeter = abs(2*((x2-x1)+(y2-y1)));
Vertices = [x1,y1; x1,y2; x2,y2; x2,y1];

end