Sunday, May 3, 2020

Solving equations with dimensional quantities

Solving equations with dimensional quantities

Here's a basic example of solving equations in which the variables have units such as meters, kilograms, etc. This example is derived from this question at Stackoverflow. Thanks to user Dux for posting the question.

I'll make use of the add-on package ezunits which implements the representation of units, unit conversions, and other operations, one of which is a function which helps other functions, which don't know anything about units, to be able to handle units. This function is named dimensionally and you can use it by just wrapping it around another function. In this example, we'll apply dimensionally to solve.

I'll start by loading ezunits and then defining the problem data.

In [1]:
load (ezunits);
Out[1]:
\[\tag{${\it \%o}_{1}$}\mbox{ /home/robert/maxima/maxima-code/share/ezunits/ezunits.mac }\]
In [2]:
a: 30*%pi/180;
Out[2]:
\[\tag{${\it \%o}_{2}$}\frac{\pi}{6}\]

Units are indicated with a back quote, e.g. 1000 ` kg means 1000 kilograms.

Note that units are displayed in Roman font -- this is a conventional way to present units.

In [3]:
masse: 1000 ` kg;
Out[3]:
\[\tag{${\it \%o}_{3}$}1000\;\mathrm{kg}\]

I'll write decimals as rational numbers because Maxima is much more comfortable with exact numbers (integers and rationals) than with inexact numbers (floats and bigfloats).

A problem here is that within the context of this problem, g is gravitational acceleration at the Earth's surface. However, in other contexts, g might mean grams. One could work around the problem by naming the gravitational acceleration something else, such as %g, but so long as g is used only one way or the other, we won't run into a problem.

In [4]:
g: 980665/100000 ` m/s^2;
Out[4]:
\[\tag{${\it \%o}_{4}$}\frac{196133}{20000}\;\frac{\mathrm{m}}{\mathrm{s}^2}\]
In [5]:
b: 3/10 ` m;
Out[5]:
\[\tag{${\it \%o}_{5}$}\frac{3}{10}\;\mathrm{m}\]
In [6]:
B: 5/10 ` m;
Out[6]:
\[\tag{${\it \%o}_{6}$}\frac{1}{2}\;\mathrm{m}\]
In [7]:
L: 1/10 ` m;
Out[7]:
\[\tag{${\it \%o}_{7}$}\frac{1}{10}\;\mathrm{m}\]

Arithmetic on dimensional quantities such as masse and g here is carried out according to the usual rules for handling units. In this case, when the quantities are multiplied, the units are multiplied too.

Multiplying mass times an acceleration yields a force. In the Systeme Internationale (metric system), force is measured in newtons, so the result of masse * g could be expressed in newtons. However, ezunits doesn't have a preferred set of units, so it won't convert to newtons automatically. We'll carry out the conversion by hand later on.

In [8]:
F_g: masse * g;
Out[8]:
\[\tag{${\it \%o}_{8}$}\frac{196133}{20}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2}\]
In [9]:
F_H: masse * g;
Out[9]:
\[\tag{${\it \%o}_{9}$}\frac{196133}{20}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2}\]

Now we're ready to start solving equations. I'll write out each equation so that we can take a look at it, and then solve it. I'll wrap solve inside of dimensionally to help solve handle the units.

In [10]:
eq1: F_H - 2 * S * sin(a) = 0;
Out[10]:
\[\tag{${\it \%o}_{10}$}\frac{196133}{20}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2}-S=0\]
In [11]:
solution1: dimensionally (solve (eq1, S));
Out[11]:
\[\tag{${\it \%o}_{11}$}\left[ S=\frac{196133}{20}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2} \right] \]

So far, so good -- the result for S looks right. (Of course, it's just a simple equation.) Let's try the second equation for H.

In [12]:
eq2: - F_g + 2 * H;
Out[12]:
\[\tag{${\it \%o}_{12}$}2\,H+\left(-\frac{196133}{20}\right)\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2}\]
In [13]:
solution2: dimensionally (solve (eq2, H));
Out[13]:
\[\tag{${\it \%o}_{13}$}\left[ H=\frac{196133}{40}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2} \right] \]

Third equation for Ly.

In [14]:
eq3: tan(a) = Ly / (B/2);
Out[14]:
\[\tag{${\it \%o}_{14}$}\frac{1}{\sqrt{3}}=4\,{\it Ly}\;\frac{1}{\mathrm{m}}\]
In [15]:
solution3: dimensionally (solve (eq3, Ly));
Out[15]:
\[\tag{${\it \%o}_{15}$}\left[ {\it Ly}=\frac{1}{4\,\sqrt{3}}\;\mathrm{m} \right] \]

The fourth and final equation for FN. This one relates FN to other variables we have already solved for. I'll state the equation first, and then substitute the solutions we found already. (There are other ways of going about it, this is one way that works so I'll just keep going like this.)

In [16]:
eq4: H * B/2 - FN * (L + Ly) + S * sin(a) * B/2 + S * cos(a) * Ly = 0;
Out[16]:
\[\tag{${\it \%o}_{16}$}\frac{\sqrt{3}\,{\it Ly}\,S}{2}+\left(\frac{S}{8}+\frac{H}{4}\right)\;\mathrm{m}-{\it FN}\,\left({\it Ly}+\frac{1}{10}\;\mathrm{m}\right)=0\]
In [17]:
prev_solutions: append (solution1, solution2, solution3);
Out[17]:
\[\tag{${\it \%o}_{17}$}\left[ S=\frac{196133}{20}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2} , H=\frac{196133}{40}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2} , {\it Ly}=\frac{1}{4\,\sqrt{3}}\;\mathrm{m} \right] \]
In [18]:
eq4a: subst (prev_solutions, eq4);
Out[18]:
\[\tag{${\it \%o}_{18}$}\left(-\left(\frac{1}{4\,\sqrt{3}}+\frac{1}{10}\right)\,{\it FN}\right)\;\mathrm{m}+\frac{588399}{160}\;\frac{\mathrm{m}^2\,\mathrm{kg}}{\mathrm{s}^2}=0\]

Now we're ready to solve for FN.

In [19]:
solution4: dimensionally (solve (eq4a, FN));
Out[19]:
\[\tag{${\it \%o}_{19}$}\left[ {\it FN}=\frac{196133\,3^{\frac{3}{2}}}{16\,\sqrt{3}+40}\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2} \right] \]

As before, Maxima finds the solution in terms of exact numbers, including the square root of 3. At some point we probably have to go back to decimal numbers, for example if we wanted to tell someone else about the solution. I'll apply the function float to convert the exact numbers to inexact.

In [20]:
float (solution4);
Out[20]:
\[\tag{${\it \%o}_{20}$}\left[ {\it FN}=15050.87322705384\;\frac{\mathrm{m}\,\mathrm{kg}}{\mathrm{s}^2} \right] \]

Finally, let's convert FN to newtons. In ezunits, that's implemented by the double back quote `` operator. Note that the operator distributes over the equal sign -- on the left-hand side, we get FN `` N while on the right we get m*kg/s^2 converted to N.

In [21]:
solution4 `` N;
Out[21]:
\[\tag{${\it \%o}_{21}$}\left[ {\it FN} `` N=\frac{196133\,3^{\frac{3}{2}}}{16\,\sqrt{3}+40}\;\mathrm{N} \right] \]

I'll let that be enough for now. ezunits has other capabilities that I haven't touched on here. If you are interested in ezunits or other aspects of Maxima, I encourage you to bring it up on the Maxima mailing list. Here is the subscription page for the maxima-discuss mailing list.

Thanks for reading and I hope you have a great day.

No comments:

Post a Comment