MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">ここ</a>」をクリックしてください。

0

right triangle area within a certain tolerence

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 Nbob
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 2019 年 4 月 3 日
It doesnt cycle throgh and give me a list of areas with .3 though

サインイン to comment.

タグ

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 件のコメント

サインイン to comment.



Translated by