systemSolverTest.frink

Download or view systemSolverTest.frink in plain text format


// Demonstration and test of solving systems of equations.

use systemSolver2.frink
symbolicMode[true]

// Ellipse equations.
ellipse = new System[[x^2/a^2 + y^2/b^2 === 1,
                      e === sqrt[(a^2-b^2)/a^2],
                      area===pi a b],
                     ["pi"]]
println["Ellipse:"]
println[join["\n",ellipse.solveAll[]]]
println[]


println["System of equations with 2 unknowns:"]
simult = new System[[x*y===8,
                     x+y===4]]
println[join["\n",simult.solveAll[]]]
println[]


println["System of equations with 4 unknowns:"]
simult2 = new System[[3a - 2b + c === 3,
                      4a + b - 3c === 10,
                      a + 6b - 2c === 5,
                      d === 4c - 10 + 2b]]
println[join["\n",simult2.solveAll[]]]
println[]


// Rump equation
println["Rump equation:"]
rump = new System[[z === ((333 + 3/4) - x^2) y^6 + x^2 (11x^2 y^2 - 121 y^4 - 2) + (5 + 1/2) y^8 + x/(2y), x===77617, y===33096]]
println[join["\n",rump.solveAll[]]]
println[]

// Cohen, p. 11
/*cc = new System[[d0+d1+d2+d3+d4 === 1,
                 d1+2d2+3d3+4d4 === 2(1-m),
                 3d0-d2+3d4     === 2g20 + g11,
                 theta d0 + phi d1 - phi d3 - theta d4 === m,
                 2 theta d0 + phi d1 + phi d3 + 2 theta d4 === 2 g10],
                 ["g20", "g11", "theta", "phi", "g10", "m"], true]
println[join["\n",cc.solveAll[true]]]
println[] */



// Expansion of the universe
// TODO:  Don't have solvers for exponential functions yet.
println["Expansion of the universe:"]
u = new System[[D0 hubbleconstant^2 e^(hubbleconstant t) === G m / d^2, d === D0,  D0 === 4.3 ly], ["D0", "hubbleconstant", "ly", "G"]]
println[join["\n",u.solveFor["t"]]]
println[]


// Circle
println["Circle:"]
circle = new System[[d === 2 r, c === pi d, a === pi r^2, e===f, g===h], ["pi"]]
println[join["\n",circle.solveAll[]]]
println[]


// See http://answers.yahoo.com/question/index?qid=20091120001614AAInec3
// TODO:
//   eliminate pAprime and pAB, as those are what we want to solve for.
println["Bayes equation:"]
bayes = new System[[pAB === pBA pA / pB,
                    pA === 1 - pAprime,
                    pAB === ( pBA pA ) / ( pBA pA + pBAprime pAprime )],
                   []]
println[join["\n", bayes.solveAll[]]]
println[]


// Cylinder.  See http://ask.metafilter.com/59183/Calculate-length-of-a-pipe-from-the-volume-it-holds
// Basically, we want the solution for "L" given "d" and "v".
println["Cylinder:"]
cyl = new System[[d === 2 r, c === pi d, a === pi r ^2, v === a L], ["pi"]]
println[join["\n", cyl.solveAll[]]]
sols = cyl.solveFor["L"]
println["Solutions for L:\n" + join["\n", sols]]


// Plug in values and solve.
args = [["d", 3/4 inch], ["v", 27 gallons]]
nsols = cyl.solveForValues["L", args]
println["Solutions for L (fully specified, symbolic):\n" + (nsols->"ft")]
println["Solutions for L (evaluated):\n" + (eval[nsols]->"ft")]
println[]

// Not fully specified case.
args = [["d", 3/4 inch]]
nsols = cyl.solveForValues["L", args, false]
println["Solutions for L (not fully specified):\n" + join["\n",nsols]]
println[]

// Phi
println["The golden ratio Phi:"]
phi = new System[[x+1===1/x]]
sols = phi.solveAll[]
println[join["\n", sols]]
println[]


// Test of parsing user-generated systems of equations.  This will generally
// fail with eval because of security constraints on creating objects.
// Thus we're using unsafeEval.  Hopefully in the future this can be done in
// such a way that we can solve user-submitted systems of equations.  Or we
// may just have to do that in an applet or in the Frink UI.  Burn your CPU
// cycles, not mine!
println["User-submitted system:"]
s = unsafeEval["new System[[density === 3.5 oz / (170 yards),v === density], []]"]
println[join["\n", s.solveFor["v"]]]
println[]


// Shweeb collision
println["Shweeb collision"]
phi = new System[[v === a t,
                  d === 1/2 a t^2,
                  E === 1/2 m v^2,
                  E === m gravity h],
                 ["gravity"]]
println[join["\n", phi.solveAll[]]]
args = [["d", 1.2 m], ["v", 30 km/hr], ["gravity", gee]]
println["Acceleration: " + (eval[phi.solveForValues["a", args]]->"gee")]
println["Height: " + (eval[phi.solveForValues["h", args]]->"ft")]
println["Collision time:" + eval[phi.solveForValues["t", args]]]
println[]


// Physical system // Suppose the stone is thrown at an angle of 35.0° below
// the horizontal from the same building (h = 50.0 m) as in the example above.
// If it strikes the ground 60.8 m away, find the following. (Hint: For part
// (a), use the equation for the x-displacement to eliminate v0t from the
// equation for the y-displacement.)
// (a) the time of flight
// (b) the initial speed
// (c) the speed and angle of the velocity vector with respect to the
//    horizontal
println["Ballistics problem:"]
phys = new System[[vx === v0 cos[35 degrees],
                   vy0 === v0 sin[35 degrees],
                   h === h0 - 1/2 g t^2 - vy0 t,
                   x === vx t],
                  ["g", "degrees"]]
println[join["\n", phys.solveAll[]]]
args = [["x", 60.8 m], ["h0", 50 m], ["g", gravity], ["h", 0 m]]
println[join["\n", eval[phys.solveForValues["v0", args]]]]
println[]

// Jupiter gravity
println["Jupiter gravity"]
jup = new System[[ E === m g h,
                   E === 1/2 m v^2],
                  ["g"]]
println[join["\n", jup.solveAll[]]]
println[]


// Lorentz equation
println["Lorentz equation:"]
lorentz = new System[[ gamma === 1/sqrt[1-v^2/c^2],
                       dprime === d / gamma ],
                       ["c"]]
println[join["\n", lorentz.solveAll[]]]

// Solve for when a distance of 5 km is compressed into 1 m
args=[["d", 5 km], ["dprime", 1 m]]
sols = lorentz.solveForValues["v", args]
println[join["\n", sols]]
println["Evaluated: " + eval[sols]]
println[]


// Photon travel from quantum electrodynamic calculations
println["Photon equations:"]
light = new System[[ f === c / lambda,
                     omega === 2 pi f,
                     f === 1/T,
                     d === v t,
                     v === c,
                     phase === omega t],
                     ["c", "pi"]]
println[join["\n", light.solveAll[false]]]
println[]


// Deep fryer.  How much oil do I need to buy to fill a cylinder to the
// specified height?
println["Deep fryer:"]
fryer = new System[[ a === pi r^2,
                     v === a h,
                     d === 2 r ],
                   ["pi"]]
println[join["\n", fryer.solveAll[]]]
args = [["d", 10.5 in], ["h", 7.5 in]]
println[eval[fryer.solveForValues["v",args]] -> "gallons"]
println[]


Download or view systemSolverTest.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20217 days, 23 hours, 51 minutes ago.