Help with nested functions and finding the circumcentre
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi. I have been asked to create a function which plots a triangle given the [x,y] coordinates. then using nested or subfunctions, we have to find the circumcentre of the triangle and display the coordinates. Once we have this we then have to use another nested or subfunction to plot the circumcircle on the same plot as the triangle and find the radius of the circle.
I have managed to get the triangle to plot using 2 1x4 row vectors with the x and y coordinates of each point, but when I try to get the circumcentre i get no result and no error. Any help would be greatly appreciated. I have the following code:
function Ex7c=triang(x,y)
tri=[x;y] %creates a row vector of x and y coordinates
hold on
plot(tri(1,:), tri(2,:)) %plot triangle along x and y points
function triinput=circumcentre(trioutput)
c=circumcentre(tri)
end
end
댓글 수: 0
채택된 답변
Geoff Hayes
2019년 3월 6일
편집: Geoff Hayes
2019년 3월 6일
Sam - you have nested the circumcentre function within your parent function (good) but you still need to call it somewhere. in order to get the result. So you could pass in the tr as an input paramter to this function and then do some sort of calculation. Or, because the function is nested, it will have access to those variables declared in the parent function. For example,
function Ex7c=triang(x,y)
tri=[x;y] %creates a row vector of x and y coordinates
hold on
plot(tri(1,:), tri(2,:)) %plot triangle along x and y points
function [c] = circumcentre
c = ...; % do something with tri
end
% get the circum-centre
c = circumcentre;
% do something with c
end
Note how we don't need to pass tri to the function as your nested function will have access to it. We call circumcentre outside of the nested function definition (you were doing this inside the nested function) to get the result c which you will then do something with.
댓글 수: 2
Geoff Hayes
2019년 3월 6일
편집: Geoff Hayes
2019년 3월 6일
Sam - you are trying to call circumcentre from itself:
function[c]=circumcenter
c = circumcenter(tr)
end
and so are making this into a recursive function (which I don't think it should be). Instead, you need to add code in this function to calculate the circumcenter given the tr variable. i.e. code for we have to find the circumcentre of the triangle and display the coordinates
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!