Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with a very simple fortran program.

  1. Jan 22, 2012 #1
    Hey, been 6 semesters since I took a fortran class and it's come up again. I'm and engineering major so I don't know what about programming and what not.

    Anyways, I need a program that multiplies an NxN matrix by an N dimensional vector. This is what I have so far:

    Program Matrix


    REAL :: N ! Dimension of the NxN matrix
    REAL :: RandVal !Random values to generate random matrix
    REAL :: MatrixA !The matrix
    REAL :: SEED !Used in generating random numbers


    WRITE (*,*) "Please define the size of your NxN Matrix"
    READ (*,*) N

    LOL, it's not much, but I have never done matrices in fortran before :/. Any insight is greatly appreciated.
  2. jcsd
  3. Jan 28, 2012 #2
    "Do loops".

    Pseudo code:

    Code (Text):

    ! This defines your vector/matrix size.
    parameter (N = 2)

    real MatrixA(N,N)
    real V(N), SolV(N)

    ! Rand stuff
    ! You fill in the blank

    !  Init Matrix
    do j = 1, N
    do i = 1, N
    MatrixA(i,j) = rand()
    end do
    end do

    !  Init Vector
    do i = 1,N
    V(i) = rand()
    end do

    ! Init SolV
    do i = 1,N
    SolV(i) = 0
    end do

    ! Now we solve the thing:
    do j = 1,N
    do i = 1,N
    SolV(i) = SolV(i)+MatrixA(i,j)*V(i)  
    end do
    end do

    print*, 'The solution vector is:  '
    do i = 1,N
    print*, SolV(i)
    end do

    !  Ask yourself:  Does this produce the right answer?
    In a nutshell, that's what the code should roughly look like in fortran77. It's your assignment, therefore, I leave this for you to debug.

    Have a good night. :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook