Solve equations with substitution method: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Raku}}: Added Raku solution)
m (add some related (arguably duplicate) tasks)
Line 5: Line 5:
<br>3x + y = -1 and 2x - 3y = -19
<br>3x + y = -1 and 2x - 3y = -19
<br>Solve it with substitution method.
<br>Solve it with substitution method.

<br><br>

;See related

* [[Reduced row echelon form]]
* [[Gaussian elimination]]




=={{header|Raku}}==
=={{header|Raku}}==

Revision as of 19:11, 8 November 2021

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


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


See related


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