Clear Filters
Clear Filters

Why wont symbolic convert a unit^3 or greater?

4 views (last 30 days)
Matthew
Matthew on 21 Oct 2023
Edited: Aiswarya on 3 Jan 2024
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 = 

Answers (1)

Aiswarya
Aiswarya on 3 Jan 2024
Edited: Aiswarya on 3 Jan 2024
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.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!