• Support PF! Buy your school textbooks, materials and every day products Here!

Multiplication of two 2x2 matrices in Fortran

  • #1

Homework Statement:

Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.

Relevant Equations:

arrays and do loops
I have tried to do this using arrays and do loops:
Fortran:
program matrixmul
    implicit none
    real A(2, 2), B (2, 2), C (2, 2)
    integer i, j, k
    write (*, *) 'Input: First matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) A (i, j)
    enddo
    enddo
    write (*, *) 'Input: Second matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) B (i, j)
    enddo
    enddo
    do i = 1, 2
    do j = 1, 2
    do k = 1, 2
    C (i, j) = C (i, j) + A (i, k) * B (k, j)
    enddo
    enddo
    enddo
    write (*, *) C
    end
So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
 

Answers and Replies

  • #2
526
146
Homework Statement: Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.
Homework Equations: arrays and do loops

I have tried to do this using arrays and do loops:
Fortran:
program matrixmul
    implicit none
    real A(2, 2), B (2, 2), C (2, 2)
    integer i, j, k
    write (*, *) 'Input: First matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) A (i, j)
    enddo
    enddo
    write (*, *) 'Input: Second matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) B (i, j)
    enddo
    enddo
    do i = 1, 2
    do j = 1, 2
    do k = 1, 2
    C (i, j) = C (i, j) + A (i, k) * B (k, j)
    enddo
    enddo
    enddo
    write (*, *) C
    end
So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
Out off topic, you should try to indent your code, it comes in handy for long programs.
 
  • Like
Likes berkeman
  • #3
33,632
5,287
Fortran:
C (i, j) = C (i, j) + A (i, k) * B (k, j)
This line is very likely causing your error.
Before you start your do loops for multiplication, you should initialize the C array to all zero values. Otherwise each value of C(i, j) will be a garbage value, so each assignment statement in the innermost loop just adds a correct value to a garbage value, which results in a different garbage value.

Executing a statement like value = value + x is something you shouldn't do in any programming language if you haven't previously initialized value to some known value.

@archaic's suggestion of indenting each loop body is a good one. This makes your code easier for humans to read.
 
Last edited:
  • Like
Likes berkeman

Related Threads on Multiplication of two 2x2 matrices in Fortran

Replies
16
Views
4K
Replies
4
Views
24K
Replies
2
Views
6K
Replies
3
Views
4K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
7
Views
788
Replies
4
Views
6K
  • Last Post
Replies
16
Views
2K
  • Last Post
Replies
5
Views
3K
Top