write a function called tri_area returns the area of a triangle with base b and height h

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

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
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
vca = area(:);
tri_area = sum(vca);
end
function [area, tri_area]= tri_area (b, h)
area=(1/2)*(b)*(h)
end
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
this will not work for inputs that are not positive integers.
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.
Assuming that Nur Ameera Nabila Abdul Rahim is addressing Naveen Gehlot then:
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(:);
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?
tri_area(b,h)=(b*h)/2 area=tri_area(b,h) %command window area = tri_area(2,3)
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)
area = 3
Here random input means can anyone help me
function tri_area(b,h)
area=.5*b*h
end
The result in area is not assigned to the output. It's just dumped to console.

Accedi per commentare.

 Risposta accettata

function area = tri_area(b,h)
area = 0.5*b*h;
end
From the command window
A = tri_area(3,2)

21 Commenti

what is the answer for random inputs here?
>> tri_area(rand,rand)
ans =
0.19421832587356
>> tri_area(rand,rand)
ans =
0.0299210820832802
>> tri_area(rand,rand)
ans =
0.362726197496423
how to convert the negative values into positive
You should consider whether you really should convert negative values into positive: negative area has uses.
how should we get the random inputs correct ?
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?
What difficulty do you observe with this code? What inputs to it are giving the wrong answer?
Done it. Nothing wrong with the code. There was a problem in random inputs.
kindly can anyone point out me where I was wrong?
someone please tell the solution for random inputs. i got the same error as above
Have you considered adding disp statements so you can see what parameters are being passed for the random input case?
How to solve the random inputs part?
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?

Accedi per commentare.

Più risposte (3)

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
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

Accedi per commentare.

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)
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.

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

Accedi per commentare.

Categorie

Scopri di più su Deep Learning Toolbox in Centro assistenza e File Exchange

Richiesto:

il 9 Apr 2020

Commentato:

DGM
il 16 Ago 2024

Community Treasure Hunt

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

Start Hunting!

Translated by