I have a problem with the nested function.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Samy Ben Thabet
le 7 Nov 2018
Commenté : Hari Ijjada
le 30 Août 2019
I need to write a function which gives the area of a triangle and in it a subfunction that calculates the distance between each point of the triangle (I need it for the area). My program doesn't work can someone let me know what I am doing wrong pls?
Here is my program:
x1=input('Enter the x coordinate of the first point ');
y1=input('Enter the y coordinate of the first point ');
x2=input('Enter the x coordinate of the second point ');
y2=input('Enter the y coordinate of the second point ');
x3=input('Enter the x coordinate of the third point ');
y3=input('Enter the y coordinate of the third point ');
s=(a+b+c)/3;
fprintf('The area of the triangle is %.2f\n',area)
end
and here is my function with the nested subfunction:
function [area]=calcarea(a,b,c,s)
function [a,b,c]=dist(x1,x2,x3,y1,y2,y3)
a=sqrt((x1-x2)^2 + (y1-y2)^2);
b=sqrt((x1-x3)^2 + (y1-y3)^2);
c=sqrt((x2-x3)^2 + (y2-y3)^2);
end
area=sqrt(s*(s-a)*(s-b)*(s-c));
end
0 commentaires
Réponse acceptée
madhan ravi
le 8 Nov 2018
Modifié(e) : madhan ravi
le 8 Nov 2018
Multiple functions way:
x1=input('Enter the x coordinate of the first point ');
y1=input('Enter the y coordinate of the first point ');
x2=input('Enter the x coordinate of the second point ');
y2=input('Enter the y coordinate of the second point ');
x3=input('Enter the x coordinate of the third point ');
y3=input('Enter the y coordinate of the third point ');
[a,b,c]=dist(x1,x2,x3,y1,y2,y3) %first function call
s=(a+b+c)/3;
area=calcarea(a,b,c,s) %second function call
fprintf('The area of the triangle is %.2f\n',area)
function [a,b,c]=dist(x1,x2,x3,y1,y2,y3)
a=sqrt((x1-x2)^2 + (y1-y2)^2);
b=sqrt((x1-x3)^2 + (y1-y3)^2);
c=sqrt((x2-x3)^2 + (y2-y3)^2);
end
function area=calcarea(a,b,c,s)
area=sqrt(s*(s-a)*(s-b)*(s-c));
end
nested function way:
x1=input('Enter the x coordinate of the first point ');
y1=input('Enter the y coordinate of the first point ');
x2=input('Enter the x coordinate of the second point ');
y2=input('Enter the y coordinate of the second point ');
x3=input('Enter the x coordinate of the third point ');
y3=input('Enter the y coordinate of the third point ');
area=calcarea(x1,x2,x3,y1,y2,y3) %parent function call
fprintf('The area of the triangle is %.2f\n',area)
function [area]=calcarea(x1,x2,x3,y1,y2,y3) %parent function
[a,b,c]=dist(x1,x2,x3,y1,y2,y3)
s=(a+b+c)/3;
area=sqrt(s*(s-a)*(s-b)*(s-c));
function [a,b,c]=dist(x1,x2,x3,y1,y2,y3) %nested function
a=sqrt((x1-x2)^2 + (y1-y2)^2);
b=sqrt((x1-x3)^2 + (y1-y3)^2);
c=sqrt((x2-x3)^2 + (y2-y3)^2);
end
end
1 commentaire
Hari Ijjada
le 30 Août 2019
if i use multiple function way or nested functions way .By using which function name i should save the function file ?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!