How to assign 10 degrees (a unit to measure angle) to a variable ?

44 ビュー (過去 30 日間)
vikas mishra
vikas mishra 2017 年 11 月 6 日
コメント済み: Walter Roberson 2017 年 11 月 7 日
I wish to define a variable that takes the value 10 degrees.
For example, I am defining \theta = 10; in a matlab program. I consider that '\theta' has stored 10 degrees (a unit to measure angle) not the real number 10. Is it a correct way to define this? How can we define \theta such that it stores 10 degrees (a unit to measure angle)?
  3 件のコメント
vikas mishra
vikas mishra 2017 年 11 月 6 日
Thanks, I am doing the same but the question is whether it is correct or not? I am plotting x vs y with the condition of varying \theta where \theta is the angle made by the curve with respect to x-axis. I am defining the initial value of \theta as \theta_o = 10. Will MATLAB consider it in degrees?
Stephen23
Stephen23 2017 年 11 月 7 日
"Will MATLAB consider it in degrees?"
No. That is the programmer's job.

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

回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 11 月 6 日
See https://www.mathworks.com/help/symbolic/units-of-measurement-tutorial.html for how to create a variable with degrees attached.
However, sin() cannot operate on such items. The way to work with degrees is with sind() or by converting the degrees into radians.

Eric
Eric 2017 年 11 月 6 日
編集済み: Eric 2017 年 11 月 7 日
As James suggests, the following will work just fine:
theta = 10; % degrees (Write a comment at the end if you want to remember later)
MATLAB ultimately doesn't really care about units anywhere. You are the one responsible for checking that any equations you use are considering degrees, not radians (or any units, really).
Having said that however, trig function calls such as sin, cos, etc. tend to default to using radians. You will need entirely different functions, namely sind, cosd, etc., in order to get proper answers with degrees as inputs. Be sure to watch out for these situations, or ones involving any polar or spherical coordinate conversions, know what you need for them and how to convert, and you should be fine.
Edit: Apparently I was wrong and MATLAB can indeed attach units to numbers via the symbolic Math Toolbox, if you really wanted it to do so. See Walter's answer for more info.
  4 件のコメント
vikas mishra
vikas mishra 2017 年 11 月 7 日
Thanks @ James
Walter Roberson
Walter Roberson 2017 年 11 月 7 日
"MATLAB ultimately doesn't really care about units anywhere."
As well as the symbolic math options, the Simscape family expects units to be attached to items, and will do automatic unit conversion. However, to call upon MATLAB routines there, you need to tell Simscape to strip off the unit and pass the value through.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by