Can I create a symbolic variable with units and assign an initial value of 0

20 次查看(过去 30 天)
G'day, new to Matlab and just been through symbolic equations tutorial.
If I do the following:
syms dia circ u
u=sysunit
dia=10*u.m %10 is assigned with the unit of a metre
If I then say
dia = 0
dia
0 %zero is returned with no units.
Can I assign units to a sym reqardless of quantity and have the units carry through all calculations?
That is, can I define dia and circ with the units of metre or kg etc without assigning a numeric value?
Thanks and reqards
Paul

回答(4 个)

Priyanshu Mishra
Priyanshu Mishra 2019-12-1
According to MATALAB documentation, you need to assign a value to speciy unit. For more information on symunit, you may refer to the following link

Paul Buckman
Paul Buckman 2019-12-4
Thank you Priyanshu
I sort of solved this by creating a function and assigning the units to the function
syms d x u
u = symunit;
fArea(d)=(pi * (d/2)^2)*u.meter^2; %Area of a circle
x = 3;
A = fArea(x);
%That returns
A =
((9*pi)/4)*[m]^2
%if I then do
>> A=0
A =
0 %no units from here on
I would humbly suggest that 0 (zero) is a valid quantity for many if not most variables. Example, velocity calculations on a four-bar linkage crank/ slider mechanism. Both the velocity and acceleration variables goto zero at some point through their position calculations; velocity goes to zero when the slider reaches its maximum and minimum travel, with acceleration going to maximum (+ or -). Motion then reverses and velocity begings to increase from zero. Therefore is a valid value and consequently it should retain its units.
Thanks and Regards
Paul

Walter Roberson
Walter Roberson 2019-12-5
Units are implemented by multiplying by a symbolic variable that can be recognized by the units manipulation package. Unfortunately because it is a multiplication, when the value being multiplied by is 0, the result simplifies to just 0 -- with no unit.
You have to use cell array representation to hold the units along with a value of 0. See the temperature 0 discussion at https://www.mathworks.com/help/symbolic/units-conversion.html

Paul Buckman
Paul Buckman 2019-12-6
Thank you Walter
I do accept the way it works, and I must say that this is an incredible application.
Still getting to grips with its power. Thank you one and all
  1 个评论
Walter Roberson
Walter Roberson 2019-12-7
It is a nuisance that 0 acts this way, and I filed a bug report about it years ago. I do recommend that other people file as well, as Mathworks prioritizes problems that multiple people complain about.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by