How to find the area of a hexagon in MATLAB

A regular hexagon is a six-sided polygon. Each side is equal in length, with each interior angle measuring 1200120^0.

Formula for the area of a hexagon

The formula to calculate the area of a regular hexagon is as follows:

Area=3×3×side22.\text{Area} = \frac{3 \times \sqrt{3} \times \text{side}^2}{2}.

In the formula above, the side represents the length of a side of a hexagon.

A regular hexagon
A regular hexagon

Problem statement

We are given the length of a hexagon, and we have to calculate its area using the formula above.

Explanation

Let’s suppose we have a side of length 5. When we put the value of the length in the formula above, we have the following equation:

Area=3×3×side22,=3×3×522,=3×3×252,64.95.\begin{align*} \text{Area} &= \frac{3 \times \sqrt{3} \times \text{side}^2}{2}, \\ &= \frac{3 \times \sqrt{3} \times \text{5}^2}{2}, \\ &= \frac{3 \times \sqrt{3} \times \text{25}}{2}, \\ &\approx 64.95. \\ \end{align*}

Implementation

The approach here is to use the sqrt() function for calculating the sqrt(3). We create a function and call it to calculate the area.

Let’s see an example to calculate the area of a hexagon:

sideLength = 5;
function area = hexagon_area(side)
area = (3 * sqrt(3) / 2) * side^2;
end
area = hexagon_area(sideLength);
disp(['Area of the hexagon: ', num2str(area)]);

Code explanation

In the code above:

  • Line 1: We declare a variable, side_length, to store the length of a hexagon.

  • Lines 3-5: We create a function, hexagon_area, that takes side as the input parameter and calculates the area of the hexagon using the formula.

  • Line 7: We declare a variable, area, to store the value of the area and call the function.

  • Line 9: We print the area of the hexagon.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved