Help with nested functions and finding the circumcentre

2 views (last 30 days)
Sam Thorpe on 6 Mar 2019
Edited: Geoff Hayes on 6 Mar 2019
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 on 6 Mar 2019
Edited: Geoff Hayes on 6 Mar 2019
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.
Geoff Hayes on 6 Mar 2019
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