right triangle area within a certain tolerence

How do I write a matlab code that uses a user defined function to determine the area of all right triangles with sides ranging from 0 to 3 inchs in .5 inch increments. I want to do it with 2 for loops and an if statement to flag all triangles with an area tollerence of .3 in^2 with the area being a UDF. Below is the code i have so far.
for b=0:.5:3
for h=0:.5:3
A=Area_tri(b,h)
if A=.3
disp("A is .3")
end
end
end

2 Kommentare

What problems are you having? What you have seems like it could work for what you have stated you want to do.
It doesnt cycle throgh and give me a list of areas with .3 though

Melden Sie sich an, um zu kommentieren.

Antworten (1)

John D'Errico
John D'Errico am 3 Apr. 2019
Bearbeitet: John D'Errico am 3 Apr. 2019

0 Stimmen

You have two problems in one line. Worse, you have two problems virtually within the same character. Hey, that could be a record. ;-)
if A=.3
You write that "test".
First, A=.3 is NOT a test. MATLAB uses = to assign something to a variable. So you would write
A = 0.3;
to assign the value 0.3 to A.
If you want to test for EXACT equality, you use == not = for the test.
So your first problem was the test for quality. But that is also your second problem. You should NEVER test for exact equality to a floating point number. MATLAB does not store the number 0.3 as an exact decimal. Instead, you should test for approximate equality. So you might do this instead:
if abs(A - 0.3) < eps
disp("A is reasonably close to 0.3.")
end

Kategorien

Tags

Gefragt:

am 3 Apr. 2019

Bearbeitet:

am 3 Apr. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by