You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
write a function called tri_area returns the area of a triangle with base b and height h
361 views (last 30 days)
Show older comments
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])
16 Comments
Tushar Tahelramani
on 13 May 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
Mujibur Rahman
on 17 May 2020
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
vca = area(:);
tri_area = sum(vca);
end
Abdullah Javed
on 27 May 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
Naveen Gehlot
on 6 Jun 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.
Walter Roberson
on 10 Aug 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
on 27 Aug 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
on 4 Feb 2023
Moved: DGM
on 4 Feb 2023
tri_area(b,h)=(b*h)/2 area=tri_area(b,h) %command window area = tri_area(2,3)
Noor Fatima
on 4 Feb 2023
Moved: DGM
on 4 Feb 2023
Can any help in finding error bcz it does not gives answer 3
Accepted Answer
Torsten
on 9 Apr 2020
Edited: darova
on 9 Apr 2020
function area = tri_area(b,h)
area = 0.5*b*h;
end
From the command window
A = tri_area(3,2)
21 Comments
Walter Roberson
on 24 May 2020
>> tri_area(rand,rand)
ans =
0.19421832587356
>> tri_area(rand,rand)
ans =
0.0299210820832802
>> tri_area(rand,rand)
ans =
0.362726197496423
Walter Roberson
on 29 May 2020
You should consider whether you really should convert negative values into positive: negative area has uses.
Walter Roberson
on 2 Jun 2020
Is there reason to believe that the code is currently processing them incorrectly? What inputs are being used and what response does your code give and what is the expected result?
Walter Roberson
on 15 Jun 2020
What difficulty do you observe with this code? What inputs to it are giving the wrong answer?
SUMANT KUMAR
on 15 Jun 2020
Done it. Nothing wrong with the code. There was a problem in random inputs.
Alric Duarte
on 21 Jun 2020
someone please tell the solution for random inputs. i got the same error as above
Walter Roberson
on 21 Jun 2020
See https://www.mathworks.com/matlabcentral/answers/446968-what-is-wrong-with-my-function#comment_839985 for why AKASH Talapatra's code does not work.
Walter Roberson
on 10 Aug 2020
Have you considered adding disp statements so you can see what parameters are being passed for the random input case?
Walter Roberson
on 26 Dec 2022
When you use the debugger and format long g then what are some sample inputs, and what is the calculated output, and what does the grading program say that the output should be?
More Answers (3)
Siya Desai
on 4 Apr 2021
Edited: Siya Desai
on 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 Comment
Walter Roberson
on 4 Apr 2021
result = tri_area(2,3) %any random input
tri_area = 3
Output argument "area" (and maybe others) not assigned during call to "solution>tri_area".
function [area] = tri_area (b,h)
tri_area = (0.5)*(b)*(h)
end
Pelden Chodon
on 27 May 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 Comment
DGM
on 4 Feb 2023
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)
allareas = 3
totalarea = 3
% some nonscalar inputs return the wrong results
[allareas totalarea] = tri_area([2 2; 2 2],[3 3; 3 3])
allareas = 2×2
6 6
6 6
totalarea = 24
% some nonscalar inputs return the wrong number of wrong results
[allareas totalarea] = tri_area([2 2],[3;3])
allareas = 6
totalarea = 6
% some nonscalar inputs fail completely
[allareas totalarea] = tri_area([2 2],[3 3])
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.
Error in solution>tri_area (line 10)
allareas = (0.5)*(b)*(h); % these parentheses serve no purpose
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
See Also
Categories
Find more on Biotech and Pharmaceutical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)