Solve equations with substitution method: Difference between revisions

m
syntax highlighting fixup automation
(added AWK)
m (syntax highlighting fixup automation)
Line 15:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SOLVE_EQUATIONS_WITH_SUBSTITUTION_METHOD.AWK
BEGIN {
Line 37:
printf("x = %g\ny = %g\n",result_x,result_y)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 46:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">arraybase 1
dim firstEquation(3)
firstEquation[1] = 3
Line 74:
 
call getCrossingPoint(firstEquation, secondEquation)
end</langsyntaxhighlight>
{{out}}
<pre>
Line 81:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Dim Shared As Integer firstEquation(1 To 3) = { 3, 1, -1}
Dim Shared As Integer secondEquation(1 To 3) = { 2,-3,-19}
 
Line 102:
 
getCrossingPoint(firstEquation(), secondEquation())
Sleep</langsyntaxhighlight>
{{out}}
<pre>x = -2
Line 111:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight QBasiclang="qbasic">DIM firstEquation(3)
firstEquation(1) = 3
firstEquation(2) = 1
Line 138:
PRINT "x = "; resultX
PRINT "y = "; resultY
END SUB</langsyntaxhighlight>
{{out}}
<pre>
Line 147:
{{works with|QBasic}}
{{trans|QBasic}}
<langsyntaxhighlight lang="qbasic">DIM firstequation(3)
LET firstequation(1) = 3
LET firstequation(2) = 1
Line 176:
 
CALL getcrossingpoint (firstequation(), secondequation())
END</langsyntaxhighlight>
{{out}}
<pre>
Line 184:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">dim firstEquation(3)
firstEquation(1) = 3
firstEquation(2) = 1
Line 211:
 
getCrossingPoint(firstEquation(), secondEquation())
end</langsyntaxhighlight>
{{out}}
<pre>
Line 218:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function parselinear(s)
ab, c = strip.(split(s, "="))
a, by = strip.(split(ab, "x"))
Line 236:
 
@show solvetwolinear("3x + y = -1", "2x - 3y = -19") # solvetwolinear("3x + y = -1", "2x - 3y = -19") = (-2.0, 5.0)
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 258:
}
 
say my $result = join ' ', solve( parse('3x + y = -1'), parse('2x - 3y = -19') );</langsyntaxhighlight>
{{out}}
<pre>-2 5</pre>
Line 264:
=={{header|Phix}}==
Slightly modified copy of solveN() from [[Solving_coin_problems#Phix]], admittedly a tad overkill for this task, as it takes any number of rules and any number of variables.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">unknowns</span><span style="color: #0000FF;">)</span>
Line 321:
<span style="color: #000080;font-style:italic;">--for 3x + y = -1 and 2x - 3y = -19:</span>
<span style="color: #000000;">solve</span><span style="color: #0000FF;">({{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}},{</span><span style="color: #008000;">"x"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"y"</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 328:
Alternatively, since I'm staring right at it, here's a
{{trans|Raku}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">solve2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">e1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e2</span><span style="color: #0000FF;">)</span>
Line 339:
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"x = %d, y = %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve2</span><span style="color: #0000FF;">({</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">19</span><span style="color: #0000FF;">}))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 347:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/python
 
firstEquation = [ 3, 1, -1]
Line 370:
 
if __name__ == "__main__":
getCrossingPoint(firstEquation, secondEquation)</langsyntaxhighlight>
{{out}}
<pre>x = -2.0
Line 377:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub solve-system-of-two-linear-equations ( [ \a1, \b1, \c1 ], [ \a2, \b2, \c2 ] ) {
my \X = ( b2 * c1 - b1 * c2 )
/ ( b2 * a1 - b1 * a2 );
Line 385:
return X, Y;
}
say solve-system-of-two-linear-equations( (3,1,-1), (2,-3,-19) );</langsyntaxhighlight>
{{out}}
<pre>(-2 5)</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
firstEquation = [3.0,1.0,-1.0] secondEquation = [2.0,-3.0,-19.0]
getCrossingPoint(firstEquation,secondEquation)
Line 400:
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
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 408:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var solve = Fn.new { |e1, e2|
e2 = e2.toList
for (i in 1..2) e2[i] = e2[i] * e1[0] / e2[0]
Line 419:
var e2 = [2, -3, -19]
var sol = solve.call(e1, e2)
System.print("x = %(sol[0]), y = %(sol[1])")</langsyntaxhighlight>
 
{{out}}
Line 428:
=={{header|XPL0}}==
This shows the vector routines from xpllib.xpl.
<langsyntaxhighlight XPL0lang="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
Line 458:
Text(0, "x = "); RlOut(0, X); CrLf(0);
Text(0, "y = "); RlOut(0, Y); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits