systemSolverTest2.frink

View or download systemSolverTest2.frink in plain text format


// Demonstration and test of solving systems of equations.

use Solver.frink
symbolicMode[true]
showApproximations[false]

// Ellipse equations.
c = new Solver[[x^2/a^2 + y^2/b^2 === 1,
                e === sqrt[(a^2-b^2)/a^2],
                area===pi a b],
               ["pi"]]
c.draw[]
println["Ellipse:"]
println[join["\n",c.solveAll[]]]
args = [["x", 10], ["y", 9]]
c.draw[]
println[c.solveForSymbolic["e",args]]
println[]

s = new Solver[[x*y===8, x+y===4]]
println["Simultaneous:"]
println[join["\n",s.solveAll[]]]
println[]
println["x = " + s.solveFor["x",[]]]
println["y = " + s.solveFor["y",[]]]
println[]


t = new Solver[[3a - 2b + c === 3,  4a + b - 3c === 10,  a + 6b - 2c === 5, d === 4c - 10 + 2b]]
//t.draw[]
t.dump[]
println[join["\n",t.solveAll[]]]
println[]

// Rump equation
r = new Solver[[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["Rump:"]
println[join["\n",r.solveAll[]]]
println[]

// Cohen, p. 11
/*cc = new Solver[[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"]]
cc.draw[]
//cc.dump[]
println[join["\n",cc.solveAll[]]]
println[]*/



// Expansion of the universe
u = new Solver[[D0 hubbleconstant^2 e^(hubbleconstant t) === G m / d^2, d === D0,  D0 === 4.3 ly], ["hubbleconstant", "ly", "G"]]
println[join["\n",u.solveFor["t"]]]
println[]


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

// See http://answers.yahoo.com/question/index?qid=20091120001614AAInec3
// solve for pB and pAB
// TODO:
//   eliminate pAprime and pAB, as those are what we want to solve for.
bayes = new Solver[[pAB === pBA pA / pB,
                    pA === 1 - pAprime,
                    pAB === ( pBA pA ) / ( pBA pA + pBAprime pAprime )],
                   []]
//bayes.dump[]
//bayes.draw[]
println["Probability:"]
println[join["\n", bayes.solveAll[]]]
args = [["pA", 0.75], ["pBA", 0.8], ["pBAprime", 0.6]]
println[]
println["Symbolic:"]
println["pB = " + bayes.solveForSymbolic["pB", args]]
println["pAB = " + bayes.solveForSymbolic["pAB", args]]
println[]
println["Numeric:"]
println["pB = " + bayes.solveFor["pB", args]]
println["pAB = " + bayes.solveFor["pAB", args]]
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["Pipe:"]
cyl = new Solver[[d === 2 r,
                  c === pi d,
                  a === pi r^2,
                  v === a L],
                 ["pi"]]
println[join["\n", cyl.solveAll[]]]
println[]

sols = cyl.solveFor["L"]
println["Solutions for L:\n" + join["\n", sols]]
println[]

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

// Plug in values and solve.
args = [["d", 3/4 inch], ["v", 27 gallons]]
nsols = cyl.solveFor["L", args]
println["Solutions for L (fully specified):\n" + join["\n", nsols]]
println[]

// Phi
phi = new Solver[[x+1===1/x]]
println[join["\n", phi.solveAll[]]]
println[]

// Shweeb collision
shweeb = new Solver[[v === a t, d === 1/2 a t^2, E === 1/2 m v^2, E === m gravity h], ["gravity"]]
//shweeb.dump[]
//shweeb.draw[]
println["Shweeb:"]
println[join["\n", shweeb.solveAll[]]]
args = [["d", 1.2 m], ["v", 30 km/hr], ["gravity", gee]]
println[join["\n", array[shweeb.solveFor["a", args]->"gee"]]]
println[join["\n", shweeb.solveFor["h", args]->"ft"]]
println[join["\n", shweeb.solveFor["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
phys = new Solver[[vx === v0 cos[35 degrees],
                   vy0 === v0 sin[35 degrees],
                   h === h0 - 1/2 g t^2 - vy0 t,
                   x === vx t],
                  ["degrees"]]
println["Ballistics:"]
println[join["\n", phys.solveAll[]]]
println[]
args = [["x", 60.8 m], ["h0", 50 m], ["g", gravity], ["h", 0 m]]
println[join["\n", eval[phys.solveFor["t", args]]]]
println[]

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


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

args=[["d", 5 km], ["dprime", 1 m]]
println[join["\n", lorentz.solveForSymbolic["v", args]]]
println[]

println[join["\n", lorentz.solveFor["v", args]]]
println[]


// Photon travel
light = new Solver[[ f === c / lambda,
                     omega === 2 pi f,
                     f === 1/T,
                     d === v t,
                     phase === omega t],
                     ["c", "pi"]]
println["Photon:"]
light.draw[]
println[join["\n", light.solveAll[]]]
println[]

// Deep fryer
fryer = new Solver[[ a === pi r^2,  v === a h, d === 2 r ], ["pi"]]
println["Fryer:"]
println[join["\n", fryer.solveAll[]]]
args = [["d", 10.5 in], ["h", 7.5 in]]
println[fryer.solveFor["v",args]->"gallons"]
println[]

// Not-directly-connected simultaneous.
ndc = new Solver[[a + e === 10, b + e === 20, c + e === 30, d + e===40, b * d === 90],[]]
//ndc.draw[]
println[join["\n", ndc.solveAll[]]]
//ndc.draw[]
println[]


View or download systemSolverTest2.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 17645 days, 8 hours, 34 minutes ago.