How can I fix my function?

17 ビュー (過去 30 日間)
Matlab Student
Matlab Student 2021 年 9 月 19 日
コメント済み: Walter Roberson 2021 年 9 月 22 日
Hi, I'm new to Matlab and have a lot of difficulty understanding how to properly write functions. I need to write a function ispythag that will recieve three positive integers (a,b,c) that will return a logical 1 if they form a pythagorean triple, or logical 0 if they don't. I also cannot use if or select statements.This is the code I have written but I always get an error saying unrecognized function or variable 'a', however when I write a script using this code and write "ispythag(1,2,3)" or "ispythag(3,4,5)" in the command prompt, I get the logical answers I need. After several attempts at changing my code without success, I was hoping someone might be able to lend me some guidance. Thank you very much for your time and help!
Ptriple = ispythag(a,b,c)
Ptriple = a^2+b^2 == c^2;
ispythag(1,2,3)
ispythag(3,4,5)
  1 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 9 月 20 日
You have not property declared your function. See this page for help with that. It also contains some working examples that you can modify to get your function started.

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

採用された回答

Wan Ji
Wan Ji 2021 年 9 月 20 日
Hey, friend
Just try the inline function
ispythag = @(a,b,c)a.^2+b.^2 == c.^2;
a = ispythag(1,2,3)
b = ispythag(3,4,5)
Then you will obtain the result
a =
logical
0
b =
logical
1
  2 件のコメント
Matlab Student
Matlab Student 2021 年 9 月 22 日
Thank you ver much for your help. Just a quick follow up question, why do i need to include the '@' in the function?
Walter Roberson
Walter Roberson 2021 年 9 月 22 日
The correct term for this in MATLAB is "anonymous function". It is documented at https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
MATLAB does have "inline" functions as well, which are not recommended; they are an older technology that has a number of drawbacks compared to anonymous functions.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by