Create a two variables function, including several law in MATLAB

1 回表示 (過去 30 日間)
la
la 2015 年 6 月 18 日
コメント済み: la 2015 年 8 月 19 日
Dear Mathworks team
Hello
Please guide me. I searched in the FAQ and the Internet, but could not find answer for my question.
I want to create a two variables function, including several law in MATLAB. Then, I want to import code into C#.(I need the output of this function for C#)
How can I create this function
Thank you in advance for your attention and answer

採用された回答

B.k Sumedha
B.k Sumedha 2015 年 6 月 18 日
  5 件のコメント
Stephen23
Stephen23 2015 年 6 月 19 日
編集済み: Stephen23 2015 年 6 月 19 日
@la: there are multiple issue with the code that could be improved, and these would make the bug-fixing easier too:
  • do not place close all and clc at the start of every function and script. Although beginners love doing this it is sloppy programming: this function has nothing to do with figures, so why close them inside it? Functionally unrelated and thus a poor design decision.
  • remove unnecessary parentheses, as these only clutter the text and make it hard to read.
  • learn about operator precedence to help remove unnecessary parentheses.
  • do not repeat logical comparisons unnecessarily: perform them once!
  • do not use chains of mutually-exclusive if statements when you can simply use elseif statements.
  • learn to read the documentation. This will help you to discover that relational operators in MATLAB are binary operators, so the syntax A<x<B is not permitted, only A<x & x<B, exactly as Walter Roberson clearly explained in his answer too.
After all of that you might end up with a function like this (attached):
la
la 2015 年 8 月 19 日
Stephen Cobeldick
thanks

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 6 月 19 日
21<x2<=27 is parsed as
((21<x2)<=27)
In the first step, 21<x2 returns a logical value, 0 or 1. In the second step, that 0 or 1 is compared <= 27. 0 and 1 are always <= 27 so the overall test would always be true.
If you want to test whether a value is in a range, write it as two tests:
21<x2 & x2 <= 27
  1 件のコメント
la
la 2015 年 6 月 19 日
Walter Roberson! Thanks a lot. :)

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

カテゴリ

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