Why wont symbolic convert a unit^3 or greater?

조회 수: 9 (최근 30일)
Matthew
Matthew 2023년 10월 21일
편집: Aiswarya 2024년 1월 3일
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)
G = 
findUnits(G)
ans = 
G_2=rewrite(G,Unit1*Unit2*Unit3)
G_2 = 
findUnits(G_2)
ans = 
% an example with length to the power of 2
G=6.6743E-11*u.m^2*u.kg^(-1)*u.s^(-2)
G = 
findUnits(G)
ans = 
G_2=rewrite(G,Unit1*Unit2*Unit3)
G_2 = 
findUnits(G_2)
ans = 

답변 (1개)

Aiswarya
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.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by