Solve equations with substitution method: Difference between revisions
Solve equations with substitution method (view source)
Revision as of 15:32, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Catskill549 (talk | contribs) (added AWK) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15:
=={{header|AWK}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 46:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
dim firstEquation(3)
firstEquation[1] = 3
Line 74:
call getCrossingPoint(firstEquation, secondEquation)
end</
{{out}}
<pre>
Line 81:
==={{header|FreeBASIC}}===
<
Dim Shared As Integer secondEquation(1 To 3) = { 2,-3,-19}
Line 102:
getCrossingPoint(firstEquation(), secondEquation())
Sleep</
{{out}}
<pre>x = -2
Line 111:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<
firstEquation(1) = 3
firstEquation(2) = 1
Line 138:
PRINT "x = "; resultX
PRINT "y = "; resultY
END SUB</
{{out}}
<pre>
Line 147:
{{works with|QBasic}}
{{trans|QBasic}}
<
LET firstequation(1) = 3
LET firstequation(2) = 1
Line 176:
CALL getcrossingpoint (firstequation(), secondequation())
END</
{{out}}
<pre>
Line 184:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<
firstEquation(1) = 3
firstEquation(2) = 1
Line 211:
getCrossingPoint(firstEquation(), secondEquation())
end</
{{out}}
<pre>
Line 218:
=={{header|Julia}}==
<
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>
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 258:
}
say my $result = join ' ', solve( parse('3x + y = -1'), parse('2x - 3y = -19') );</
{{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.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 328:
Alternatively, since I'm staring right at it, here's a
{{trans|Raku}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 347:
=={{header|Python}}==
<
firstEquation = [ 3, 1, -1]
Line 370:
if __name__ == "__main__":
getCrossingPoint(firstEquation, secondEquation)</
{{out}}
<pre>x = -2.0
Line 377:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
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) );</
{{out}}
<pre>(-2 5)</pre>
=={{header|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>
{{out}}
<pre>
Line 408:
=={{header|Wren}}==
<
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])")</
{{out}}
Line 428:
=={{header|XPL0}}==
This shows the vector routines from xpllib.xpl.
<
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);
]</
{{out}}
|