Help with nested functions and finding the circumcentre
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Geoff Hayes
il 6 Mar 2019
Modificato: Geoff Hayes
il 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.
2 Commenti
Geoff Hayes
il 6 Mar 2019
Modificato: Geoff Hayes
il 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Graphics Object Programming in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!