2 by 2 Simultaneous Equations Code in Perl
Well, it has not been much of algebra so far; We are about to have
our first taste of it.
Simultaneous equations can be solved using either
Elimination or Substitution method.
We will be using the Elimination method.
Algorithm Steps to Follow for Simultaneous Equations with 2 unknowns in Perl
Consider the equations:
2x + 3y = 13; and
5x - y = 7
Step 1:
Pick a variable to eliminate, either x or y.
Our code will always eliminate the y variable.
Step 2:
Multiply equation 1 by the coefficient of variable y
in equation 2.
⇒
-1 X (2x + 3y = 13)
⇒
-2x - 3y = -13
Step 3:
Multiply equation 2 by the coefficient of variable y
in equation 1.
⇒
3 X (5x - y = 7)
⇒
15x - 3y = 21
Step 4:
Subtract the new equations obtained from Steps 2 and 3.
-2x - 3y = -13
-
15x - 3y = 21
⇒
-17x = -34
Step 5:
Divide the R.H.S. from Step 4 by the coefficient of
x to obtain x.
⇒
x = -34/-17 = 2;
Step 6:
Obtain y by solving for y from any of the
original equations, using the found value of x.
2x + 3y = 13
⇒
2(2) + 3y = 13;
⇒
3y = 13 - 4 = 9;
⇒
y = 9/3 = 3;
Create a new module file;
call it Simultaneous2Unknown.pm.
Type out the adjoining Perl code for solving simultaneous equations with 2 unknowns.
Note: You can comment out the SortFraction Perl object
code in the main class from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of x_coefficients,
y_coefficients and equals values for 2 by 2 Simultaneous Equations.
Perl code for Simultaneous2Unknown Module File
package SIMULTANEOUS2UNKNOWN;
BEGIN {
require Exporter;
our $VERSION = 2016.12;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(solveSimultaneous);
}
use warnings;
use strict;
use Carp "croak";
my ($x_variable, $y_variable);
my (@x_coefficients, @y_coefficients, @equals);
my @eliminator;
my %equations;
sub new {
no warnings "all";
my $this = shift;
my $parameters = shift;
bless $parameters, $this;
$this->_init($parameters);
return $this;
}
sub _init {
my $self = shift;
my $aux = shift;
$equations{x} = $aux->{x};
$equations{y} = $aux->{y};
$equations{eq} = $aux->{eq};
@x_coefficients = @{$equations{x}};
@y_coefficients = @{$equations{y}};
@equals = @{$equations{eq}};
}
sub solveSimultaneous {
$eliminator[0][0] = $y_coefficients[1] * $x_coefficients[0];
$eliminator[0][1] = $y_coefficients[1] * $equals[0];
$eliminator[1][0] = $y_coefficients[0] * $x_coefficients[1];
$eliminator[1][1] = $y_coefficients[0] * $equals[1];
eval {
$x_variable = ($eliminator[0][1] - $eliminator[1][1]) / ($eliminator[0][0] - $eliminator[1][0]);
$y_variable = ($equals[0] - $x_coefficients[0] * $x_variable) / $y_coefficients[0];
return [$x_variable, $y_variable];
} or croak "Error $@ happened";
}
1;
main class
use strict;
use warnings;
use SIMULTANEOUS2UNKNOWN;
my $solution;
my (@x_coefficients, @y_coefficients);
my (@operators, @equals);
@operators = ('+', '+');
@x_coefficients = (2, 1);
@y_coefficients = (-1, 1);
@equals = (1, 2);
$operators[0] = '-' if $y_coefficients[0] < 0;
$operators[1] = '-' if $y_coefficients[1] < 0;
print "\n Solving simultaneously the equations:\n";
printf("%40dx %s %dy = %d\n", $x_coefficients[0], $operators[0], abs($y_coefficients[0]), $equals[0]);
printf("%40dx %s %dy = %d\n", $x_coefficients[1], $operators[1], abs($y_coefficients[1]), $equals[1]);
printf("\n%30s\n%40s", "Yields:", "(x, y) = ");
eval {
my $sim2unk = SIMULTANEOUS2UNKNOWN->new({
x => \@x_coefficients,
y => \@y_coefficients,
eq => \@equals
});
$solution = $sim2unk->solveSimultaneous();
printf("(%.4f, %.4f)\n", $solution->[0], $solution->[1]);
} or printf("(%s, %s)\n", "infinity", "infinity");
print "\n\n";