How Can You Calculate the Area of a Polygon in MATLAB?

  • Thread starter Thread starter StephvsEinst
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a MATLAB function to calculate the area of a polygon defined by a variable number of vertices. Participants explore coding techniques, clarify terminology, and share personal experiences related to the problem.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant outlines a method for calculating the area of a polygon by dividing it into triangles and using the determinant to find the area of each triangle.
  • Another participant suggests that the user needs to input the coordinates of all the vertices for the calculation.
  • A later reply provides a MATLAB function that implements the area calculation, including input prompts for vertex coordinates and handling cases with insufficient vertices.
  • One participant corrects a terminology error, clarifying the difference between a polygon and a polynomial, which leads to a humorous acknowledgment of the mistake.
  • There is a light-hearted exchange about the participant's location, with one confirming they are from Portugal.

Areas of Agreement / Disagreement

Participants generally agree on the need for correct terminology and the method for calculating the area, but there is no consensus on the best approach to implement the function in MATLAB, as different coding strategies are presented.

Contextual Notes

The discussion includes assumptions about the input format and the handling of different numbers of vertices, which may not be fully resolved in the provided solutions.

StephvsEinst
Science Advisor
Messages
41
Reaction score
1

Homework Statement


H[/B]ow to create a function to determine the area of a polynomial that has N vertexes in MATLAB?

Homework Equations


p = input('Introduce the number of vertexes of the polynomial:')
n=p-2;

The polynomial can be devided by N-2 triangles and the area of each triangle is given by A=(1/2)*det(B) where B=[x1 x2 x3 ... xn; y1 y2 y3 ... yn; 1 1 1 1 1 ... 1(last row is filled with n ones)]

The Attempt at a Solution


p = input('Introduce the number of vertexes of the polynomial:')
n=p-2;
if n<3:
fprintf('error')
else
(...) - I tried 'if cicles' and sums but I don't know know how to ask for all x and y of the polynomial vertexes (because the size of the matrix varies with the number of vertexes of the polynomial).

Would apreciate any kind of help :D
 
Physics news on Phys.org
You have to ask all the coordinates of the vertexes
 
Already solved it:

function p = area_polinomio(N)
i=1;
area = 0;
A = [0,0,0;0,0,0;0,0,0];
if N < 3
warning('pontos insuficientes')
return
else
a = [input('introduza a abcissa do primeiro ponto: '); input('\n introduza a ordenada do primeiro ponto: '); 1];
for i = 1 : N-1
b = [input('\n introduza a abcissa do ponto seguinte: '); input('\n introduza a ordenada do ponto seguinte: '); 1];
if i == 1
A = [a, b];
end
if i == 2
A = [A, b];
area = area + (1/2)*det(A);
end
if i > 2
A = A(:,[1:1,3,3:2,2,4:end]);
A = A(1:3,1:2);
A = [A, b];
area = area + (1/2)*det(A);
end

i = i+1;
end
end
%A

Thanks.
 
The word you're searching for is polygon, a two-dimensional figure with three or more straight sides that intersect at the vertices of the polygon. Triangles, rectangles, and hexagons are examples of polygons.

A polynomial is an expression made up of sums of integer powers of the variable. For example, f(x) = 2x + 3, g(x) = x2 - 3x + 2, and h(x) = x4 - 1 are polynomial functions of degree 1, 2, and 4 respectively.
 
LOOOL. That made me laugh xD
A silly misake made by me. Acually when I was solving this I named the function area_polynomial and not area_polygon until it was time to run it, then I changed the name. Was completely focused on the exercise xD
 
Are you from Portugal or Brazil? I'm just curious...
 
Portugal :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
15
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
4K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K