File Exchange Pick of the Week

我们最好的用户提交

Physical Units in MATLAB

内容

Physical Units in MATLAB

Greg’spick this week is物理单位工具箱bySky Sartorius.

有单位使用您在MATLAB中计算的数字,这不是很好吗?

% Velocity in kilometers per hourv = 12.3 * u.kph% Time in secondst = 3.7 *美国% Distance travelledd = v * t%将距离转换为脚U2num(D,U.Foot)%将距离转换为脚的另一种方法d/u.ft
v = 3.4167 m - s t = 3.7000 s d = 12.6417 m ans = 41.4753 ans = 41.4753

I’ve always wanted this functionality in both MATLAB and Simulink. When I was a student in University, MATLAB was my favorite programming tool, but I didn’t use it much for engineering homework, as there were other tools that had nice worksheets and included units with unit conversions and computations.

即使作为数学工作的员工,我也发现自己使用了其他数学软件来协助需要仔细跟踪和转换单元的计算。

我为什么选择这个条目?

对我来说,这两个关键要素是:

  • Easy to use
  • 据记录

The inclusion of units by just simply multiplying by the desired unit is a well thought out mechanism. Also, there are of course functions and methods to extract values of a specific unit type.

And there is unit consistency checking, making sure that you don’t attempt to do things like convert a distance to a temperature.

尝试U2num(D,英国)抓住Me disp(Me.message);end
不兼容的输入尺寸

The attention to detail and planning the functionality provides for easy use of this tool. For example, Sky included functions for unit conversion for standard MATLAB variables, not just the unit class. This means you don’t have to think about whether or not a particular variable is of theDimvar是否上课。

a = pi;U2NUM(A)
ANS = 3.1416

扩展班级的能力

The organization and documentation of the code makes it pretty easy to add additional functions that enable conversions. For example, many times I may want to include the result of a particular computation as a variable in Simulink. With the addition of units in Simulink (R2016a) there can be consistency between the environments.

g = 9.81 *u.m/u.s^2%创建具有与“ G万博1manbetx”相同的值和单位的参数对象。gParam = u2param(g)
g = 9.8100 m --- s^2 gParam = Parameter with properties: Value: 9.8100 CoderInfo: [1×1 Simulink.CoderInfo] Description: '' DataType: 'auto' Min: [] Max: [] Unit: 'm/s^2' Complexity: 'real' Dimensions: [1 1]

现在,simulink中的单元引擎将能够帮助何时万博1manbetx在Simulink模型中使用此变量“ GPARAM”之类的单元一致性检查。

typeu2paramtypegetunitstring
function p = u2param(v) assert(isa(v, 'DimVar')); s = getUnitString(v); p = Simulink.Parameter(u2num(v)); p.Unit = s; end function s = getUnitString(v) assert(isa(v, 'DimVar')); [num, dem] = display(v); s = sprintf('%s/%s', num, dem); end

Getting the unit text

将一个值的单元作为字符串单位,可以通过其他MATLAB和SIMULINK工具来解释,这将很有帮助。万博1manbetx

Technically the UNITSOF function returns the unit string of an object, but in a specialized format.

大学tsOf(g)
[M^1] [S^-2]

This can be remedied pretty easily with the following:

[分子,分母] = display(g);[分子'/'分母]
ans = 'm/s^2'

当然,作为或R2017A,您可以做类似的事情:

分子 +“/”+分母
ans =“ m/s^2”

MathWorks tools that have units

有一些插件确实支持物理单位。万博1manbetx

万博1manbetx现在,在信号线上有一个单元的概念。

TheSIMSCAPEs manbetx 845在定义物理元素的属性时,所有产品都需要使用特定单元。

The符号数学工具箱also has somecapabilityto apply physical units to calculations and enable symbolic manipulations as well.

Should MATLAB have a units system built in?

Let us know这里.

与Matlab®R2017A一起出版

|
  • print
  • send email

注释

To leave a comment, please click这里to sign in to your MathWorks Account or create a new one.