Solve equations with substitution method: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) (added AWK) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15: | Line 15: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SOLVE_EQUATIONS_WITH_SUBSTITUTION_METHOD.AWK |
# syntax: GAWK -f SOLVE_EQUATIONS_WITH_SUBSTITUTION_METHOD.AWK |
||
BEGIN { |
BEGIN { |
||
Line 37: | Line 37: | ||
printf("x = %g\ny = %g\n",result_x,result_y) |
printf("x = %g\ny = %g\n",result_x,result_y) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 46: | Line 46: | ||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="basic256">arraybase 1 |
||
dim firstEquation(3) |
dim firstEquation(3) |
||
firstEquation[1] = 3 |
firstEquation[1] = 3 |
||
Line 74: | Line 74: | ||
call getCrossingPoint(firstEquation, secondEquation) |
call getCrossingPoint(firstEquation, secondEquation) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 81: | Line 81: | ||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
< |
<syntaxhighlight lang="freebasic">Dim Shared As Integer firstEquation(1 To 3) = { 3, 1, -1} |
||
Dim Shared As Integer secondEquation(1 To 3) = { 2,-3,-19} |
Dim Shared As Integer secondEquation(1 To 3) = { 2,-3,-19} |
||
Line 102: | Line 102: | ||
getCrossingPoint(firstEquation(), secondEquation()) |
getCrossingPoint(firstEquation(), secondEquation()) |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>x = -2 |
<pre>x = -2 |
||
Line 111: | Line 111: | ||
{{works with|QuickBasic}} |
{{works with|QuickBasic}} |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="qbasic">DIM firstEquation(3) |
||
firstEquation(1) = 3 |
firstEquation(1) = 3 |
||
firstEquation(2) = 1 |
firstEquation(2) = 1 |
||
Line 138: | Line 138: | ||
PRINT "x = "; resultX |
PRINT "x = "; resultX |
||
PRINT "y = "; resultY |
PRINT "y = "; resultY |
||
END SUB</ |
END SUB</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 147: | Line 147: | ||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
{{trans|QBasic}} |
{{trans|QBasic}} |
||
< |
<syntaxhighlight lang="qbasic">DIM firstequation(3) |
||
LET firstequation(1) = 3 |
LET firstequation(1) = 3 |
||
LET firstequation(2) = 1 |
LET firstequation(2) = 1 |
||
Line 176: | Line 176: | ||
CALL getcrossingpoint (firstequation(), secondequation()) |
CALL getcrossingpoint (firstequation(), secondequation()) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 184: | Line 184: | ||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="yabasic">dim firstEquation(3) |
||
firstEquation(1) = 3 |
firstEquation(1) = 3 |
||
firstEquation(2) = 1 |
firstEquation(2) = 1 |
||
Line 211: | Line 211: | ||
getCrossingPoint(firstEquation(), secondEquation()) |
getCrossingPoint(firstEquation(), secondEquation()) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 218: | Line 218: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">function parselinear(s) |
||
ab, c = strip.(split(s, "=")) |
ab, c = strip.(split(s, "=")) |
||
a, by = strip.(split(ab, "x")) |
a, by = strip.(split(ab, "x")) |
||
Line 236: | Line 236: | ||
@show solvetwolinear("3x + y = -1", "2x - 3y = -19") # solvetwolinear("3x + y = -1", "2x - 3y = -19") = (-2.0, 5.0) |
@show solvetwolinear("3x + y = -1", "2x - 3y = -19") # solvetwolinear("3x + y = -1", "2x - 3y = -19") = (-2.0, 5.0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 258: | Line 258: | ||
} |
} |
||
say my $result = join ' ', solve( parse('3x + y = -1'), parse('2x - 3y = -19') );</ |
say my $result = join ' ', solve( parse('3x + y = -1'), parse('2x - 3y = -19') );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>-2 5</pre> |
<pre>-2 5</pre> |
||
Line 264: | Line 264: | ||
=={{header|Phix}}== |
=={{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. |
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. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | Line 321: | ||
<span style="color: #000080;font-style:italic;">--for 3x + y = -1 and 2x - 3y = -19:</span> |
<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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 328: | Line 328: | ||
Alternatively, since I'm staring right at it, here's a |
Alternatively, since I'm staring right at it, here's a |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | Line 339: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
<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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 347: | Line 347: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/python |
||
firstEquation = [ 3, 1, -1] |
firstEquation = [ 3, 1, -1] |
||
Line 370: | Line 370: | ||
if __name__ == "__main__": |
if __name__ == "__main__": |
||
getCrossingPoint(firstEquation, secondEquation)</ |
getCrossingPoint(firstEquation, secondEquation)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>x = -2.0 |
<pre>x = -2.0 |
||
Line 377: | Line 377: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>sub solve-system-of-two-linear-equations ( [ \a1, \b1, \c1 ], [ \a2, \b2, \c2 ] ) { |
||
my \X = ( b2 * c1 - b1 * c2 ) |
my \X = ( b2 * c1 - b1 * c2 ) |
||
/ ( b2 * a1 - b1 * a2 ); |
/ ( b2 * a1 - b1 * a2 ); |
||
Line 385: | Line 385: | ||
return X, Y; |
return X, Y; |
||
} |
} |
||
say solve-system-of-two-linear-equations( (3,1,-1), (2,-3,-19) );</ |
say solve-system-of-two-linear-equations( (3,1,-1), (2,-3,-19) );</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(-2 5)</pre> |
<pre>(-2 5)</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
firstEquation = [3.0,1.0,-1.0] secondEquation = [2.0,-3.0,-19.0] |
firstEquation = [3.0,1.0,-1.0] secondEquation = [2.0,-3.0,-19.0] |
||
getCrossingPoint(firstEquation,secondEquation) |
getCrossingPoint(firstEquation,secondEquation) |
||
Line 400: | Line 400: | ||
resultY = ((temp[1]* r2) - (x2 * temp[3])) / ((x2 * temp[2]) + (temp[1]*y2)) resultX = (r1 - (y1*resultY)) / x1 |
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 |
see "x = " + resultX + nl + "y = " + resultY + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 408: | Line 408: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">var solve = Fn.new { |e1, e2| |
||
e2 = e2.toList |
e2 = e2.toList |
||
for (i in 1..2) e2[i] = e2[i] * e1[0] / e2[0] |
for (i in 1..2) e2[i] = e2[i] * e1[0] / e2[0] |
||
Line 419: | Line 419: | ||
var e2 = [2, -3, -19] |
var e2 = [2, -3, -19] |
||
var sol = solve.call(e1, e2) |
var sol = solve.call(e1, e2) |
||
System.print("x = %(sol[0]), y = %(sol[1])")</ |
System.print("x = %(sol[0]), y = %(sol[1])")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 428: | Line 428: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
This shows the vector routines from xpllib.xpl. |
This shows the vector routines from xpllib.xpl. |
||
< |
<syntaxhighlight lang="xpl0">func real VSub(A, B, C); \Subtract two 3D vectors |
||
real A, B, C; \A:= B - C |
real A, B, C; \A:= B - C |
||
[A(0):= B(0) - C(0); \VSub(A, A, C) => A:= A - C |
[A(0):= B(0) - C(0); \VSub(A, A, C) => A:= A - C |
||
Line 458: | Line 458: | ||
Text(0, "x = "); RlOut(0, X); CrLf(0); |
Text(0, "x = "); RlOut(0, X); CrLf(0); |
||
Text(0, "y = "); RlOut(0, Y); CrLf(0); |
Text(0, "y = "); RlOut(0, Y); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 15:32, 28 August 2022
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
AWK
# syntax: GAWK -f SOLVE_EQUATIONS_WITH_SUBSTITUTION_METHOD.AWK
BEGIN {
main("3,1,-1","2,-3,-19")
exit(0)
}
function main(s1,s2, arr,e1,e2,result_x,result_y,r1,r2,x1,x2,y1,y2) {
split(s1,e1,",")
split(s2,e2,",")
x1 = e1[1]
y1 = e1[2]
r1 = e1[3]
x2 = e2[1]
y2 = e2[2]
r2 = e2[3]
arr[1] = x1
arr[2] = -y1
arr[3] = r1
result_y = ((arr[1]*r2) - (x2*arr[3])) / ((x2*arr[2]) + (arr[1]*y2))
result_x = (r1 - (y1*result_y)) / x1
printf("x = %g\ny = %g\n",result_x,result_y)
}
- Output:
x = -2 y = 5
BASIC
BASIC256
arraybase 1
dim firstEquation(3)
firstEquation[1] = 3
firstEquation[2] = 1
firstEquation[3] = -1
dim secondEquation(3)
secondEquation[1] = 2
secondEquation[2] = -3
secondEquation[3] = -19
subroutine getCrossingPoint(firstEquation, secondEquation)
x1 = firstEquation[1]
y1 = firstEquation[2]
r1 = firstEquation[3]
x2 = secondEquation[1]
y2 = secondEquation[2]
r2 = secondEquation[3]
dim temp(3)
temp[1] = x1
temp[2] = -y1
temp[3] = r1
resultY = ((temp[1]*r2) - (x2*temp[3])) / ((x2*temp[2]) + (temp[1]*y2))
resultX = (r1 - (y1*resultY)) / x1
print "x = "; resultX
print "y = "; resultY
end subroutine
call getCrossingPoint(firstEquation, secondEquation)
end
- Output:
Igual que la entrada de FreeBASIC.
FreeBASIC
Dim Shared As Integer firstEquation(1 To 3) = { 3, 1, -1}
Dim Shared As Integer secondEquation(1 To 3) = { 2,-3,-19}
Sub getCrossingPoint(firstEquation() As Integer, secondEquation() As Integer)
Dim As Integer x1 = firstEquation(1)
Dim As Integer y1 = firstEquation(2)
Dim As Integer r1 = firstEquation(3)
Dim As Integer x2 = secondEquation(1)
Dim As Integer y2 = secondEquation(2)
Dim As Integer r2 = secondEquation(3)
Dim As Integer temp(3)
temp(1) = x1
temp(2) = -y1
temp(3) = r1
Dim As Integer resultY = ((temp(1)* r2) - (x2 * temp(3))) / ((x2 * temp(2)) + (temp(1)*y2))
Dim As Integer resultX = (r1 - (y1*resultY)) / x1
Print "x = "; resultX
Print "y = "; resultY
End Sub
getCrossingPoint(firstEquation(), secondEquation())
Sleep
- Output:
x = -2 y = 5
QBasic
DIM firstEquation(3)
firstEquation(1) = 3
firstEquation(2) = 1
firstEquation(3) = -1
DIM secondEquation(3)
secondEquation(1) = 2
secondEquation(2) = -3
secondEquation(3) = -19
CALL getCrossingPoint(firstEquation(), secondEquation())
END
SUB getCrossingPoint (firstEquation(), secondEquation())
x1 = firstEquation(1)
y1 = firstEquation(2)
r1 = firstEquation(3)
x2 = secondEquation(1)
y2 = secondEquation(2)
r2 = secondEquation(3)
DIM temp(3)
temp(1) = x1
temp(2) = -y1
temp(3) = r1
resultY = ((temp(1) * r2) - (x2 * temp(3))) / ((x2 * temp(2)) + (temp(1) * y2))
resultX = (r1 - (y1 * resultY)) / x1
PRINT "x = "; resultX
PRINT "y = "; resultY
END SUB
- Output:
Igual que la entrada de FreeBASIC.
True BASIC
DIM firstequation(3)
LET firstequation(1) = 3
LET firstequation(2) = 1
LET firstequation(3) = -1
DIM secondequation(3)
LET secondequation(1) = 2
LET secondequation(2) = -3
LET secondequation(3) = -19
SUB getcrossingpoint (firstequation(),secondequation())
LET x1 = firstequation(1)
LET y1 = firstequation(2)
LET r1 = firstequation(3)
LET x2 = secondequation(1)
LET y2 = secondequation(2)
LET r2 = secondequation(3)
DIM temp(3)
LET temp(1) = x1
LET temp(2) = -y1
LET temp(3) = r1
LET resulty = ((temp(1)*r2)-(x2*temp(3)))/((x2*temp(2))+(temp(1)*y2))
LET resultx = (r1-(y1*resulty))/x1
PRINT "x = "; resultx
PRINT "y = "; resulty
END SUB
CALL getcrossingpoint (firstequation(), secondequation())
END
- Output:
Igual que la entrada de FreeBASIC.
Yabasic
dim firstEquation(3)
firstEquation(1) = 3
firstEquation(2) = 1
firstEquation(3) = -1
dim secondEquation(3)
secondEquation(1) = 2
secondEquation(2) = -3
secondEquation(3) = -19
sub getCrossingPoint(firstEquation(), secondEquation())
x1 = firstEquation(1)
y1 = firstEquation(2)
r1 = firstEquation(3)
x2 = secondEquation(1)
y2 = secondEquation(2)
r2 = secondEquation(3)
dim temp(3)
temp(1) = x1
temp(2) = -y1
temp(3) = r1
resultY = ((temp(1)*r2) - (x2*temp(3))) / ((x2*temp(2)) + (temp(1)*y2))
resultX = (r1 - (y1*resultY)) / x1
print "x = ", resultX
print "y = ", resultY
end sub
getCrossingPoint(firstEquation(), secondEquation())
end
- Output:
Igual que la entrada de FreeBASIC.
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)
Perl
use strict;
use warnings;
use feature 'say';
sub parse {
my($e) = @_;
$e =~ s/ ([xy])/ 1$1/;
$e =~ s/[ =\+]//g;
split /[xy=]/, $e;
}
sub solve {
my($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 my $result = join ' ', solve( parse('3x + y = -1'), parse('2x - 3y = -19') );
- Output:
-2 5
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.
with javascript_semantics procedure solve(sequence rules, unknowns) -- -- Based on https://mathcs.clarku.edu/~djoyce/ma105/simultaneous.html -- aka the ancient Chinese Jiuzhang suanshu ~100 B.C. (!!) -- -- Example: -- rules = {{18,1,1},{38,1,5}}, ie 18==p+n, 38==p+5*n -- unknowns = {"pennies","nickels"} -- -- In the elimination phase, both p have multipliers of 1, so we can -- ignore those two sq_mul and just do (38=p+5n)-(18=p+n)==>(20=4n). -- Obviously therefore n is 5 and substituting backwards p is 13. -- string res sequence sentences = rules, ri, rj integer l = length(rules), rii, rji rules = deep_copy(rules) for i=1 to l do -- successively eliminate (grow lower left triangle of 0s) ri = rules[i] if length(ri)!=l+1 then ?9/0 end if rii = ri[i+1] if rii=0 then ?9/0 end if for j=i+1 to l do rj = rules[j] rji = rj[i+1] if rji!=0 then rj = sq_sub(sq_mul(rj,rii),sq_mul(ri,rji)) if rj[i+1]!=0 then ?9/0 end if -- (job done) rules[j] = rj end if end for end for for i=l to 1 by -1 do -- then substitute each backwards ri = rules[i] rii = ri[1]/ri[i+1] -- (all else should be 0) rules[i] = sprintf("%s = %d",{unknowns[i],rii}) for j=i-1 to 1 by -1 do rj = rules[j] rji = rj[i+1] if rji!=0 then rules[j] = 0 rj[1] -= rji*rii rj[i+1] = 0 rules[j] = rj end if end for end for res = join(rules,", ") printf(1,"%v ==> %s\n",{sentences,res}) end procedure --for 3x + y = -1 and 2x - 3y = -19: solve({{-1,3,1},{-19,2,-3}},{"x","y"})
- Output:
{{-1,3,1},{-19,2,-3}} ==> x = -2, y = 5
Alternatively, since I'm staring right at it, here's a
with javascript_semantics function solve2(sequence e1,e2) atom {a1,b1,c1} = e1, {a2,b2,c2} = e2, x = (b2*c1 - b1*c2) / (b2*a1 - b1*a2), y = (a1*x - c1)/-b1; return {x, y} end function printf(1,"x = %d, y = %d\n",solve2({3,1,-1},{2,-3,-19}))
- Output:
x = -2, y = 5
Python
#!/usr/bin/python
firstEquation = [ 3, 1, -1]
secondEquation = [ 2,-3,-19]
def getCrossingPoint(firstEquation, secondEquation):
x1 = firstEquation[0]
y1 = firstEquation[1]
r1 = firstEquation[2]
x2 = secondEquation[0]
y2 = secondEquation[1]
r2 = secondEquation[2]
temp = []
temp.append( x1)
temp.append(-y1)
temp.append( r1)
resultY = ((temp[0]*r2) - (x2*temp[2])) / ((x2*temp[1]) + (temp[0]*y2))
resultX = (r1 - (y1*resultY)) / x1
print("x = ", resultX)
print("y = ", resultY)
if __name__ == "__main__":
getCrossingPoint(firstEquation, secondEquation)
- Output:
x = -2.0 y = 5.0
Raku
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) );
- Output:
(-2 5)
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
- Output:
x = -2 y = 5
Wren
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])")
- Output:
x = -2, y = 5
XPL0
This shows the vector routines from xpllib.xpl.
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);
]
- Output:
x = -2.00000 y = 5.00000