right triangle area within a certain tolerence

3 ビュー (過去 30 日間)
ian flanagan
ian flanagan 2019 年 4 月 3 日
編集済み: John D'Errico 2019 年 4 月 3 日
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 件のコメント
Bob Thompson
Bob Thompson 2019 年 4 月 3 日
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 2019 年 4 月 3 日
It doesnt cycle throgh and give me a list of areas with .3 though

サインインしてコメントする。

回答 (1 件)

John D'Errico
John D'Errico 2019 年 4 月 3 日
編集済み: John D'Errico 2019 年 4 月 3 日
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

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by