right triangle area within a certain tolerence

3 views (last 30 days)
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 Comments
Bob Thompson
Bob Thompson on 3 Apr 2019
What problems are you having? What you have seems like it could work for what you have stated you want to do.
ian flanagan
ian flanagan on 3 Apr 2019
It doesnt cycle throgh and give me a list of areas with .3 though

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 3 Apr 2019
Edited: John D'Errico on 3 Apr 2019
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

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!