I have a problem with the nested function.

2 visualizaciones (últimos 30 días)
Samy Ben Thabet
Samy Ben Thabet el 7 de Nov. de 2018
Comentada: Hari Ijjada el 30 de Ag. de 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

Respuesta aceptada

madhan ravi
madhan ravi el 8 de Nov. de 2018
Editada: madhan ravi el 8 de Nov. de 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 comentario
Hari Ijjada
Hari Ijjada el 30 de Ag. de 2019
if i use multiple function way or nested functions way .By using which function name i should save the function file ?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by