write a function called tri_area returns the area of a triangle with base b and height h
Mostra commenti meno recenti
hello this is my function code and command window code and there is a message of invalid expression at line 2 and i dont know what is the wrong can anyone help me
function [area] = tri_area([b,h]);
tri_area([b,h])=(0.5)*(b)*(h)
area=tri_area([b,h])
end
%command window
area = tri_area[3,2])
18 Commenti
Tushar Tahelramani
il 13 Mag 2020
actually my code wgich is similar almost similar to yours work well for 1st assessment of tri_area[3,2] , but second assessment indicates RANDOM INPUTS for that i don't know what to do
plz help me
pramod kumar jaipal
il 15 Mag 2020
for randam input
Mujibur Rahman
il 17 Mag 2020
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
vca = area(:);
tri_area = sum(vca);
end
Hrvoje Rogulj
il 19 Mag 2020
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
end
Abdullah Javed
il 27 Mag 2020
Make a little change in your code and it will work.
function [area] = tri_area(b,h)
tri_area(b,h)=(0.5)*(b)*(h)
area=tri_area(b,h)
end
Walter Roberson
il 27 Mag 2020
this will not work for inputs that are not positive integers.
Naveen Gehlot
il 6 Giu 2020
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
vca = area(:);
tri_area = sum(vca);
end
use this function for randow inputs
it will works.
Nur Ameera Nabila Abdul Rahim
il 10 Ago 2020
do you mind explaining to me line 3 & 4
Thanks !
Walter Roberson
il 10 Ago 2020
area=(1/2)*(b)*(h)
is just the standard formula for the area of a triangle.
vca = area(:);
tri_area = sum(vca);
That is one way of calculating the total area, which is something that might potentially be done if the user asked to calculate the area for multiple triangles at the same time. However, if the user did ask to calculate for multiple triangles at the same time, it is fairly likely that area=(1/2)*(b)*(h) would be the wrong formula, and that it should instead be
area=(1/2)*(b).*(h);
or better
area = (1/2) .* b(:) .* h(:);
Christine Mizzi
il 27 Ago 2020
What is the purpose for writing two output arguments in the code? i.e. [area, tri_area]
If the user is calling the area of a triangle wouldn't that be only one output argument?
Noor Fatima
il 4 Feb 2023
Spostato: DGM
il 4 Feb 2023
tri_area(b,h)=(b*h)/2 area=tri_area(b,h) %command window area = tri_area(2,3)
Noor Fatima
il 4 Feb 2023
Spostato: DGM
il 4 Feb 2023
Can any help in finding error bcz it does not gives answer 3
tri_area = @(b,h) (b*h)/2;
area = tri_area(2,3)
Noor Fatima
il 4 Feb 2023
Here random input means can anyone help me
Walter Roberson
il 4 Feb 2023
tri_area(rand(), rand())
Noor Fatima
il 5 Feb 2023
Thanku Walter roberson
Habiba
il 16 Ago 2024
function tri_area(b,h)
area=.5*b*h
end
DGM
il 16 Ago 2024
The result in area is not assigned to the output. It's just dumped to console.
Risposta accettata
Più risposte (3)
Imane Tahar
il 19 Nov 2020
function area = tri_area(b,h)
area = (b*h)/2
end
Siya Desai
il 4 Apr 2021
Modificato: Siya Desai
il 4 Apr 2021
function
function [area] = tri_area (b,h)
tri_area = (0.5)*(b)*(h)
code to call your function
tri_area(2,3) %any random input
1 Commento
result = tri_area(2,3) %any random input
function [area] = tri_area (b,h)
tri_area = (0.5)*(b)*(h)
end
Pelden Chodon
il 27 Mag 2021
function [area, tri_area] = tri_area(b,h) ;
area = (0.5)*(b)*(h);
v = area(:);
tri_area = sum(v);
end
% Test that your function runs as expected before pressing Submit
[area, tri_area] = tri_area(2,3)
1 Commento
You're declaring a variable with the same name as the function you're calling. This will either throw an error or silently cause other errors, depending on where you put the function.
For scalar inputs, summing area serves no purpose. For nonscalar inputs, the results are either wrong, or will throw an error.
% summing a scalar accomplishes nothing
[allareas totalarea] = tri_area(2,3)
% some nonscalar inputs return the wrong results
[allareas totalarea] = tri_area([2 2; 2 2],[3 3; 3 3])
% some nonscalar inputs return the wrong number of wrong results
[allareas totalarea] = tri_area([2 2],[3;3])
% some nonscalar inputs fail completely
[allareas totalarea] = tri_area([2 2],[3 3])
function [allareas, totalarea] = tri_area(b,h)
allareas = (0.5)*(b)*(h); % these parentheses don't do anything
v = allareas(:);
totalarea = sum(v);
end
As this is all largely copy-pasted from other bad answers posted above, I should point out that much of these problems were already openly explained before you posted this. See also: function, array vs matrix operations
Categorie
Scopri di più su Deep Learning Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
