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.
load (ezunits);
a: 30*%pi/180;
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.
masse: 1000 ` 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.
g: 980665/100000 ` m/s^2;
b: 3/10 ` m;
B: 5/10 ` m;
L: 1/10 ` 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.
F_g: masse * g;
F_H: masse * g;
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.
eq1: F_H - 2 * S * sin(a) = 0;
solution1: dimensionally (solve (eq1, S));
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
.
eq2: - F_g + 2 * H;
solution2: dimensionally (solve (eq2, H));
Third equation for Ly
.
eq3: tan(a) = Ly / (B/2);
solution3: dimensionally (solve (eq3, Ly));
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.)
eq4: H * B/2 - FN * (L + Ly) + S * sin(a) * B/2 + S * cos(a) * Ly = 0;
prev_solutions: append (solution1, solution2, solution3);
eq4a: subst (prev_solutions, eq4);
Now we're ready to solve for FN
.
solution4: dimensionally (solve (eq4a, FN));
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.
float (solution4);
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
.
solution4 `` N;
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.