right triangle area within a certain tolerence
3 ビュー (過去 30 日間)
古いコメントを表示
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
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.
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Debugging and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!