usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

Solving 3x3 Simultaneous Equations in Perl | Elimination Method Algorithm



Solving Simultaneous Equations in Perl: A Junior Secondary Guide

Welcome to this junior secondary Perl math project! In this tutorial, you'll learn how to solve simultaneous equations with three unknowns using Perl. This is a great way to combine your coding skills with algebra and logic.
You'll also learn the following:

  • How to use Perl to solve 3x3 simultaneous equations
  • Applying the elimination method step-by-step
  • Using LCM (Least Common Multiple) to simplify equations
  • Writing a Perl class to automate the solving process
Solving equations is a key part of algebra. By coding the solution in Perl, you'll not only understand the math better; you'll also build a useful tool. This project is perfect for students looking to explore Perl math algorithms, or teachers seeking Perl algebra exercises for the classroom.


How to Solve Three-Variable Algebra Problems | Maths Explanation for Perl Kids

To solve 3 by 3 simultaneous equations, we will simply eliminate the z variable, then call out to our Perl Code for Simultaneous Equations with 2 Unknowns module.



Step-by-Step Guide to Solve Three-Variable Algebra Equations | Elimination Method Perl Algorithm

Let's try to draft a Perl algorithm that solves simultaneous equations with 2 unknowns, using the elimination method, with the following set of equations in consideration.
         x + 2y - z = 2; and
         3x - y + 2z = 4
         2x + 3y + 4z = 9
These steps will help the student understand both the math and the logic behind the code.

Step 1:

Using the Find LCM in Perl class from the Primary Category, find the LCM of the coefficients of variable z. Multiply equations 1, 2 & 3 by the LCM of the coefficients of variable z, divided by the z coefficient of the respective equation.
         (4/-1) X (x + 2y - z = 2)
    ⇒     -4x - 8y + 4z = -8
         (4/2) X (3x - y + 2z = 4)
    ⇒     6x - 2y + 4z = 8
         (4/4) X (2x + 3y + 4z = 9)
    ⇒     2x + 3y + 4z = 9

Step 2:

Subtract the new equations obtained in Step 2; eqn (2) from eqn (1) and eqn (3) from eqn (2).
         -4x - 8y + 4z = -8
    -     6x - 2y + 4z = 8
    ⇒     -10x - 6y = -16
         6x - 2y + 4z = 8
    -     2x + 3y + 4z = 9     ⇒     4x - 5y = -1

Step 3:

Call out to our Perl Code for Simultaneous Equations with 2 Unknowns module to solve for x and y.
⇒         (x, y) = (1, 1);

Step 4:

Obtain z by solving for z from any of the original equations, using the found values of x and y.
         x + 2y - z = 2
⇒         1 + 2(1) - z = 2;
⇒         -z = 2 - 3 = -1;
⇒         z = -1/-1 = 1;


Create a new Perl module file; call it Simultaneous3Unknown.pm.
Type out the adjoining Perl code for solving simultaneous equations with 3 unknowns.


Note: The code module for finding LCM in Perl has been explained in the Primary Category.

You can comment out the Simultaneous2Unknown Perl object code in the main class from the previous lesson or simply continue from where it stopped.


So! Perl Fun Practice Exercise - Simultaneous Equations with 3 Unknowns

As a fun practice exercise, feel free to try out your own set of x_coefficients, y_coefficients and equals values, and see how the Perl code solves the resulting 3x3 Simultaneous Equations.







Perl Code for Solving Simultaneous Equations with 3 Unknowns - Module File

package SIMULTANEOUS3UNKNOWN;

BEGIN {
    require Exporter;

    # for the sake of standard
    our $VERSION = 2016.12;

    # Inherit from exporter to export functions and variables
    our @ISA = qw(Exporter);

    # Functions and variables to be exported by default
    our @EXPORT_OK = qw(solveSimultaneous);
}

use warnings;
use strict;
use Carp "croak";

my ($x_variable$y_variable$z_variable$partial_solution);
my (@x_coefficients@y_coefficients@z_coefficients@equals);
my @eliminator;
my %equations;

# simulate an object construct
# takes one argument  -- besides its package name;
# hash reference to array references of the variables
sub new {
    no warnings "all";
    
    my $this = shift;
    my $parameters = shift#this is already a hash reference
    bless $parameters$this;
    $this->_init($parameters);
    return $this;
}

# Simulate a constructor
sub _init {
    my $self = shift;
    my $aux = shift;
    
    $equations{x}    = $aux->{x};
    $equations{y}    = $aux->{y};
    $equations{z}    = $aux->{z};
    $equations{eq}    = $aux->{eq};
    @x_coefficients    = @{$equations{x}};
    @y_coefficients    = @{$equations{y}};
    @z_coefficients    = @{$equations{z}};
    @equals            = @{$equations{eq}};
}

# Returns an array of the result
sub solveSimultaneous {
    use LCM;
    my $lcm = LCM->new(\@z_coefficients);
    $lcm = $lcm->getLCM();

    # STEP 1:
    $eliminator[0][0] = ($lcm * $x_coefficients[0]) / $z_coefficients[0];
    $eliminator[0][1] = ($lcm * $y_coefficients[0]) / $z_coefficients[0];
    $eliminator[0][2] = ($lcm * $equals[0])            / $z_coefficients[0];

    $eliminator[1][0] = ($lcm * $x_coefficients[1]) / $z_coefficients[1];
    $eliminator[1][1] = ($lcm * $y_coefficients[1]) / $z_coefficients[1];
    $eliminator[1][2] = ($lcm * $equals[1])            / $z_coefficients[1];

    $eliminator[2][0] = ($lcm * $x_coefficients[2]) / $z_coefficients[2];
    $eliminator[2][1] = ($lcm * $y_coefficients[2]) / $z_coefficients[2];
    $eliminator[2][2] = ($lcm * $equals[2])            / $z_coefficients[2];

    # STEP 2:
    my @new_x = (
        $eliminator[0][0] - $eliminator[1][0],
        $eliminator[1][0] - $eliminator[2][0]
    );
    my @new_y = (
        $eliminator[0][1] - $eliminator[1][1],
        $eliminator[1][1] - $eliminator[2][1]
    );
    my @new_eq = (
        $eliminator[0][2] - $eliminator[1][2],
        $eliminator[1][2] - $eliminator[2][2]
    );

    eval {
        # STEP 3
        use SIMULTANEOUS2UNKNOWN;
        my $s2u = SIMULTANEOUS2UNKNOWN->new({x=>\@new_x, y=>\@new_y, eq=>\@new_eq});
        $partial_solution = $s2u->solveSimultaneous();

        $x_variable = $partial_solution->[0];
        $y_variable = $partial_solution->[1];
        # STEP 4:
        $z_variable = ($equals[0] - $x_coefficients[0] * $x_variable - 
            $y_coefficients[0] * $y_variable) / $z_coefficients[0];
            
        return [$x_variable$y_variable$z_variable];
    } or croak "Error $@ happenned!";
}


1;


Perl Code for Solving Simultaneous Equations with 3 Unknowns - Main Class

#!/usr/bin/perl;
use strict;
use warnings;
use SIMULTANEOUS3UNKNOWN;

# Useful variables
my $solution;

my (@x_coefficients@y_coefficients@z_coefficients);
my (@operators@equals);

##
 # Simultaneous Equations with 3 unknowns
 ##

@x_coefficients = (242);
@y_coefficients = (1, -13);
@z_coefficients = (1, -2, -8);
@equals            = (41, -3);

@operators = ();
for (0 .. 2) {
    $operators[$_][0] = '+';
    $operators[$_][0] = '-' if $y_coefficients[$_] < 0;
    $operators[$_][1] = '+';
    $operators[$_][1] = '-' if $z_coefficients[$_] < 0;
}

print "\n    Solving simultaneously the equations:\n";
#Print as an equation
printf(
    "%40dx %s %dy %s %dz = %d\n", $x_coefficients[0], $operators[0][0],
    abs($y_coefficients[0]), $operators[0][1], abs($z_coefficients[0]), $equals[0]
);
printf(
    "%40dx %s %dy %s %dz = %d\n", $x_coefficients[1], $operators[1][0],
    abs($y_coefficients[1]), $operators[1][1], abs($z_coefficients[1]), $equals[1]
);
printf(
    "%40dx %s %dy %s %dz = %d\n", $x_coefficients[2], $operators[2][0],
    abs($y_coefficients[2]), $operators[2][1], abs($z_coefficients[2]), $equals[2]
);
printf("\n%30s\n%40s""Yields:""(x, y, z)  =  ");

eval {
    my $sim3unk = SIMULTANEOUS3UNKNOWN->new({
        x    => \@x_coefficients,
        y    => \@y_coefficients,
        z    => \@z_coefficients,
        eq    => \@equals
    });
    $solution = $sim3unk->solveSimultaneous();

    printf("(%.4f, %.4f, %.4f)\n", $solution->[0], $solution->[1], $solution->[2]);

} or printf("(%s, %s, %s)\n""infinity""infinity""infinity");


print "n\n";



<< Previous Next >>