Help with nested functions and finding the circumcentre

1 回表示 (過去 30 日間)
Sam Thorpe
Sam Thorpe 2019 年 3 月 6 日
編集済み: Geoff Hayes 2019 年 3 月 6 日
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

採用された回答

Geoff Hayes
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.
See Nested Functions for more details.
  2 件のコメント
Sam Thorpe
Sam Thorpe 2019 年 3 月 6 日
Thanks for the help Geoff. I have tried it but now I am getting the argument that there are too many inputs. It is not liking the circumcenter function. I have tried to modify the main functions method of displaying a triangle so I have only have 1 input argument like it does on the circumcenter tutorial, but still nothing. Any advice?
the updated code is:
function Ex7c=triang(x,y)
P=[x',y']
T=[1 2 3]
tr=triangulation(T,P)
figure
triplot(tr)
function[c]=circumcenter
c = circumcenter(tr)
end
c=circumcenter
end
Geoff Hayes
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 ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by