Question: "Is this triangle right-angled?" - where can I find functions and their uses/learn about the program outside of class

9 ビュー (過去 30 日間)
So the question I have to answer is:
"Given three positive numbers a, b, c, where c is the largest number, return true if the triangle with sides a, b and c is right-angled. Otherwise, return false."
This is my solution which doesn't work.
function flag = isRightAngled(a, b, c)
flag = true
function flag ~= isRightAngled(a, b, c) flag = false;
end
The first part of the code works fine, and flag returns true when I run the code through test suite on cody courseworks, bolded code is what I added and doesn't work - which I need help with!
Thanks
  2 件のコメント
Roger Stafford
Roger Stafford 2017 年 3 月 3 日
編集済み: Roger Stafford 2017 年 3 月 3 日
Consult the pythagorean theorem
Stephen23
Stephen23 2017 年 3 月 3 日
編集済み: Stephen23 2017 年 3 月 3 日
And the line
function flag ~= isRightAngled(a, b, c) flag = false;
is not MATLAB syntax at all. Read the code help (red/orange underlines): they will help you to figure out why this is not valid syntax. Some of the problems in that line are:
  • trying to define a nested function without a matching end.
  • trying to define a function and allocate its output using ~=.
  • Recursive function calling without any limits.
I suspect that none of these were intentional. To fix this you need to how to call functions, and how this is different to defining functions:

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

回答 (5 件)

Lewis Pole
Lewis Pole 2017 年 3 月 3 日
I have no idea how to implement a^2+b^2=c^2 into code
I was hoping someone could fix the whole code up and I could break it down to understand how to set code out, what does what, etc.

Jan
Jan 2017 年 3 月 3 日
d = a^2 + b^2 - c^2;
Now you cannot expect this to be exactly zeros due to the limited precision of floating point values in the double format. But you can check, if the result is "really small". See the command eps(x). The argument of eps() is important for the magnitude of the limit.

Lewis Pole
Lewis Pole 2017 年 3 月 3 日
I'm still having problems..
function flag = isRightAngled(a, b, c) flag = true
end
Passes the flag=true test.
I don't know how to implement the function a^2+b^2=c^2 into code, which if it fails, should return flag=false to pass the flag=false test.
I know it might be asking to be spoon fed, but I haven't been taught anything yet and I'm just wanting to know more about code before I enter class next week. I believe breaking everything down is a more efficient way to learn.
  1 件のコメント
Stephen23
Stephen23 2017 年 3 月 3 日
編集済み: Stephen23 2017 年 3 月 3 日
"I'm just wanting to know more about code"
That is a great idea! The best place to start is by working through the introductory tutorials:
There are also video tutorials:
You will also find hundreds of online tutorials for getting started with MATLAB.

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


Star Strider
Star Strider 2017 年 3 月 3 日
I would add a sort call, since the three sides could be input in any order:
a = rand; % Input Argument
b = rand; % Input Argument
c = rand; % Input Argument
sides = sort([a b c]); % Sort To Determine Order
test = sides(3)^2 - sides(2)^2 + sides(1)^2; % Check Pythagorean Theorem
flag = abs(test < 1E-4); % Allow For Small Errors

Pen-Li (Ben) Yu
Pen-Li (Ben) Yu 2020 年 12 月 4 日
flag = (0 == -a^2-b^2-c^2+2*max([a,b,c])^2)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by