필터 지우기
필터 지우기

How to plot a triangle with functions in matlab

조회 수: 2 (최근 30일)
Chloe
Chloe 2024년 4월 22일
이동: Steven Lord 2024년 6월 18일
Does this code work
xIN = 10;
yIN = 5;
xValuesArray = [];
yValuesArray = [];
for i=1:3
[xValuesArray(i) , yValuesArray(i)] = get2();
end
v(:,1)= xValuesArray'
v(:,2)= yValuesArray'
polyin = polyshape(v)
p = perimeter(polyin)
a = polyarea(xValuesArray, yValuesArray)
figure
hold on
plot([xValuesArray(1) xValuesArray(2)],[yValuesArray(1) yValuesArray(2)], ...
[xValuesArray(2) xValuesArray(3)],[yValuesArray(2) yValuesArray(3)], ...
[xValuesArray(3) xValuesArray(1)],[yValuesArray(3) yValuesArray(1)])
function [getX, getY] = get2()
getX = input("Give me a value for x\n");
getY = input("Give me a value for y\n");
end

답변 (2개)

DGM
DGM 2024년 4월 22일
이동: Steven Lord 2024년 6월 18일
Using a bunch of input() calls has to be the most tedious and cumbersome way of getting a point list, but I suppose it works.
Also, since you created a polyshape, you can just plot it. You can also just get its area instead of using polyarea().
% a pointlist
xy = [0 0; 1 0; 0 1];
polyin = polyshape(xy);
p = perimeter(polyin)
p = 3.4142
a = area(polyin)
a = 0.5000
plot(polyin)

Animesh
Animesh 2024년 6월 18일
Hi Chloe,
It has become really simple these days to quicky visulaize and implement our ideas through AI chat playground (https://in.mathworks.com/matlabcentral/playground/new).
I was able to implement your query with just one line. Attaching the screenshot for your reference:
Do check out the new MATLAB AI Chat Playground.
I hope this helps!
Animesh

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by