Why wont symbolic convert a unit^3 or greater?
1 次查看(过去 30 天)
显示 更早的评论
in the code below, all other Units are converted except where a unit power is greater than 2 is there some workaround for this?
u=symunit;
% these are the constants that i am working with for a question, i know that AU is provided by Matlab
Unit1=newUnit('Astrounit',1.495978707E11*u.m);
Unit2=newUnit('Yr',3.15576E7*u.s);
Unit3=newUnit('Solar',1.9885E30*u.kg);
G=6.6743E-11*u.m^3*u.kg^(-1)*u.s^(-2)
findUnits(G)
G_2=rewrite(G,Unit1*Unit2*Unit3)
findUnits(G_2)
% an example with length to the power of 2
G=6.6743E-11*u.m^2*u.kg^(-1)*u.s^(-2)
findUnits(G)
G_2=rewrite(G,Unit1*Unit2*Unit3)
findUnits(G_2)
0 个评论
回答(1 个)
Aiswarya
2024-1-3
编辑:Aiswarya
2024-1-3
Hi Matthew,
I observed the same issue with 'rewrite' and 'unitConvert' (https://www.mathworks.com/help/symbolic/unitconvert.html). Here is a workaround you can use:
You can define a new units system in MATLAB using the function 'newUnitSystem'
Refer to the following code:
u = symunit;
% Modify the SI units to use Astrounit for length, Yr for time and Solar
% for mass
SIUnits = baseUnits('SI'); % Extract the Base Units
newUnits = subs(SIUnits,[u.m u.s u.kg],[Unit1 Unit2 Unit3]); % Substitute the desired units
% Define the new unit system SI_myUnits using the new base units
newUnitSystem('SI_myUnits',newUnits);
% Rewrite G using the new Unit System
G=6.6743E-11*u.m^3*u.kg^(-1)*u.s^(-2);
G_2=rewrite(G,'SI_myUnits');
The answer will be in terms of your defined units only:
G_2 =
(131406052046712958830620234047406505291/3328529041768042103396741079787110400)*([Astrounit]^3/([Solar]*[Yr]^2))
You may also use 'findUnits' to verify the units of 'G_2' :
findUnits(G_2)
ans =
[[Astrounit], [Solar], [Yr]]
This will resolve the issue and it works for all powers greater than or equal to 3.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!