Solve equations with substitution method

Revision as of 23:41, 8 November 2021 by Loren (talk | contribs) (Added XPL0 example.)


Let given equations:
3x + y = -1 and 2x - 3y = -19
Solve it with substitution method.

Solve equations with substitution method is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task


See related



Julia

<lang julia>function parselinear(s)

   ab, c = strip.(split(s, "="))
   a, by = strip.(split(ab, "x"))
   b = replace(by, r"[\sy]" => "")
   b[end] in "-+" && (b *= "1")
   b = replace(b, "--" => "")
   return map(x -> parse(Float64, x == "" ? "1" : x), [a, b, c])

end

function solvetwolinear(s1, s2)

   a1, b1, c1 = parselinear(s1)
   a2, b2, c2 = parselinear(s2)
   x = (b2 * c1 - b1 * c2) / (b2 * a1 - b1 * a2)
   y = (a1 * x - c1 ) / -b1
   return x, y

end

@show solvetwolinear("3x + y = -1", "2x - 3y = -19") # solvetwolinear("3x + y = -1", "2x - 3y = -19") = (-2.0, 5.0) </lang>

Raku

<lang perl6>sub solve-system-of-two-linear-equations ( [ \a1, \b1, \c1 ], [ \a2, \b2, \c2 ] ) {

   my \X = ( b2 * c1   -   b1 * c2 )
         / ( b2 * a1   -   b1 * a2 );
   my \Y = ( a1 * X    -   c1 ) / -b1;
   return X, Y;

} say solve-system-of-two-linear-equations( (3,1,-1), (2,-3,-19) );</lang>

Output:
(-2 5)

Ring

<lang ring> firstEquation = [3.0,1.0,-1.0] secondEquation = [2.0,-3.0,-19.0] getCrossingPoint(firstEquation,secondEquation)

func getCrossingPoint(firstEquation,secondEquation)

    x1 = firstEquation[1] y1 = firstEquation[2] r1 = firstEquation[3] x2 = secondEquation[1] y2 = secondEquation[2] r2 = secondEquation[3]
    temp = []
    add(temp,x1) add(temp,-y1) add(temp,r1)
    resultY = ((temp[1]* r2) - (x2 * temp[3])) / ((x2 * temp[2]) + (temp[1]*y2))
    resultX = (r1 - (y1*resultY)) / x1
    see "x = " + resultX + nl + "y = " + resultY + nl

</lang>

Output:
x = -2
y = 5

Wren

<lang ecmascript>var solve = Fn.new { |e1, e2|

   e2 = e2.toList
   for (i in 1..2) e2[i] = e2[i] * e1[0] / e2[0]
   var y = (e2[2] - e1[2]) / (e2[1] - e1[1])
   var x = (e1[2] - e1[1] * y) / e1[0]
   return [x, y]

}

var e1 = [3, 1, -1] var e2 = [2, -3, -19] var sol = solve.call(e1, e2) System.print("x = %(sol[0]), y = %(sol[1])")</lang>

Output:
x = -2, y = 5

XPL0

This shows the vector routines from xpllib.xpl. <lang XPL0>func real VSub(A, B, C); \Subtract two 3D vectors real A, B, C; \A:= B - C [A(0):= B(0) - C(0); \VSub(A, A, C) => A:= A - C

A(1):= B(1) - C(1);
A(2):= B(2) - C(2);

return A; ]; \VSub

func real VMul(A, B, S); \Multiply 3D vector by a scalar real A, B, S; \A:= B * S [A(0):= B(0) * S; \VMul(A, A, S) => A:= A * S

A(1):= B(1) * S;
A(2):= B(2) * S;

return A; ]; \VMul

real E1, E2, X1, X2, X, Y; [E1:= [3., 1., -1.];

E2:= [2., -3., -19.];

X1:= E1(0); X2:= E2(0); VMul(E1, E1, X2); VMul(E2, E2, X1); VSub(E1, E1, E2); Y:= E1(2)/E1(1); E2(1):= E2(1)*Y; E2(2):= E2(2)-E2(1); X:= E2(2)/E2(0); Text(0, "x = "); RlOut(0, X); CrLf(0); Text(0, "y = "); RlOut(0, Y); CrLf(0); ]</lang>

Output:
x =    -2.00000
y =     5.00000