Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Last active March 4, 2022 06:41
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/d6d499f14c808b159689 to your computer and use it in GitHub Desktop.
Save dharmatech/d6d499f14c808b159689 to your computer and use it in GitHub Desktop.

The problem to solve

An example problem from the textbook Physics for Scientists and Engineers by Serway and Jewett:

Let's solve this using Symbolism, a C# library for computer algebra and symbolic computation.

Symbol definitions

We'll call A the initial location. C is the final location. B is the location after half the time has elapsed.

sAC is the distance between A and C.

var sAC = new Symbol("sAC");

Let's also define sAB:

var sAB = new Symbol("sAB");

vA is the velocity at A.

var vA = new Symbol("vA");

Let's also define vB and vC:

var vB = new Symbol("vB");
var vC = new Symbol("vC");

a is acceleration:

var a = new Symbol("a");

Time between A and C:

var tAC = new Symbol("tAC");

And tAB:

var tAB = new Symbol("tAB");

Equations

Two basic equations for kinematics are:

v == u + a * t

s == (u + v) * t / 2

Where

s is displacement
u is initial velocity
v is final velocity
a is acceleration
t is time elapsed

Let's define a function Kinematic to return those equations populated with particular symbols:

static List<Equation> Kinematic(Symbol s, Symbol u, Symbol v, Symbol a, Symbol t)
{
    return new List<Equation>()
    {
        v == u + a * t,
        s == (u + v) * t / 2
    };
}

Our set of equations for this problem includes the kinematic equations for the time between A and C as well as the equations for the time between A and B. We'll also add one to relate tAB and tAC. Let's build up this list of equations in an And expression. This is a way of saying "all these equations are true".

var eqs = new And(tAB == tAC / 2);

eqs.args.AddRange(Kinematic(sAC, vA, vC, a, tAC));
eqs.args.AddRange(Kinematic(sAB, vA, vB, a, tAB));

The known variables are the initial velocity, final velocity, and time elapsed between A and C:

var vals = new List<Equation>() { vA == 10, vC == 30, tAC == 10 };

What is the acceleration?

Part a of the problem asks for the acceleration. We'd like the answer in terms of quantities that we know (vals) so let's eliminate the unknown quantities, isolate a and display the result:

eqs
    .EliminateVariables(tAB, sAC, vB, sAB)
    .IsolateVariable(a)
    .Disp()

The result as shown on the console:

Let's take that symbolic result and substitute the known numeric values:

eqs
    .EliminateVariables(tAB, sAC, vB, sAB)
    .IsolateVariable(a)
    .SubstituteEqLs(vals)
    .Disp();

Console output:

How far did it go after half the time elapsed?

Now we want to know sAB, so let's not eliminate that symbol. We successfully solved for a so we can eliminate that:

eqs
    .EliminateVariables(vB, a, tAB, sAC)
    .Disp()

Console output:

The numeric solution:

eqs
    .EliminateVariables(vB, a, tAB, sAC)
    .SubstituteEqLs(vals)
    .Disp();

Console output:

This example is used in one of the unit tests for Symbolism.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment