Sunday, May 22, 2011

Perkalian matriks dengan PHP

Nih, sekedar share aja... Program PHP sederhana untuk menghitung matriks odro 3x3... Silakan mencoba.
Oiya sekedar mengingatkan, ini file dibagi menjadi dua file .php. Yang pertama untuk menginputkan matriks, yang kedua untuk menghitung perkalian matriks. Kalau gagal dalam mengcompile, pada tanda petik itu hapus dan ganting dengan tanda petik yang baru.

Untuk file pertama:

<html>
<head>
<title>Perkalian Matriks</title>
</head>
<body>
    <h2>Perkalian Matriks Ordo 3x3</h2>
    <P>By : Vera Suryaningsih M0509074</p>
    <P>Kunjungi spirit221.blogspot.com</p>
    <form method="POST" action="rumus.php">
        Inputkan nilai matriks pertama:<BR>
        <INPUT TYPE = "text" NAME = "A11">
        <INPUT TYPE = "text" NAME = "A12">
        <INPUT TYPE = "text" NAME = "A13"><BR>
        <INPUT TYPE = "text" NAME = "A21">
        <INPUT TYPE = "text" NAME = "A22">
        <INPUT TYPE = "text" NAME = "A23"><BR>
        <INPUT TYPE = "text" NAME = "A31">
        <INPUT TYPE = "text" NAME = "A32">
        <INPUT TYPE = "text" NAME = "A33"><BR>
        Inputkan nilai matriks kedua:<BR>
        <INPUT TYPE = "text" NAME = "B11">
        <INPUT TYPE = "text" NAME = "B12">
        <INPUT TYPE = "text" NAME = "B13"><BR>
        <INPUT TYPE = "text" NAME = "B21">
        <INPUT TYPE = "text" NAME = "B22">
        <INPUT TYPE = "text" NAME = "B23"><BR>
        <INPUT TYPE = "text" NAME = "B31">
        <INPUT TYPE = "text" NAME = "B32">
        <INPUT TYPE = "text" NAME = "B33"><BR>
        <INPUT TYPE="submit" VALUE="hitung" NAME="submit">
    </form>
</body>
</html>

Untuk file kedua: (beri nama rumus.php)
 <html>
<head>
<title>Perkalian Matriks</title>
</head>
<body>
    <h2 align="center">Perkalian Matriks Ordo 3x3</h2>
    <P>Kunjungi spirit221.blogspot.com</p>
    <?php
    $A = array();
    $A[0] = $_POST['A11'];
    $A[1] = $_POST['A12'];
    $A[2] = $_POST['A13'];
    $A[3] = $_POST['A21'];
    $A[4] = $_POST['A22'];
    $A[5] = $_POST['A23'];
    $A[6] = $_POST['A31'];
    $A[7] = $_POST['A32'];
    $A[8] = $_POST['A33'];

    $B = array();
    $B[0] = $_POST['B11'];
    $B[1] = $_POST['B12'];
    $B[2] = $_POST['B13'];
    $B[3] = $_POST['B21'];
    $B[4] = $_POST['B22'];
    $B[5] = $_POST['B23'];
    $B[6] = $_POST['B31'];
    $B[7] = $_POST['B32'];
    $B[8] = $_POST['B33'];

    $C = array();
    $C[0] = ($A[0]*$B[0])+($A[1]*$B[3])+($A[2]*$B[6]);
    $C[1] = ($A[0]*$B[1])+($A[1]*$B[4])+($A[2]*$B[7]);
    $C[2] = ($A[0]*$B[2])+($A[1]*$B[5])+($A[2]*$B[8]);
    $C[3] = ($A[3]*$B[0])+($A[4]*$B[3])+($A[5]*$B[6]);
    $C[4] = ($A[3]*$B[1])+($A[4]*$B[4])+($A[5]*$B[7]);
    $C[5] = ($A[3]*$B[2])+($A[4]*$B[5])+($A[5]*$B[8]);
    $C[6] = ($A[6]*$B[0])+($A[7]*$B[3])+($A[8]*$B[6]);
    $C[7] = ($A[6]*$B[1])+($A[7]*$B[4])+($A[8]*$B[7]);
    $C[8] = ($A[6]*$B[2])+($A[7]*$B[5])+($A[8]*$B[8]);
    ?>

    <table border="1" align="center" width="200" >
    <?php
    echo "<tr>";
    for($k=0; $k<=2; $k+=1)
    {
        echo "<td>$C[$k]</td>";
    }
    echo "</tr><tr>";
    for($k=3; $k<=5; $k+=1)
    {
         echo "<td>$C[$k]</td>";
    }
    echo "</tr><tr>";
    for($k=6; $k<=8; $k+=1)
    {
         echo "<td>$C[$k]</td>";
    }
    echo "</tr>";
    ?>
    </table>
</body>
</html>
Nih printscreen nya:
Jalankan file pertama


4 comments:

  1. Mbak Vera Suryaningsih, terimakasih artikelnya sangat membantu.

    http://petik-ilmu.blogspot.com
    http://www.facebook.com/idrisbaikhati

    ReplyDelete
  2. mantap infonya mba, terima kasih sudah berbagi :)

    ReplyDelete
  3. Notice: Undefined index: A11 in C:\xampp\htdocs\LSP\rumus.php on line 10

    Notice: Undefined index: A12 in C:\xampp\htdocs\LSP\rumus.php on line 11

    Notice: Undefined index: A13 in C:\xampp\htdocs\LSP\rumus.php on line 12

    Notice: Undefined index: A21 in C:\xampp\htdocs\LSP\rumus.php on line 13

    Notice: Undefined index: A22 in C:\xampp\htdocs\LSP\rumus.php on line 14

    Notice: Undefined index: A23 in C:\xampp\htdocs\LSP\rumus.php on line 15

    Notice: Undefined index: A31 in C:\xampp\htdocs\LSP\rumus.php on line 16

    Notice: Undefined index: A32 in C:\xampp\htdocs\LSP\rumus.php on line 17

    Notice: Undefined index: A33 in C:\xampp\htdocs\LSP\rumus.php on line 18

    Notice: Undefined index: B11 in C:\xampp\htdocs\LSP\rumus.php on line 21

    Notice: Undefined index: B12 in C:\xampp\htdocs\LSP\rumus.php on line 22

    Notice: Undefined index: B13 in C:\xampp\htdocs\LSP\rumus.php on line 23

    Notice: Undefined index: B21 in C:\xampp\htdocs\LSP\rumus.php on line 24

    Notice: Undefined index: B22 in C:\xampp\htdocs\LSP\rumus.php on line 25

    Notice: Undefined index: B23 in C:\xampp\htdocs\LSP\rumus.php on line 26

    Notice: Undefined index: B31 in C:\xampp\htdocs\LSP\rumus.php on line 27

    Notice: Undefined index: B32 in C:\xampp\htdocs\LSP\rumus.php on line 28

    Notice: Undefined index: B33 in C:\xampp\htdocs\LSP\rumus.php on line 29
    tuh kenapa padahal petik dah di ganti ,mohon bantuannya!

    ReplyDelete