Question: Write a program that can solve 2x2 and 3x3 systems of equations using Cramers Rule. The main program is written for you! Download Cramer.f95 to
Write a program that can solve 2x2 and 3x3 systems of equations using Cramers Rule. The main program is written for you! Download Cramer.f95 to start your programming and you will also need Data2.txt in the same folder where your program is run. Note that all the main program does is read in the data, call a subroutine that solves the problem, and then prints the results. This is typical in scientific computing. The data file is constructed so that each system of equations is represented in the file by the following set of consecutive lines: a. First line for the system of equations: 1 integer indicating the number of equations in (or the size of) the system. Well denote that number by n. b. The next n lines in the file, each has n+1 real numbers representing the coefficients of the system and the right hand side value. Note that a single file can contain more than one system of equations, each of which is represented by the set of values noted in (a) and (b) above. The end of the file (i.e. the line at which you should stop reading data from the file) is the first line in the file that contains only the digit zero (0). An example of such a file with one system of equations (n=2) is shown below.
Your job is to write the subroutines Cramer, Column Insert, and the function Determinant. You should be able to just copy in your determinant function that you did in class. 1. First, write Column Insert and Determinant. As you work on each one, only concentrate on what that particular subroutine does.
2. First thing in subroutine Cramer, compute the determinant of the matrix (use your function) and print it to the screen, quit the subroutine if the determinant is zero after setting the logical flag for failure that gets returned to my main program.
3. Then write the rest of Cramer that implements Cramers Rule. The 5th system should have no solution. You might or might not have gotten that. Have your program print to the screen the determinant of each original matrix. In addition, examine the output file Data2Out.txt. Based on findings, propose an explanation for why the 5th system should have no solution
Content of the files mentioned above :
Data2.txt:
2
2.0 -3.0 5
1.0 4.0 6
2
2.0 -3.0 5
2.0 -3.0 6
3
1.2 4.3 -2.0 3.1
3.1 1.1 9.4 9.2
-2.0 5.4 6.2 1.1
3
5.2 6.6 0.0 0.0
-4.4 6.2 7.7 0.0
1.1 3.3 9.9 0.0
3
1.2 4.3 -2.0 1.1
3.1 1.1 9.4 2.2
-4.3 -5.4 -7.4 3.3
3
0.0 0.0 -1.0 1.0
0.0 1.0 0.0 2.0
1.0 0.0 0.0 3.0
Cramer.f95 :
program CramersRule
! System of equations. 2x2, 3x3
! The main program is written for you. Read through the comments and
! see how the main program works.
! 2 Special Notes!!!!!
! 1: Take note of how the logial variable 'Success' will either write
! the solution or 'No Solution' to the output file.
! 2: Take note of how inside the do loop, allocating and deallocating
! memory for the arrays Matrix1, b, and x are done so the amount of
! memory allocated changes for each system. You cannot allocate more
! memory for an array until currently allocated memory is deallocated.
implicit none
! Declare varialble
integer :: n, row, col, i
real, allocatable :: Matrix1(:,:), b(:), x(:)
real :: detA, detM, determinant
logical :: Success
! Open the input and output files.
open(42,file='Data2.txt')
open(43,file='Data2Out.txt')
! Solve each system in the input files.
do
! Read in size of first system.
read(42,*) n
if (n .eq. 0) exit ! Quit if zero.
! Allocate memory for system, right hand side, and solution vector.
allocate(Matrix1(n,n), b(n), x(n))
! Read in the system. Ask if you do not understand how this works!
do row = 1, n
read(42,*) (Matrix1(row, col), col = 1, n), b(row)
enddo
! Use cramers rule to get solution.
call Cramer(Matrix1, b, n, x, Success)
if (Success) then
! Write solution to file
do row = 1, n
write(43,*) x(row)
enddo
write(43,*)
else ! This happens when there is no unique solution.
write(43,*) 'No Solution'
write(43,*)
endif
! clean up memory and go back up to top for next system.
deallocate(Matrix1, b, x)
enddo
! close files
close(42)
close(43)
end program CramersRule
subroutine Cramer(M, b, n, x, Success)
! This subroutine does Cramer's Rule
! Declare and initialize your variables first.
! Find the determinant of M first. print it to screen.
! If it is zero, set the Success logical variable and quit.
! Allocate memory for a working matrix for column substituion. Then, for each
! column, i, substitute column i with vector b and get that determinant.
! Compute the ith solution.
! deallocate memory for the working matrix.
end subroutine Cramer
subroutine ColumnInsert(M, b, n, col, MatOut)
! This subroutine takes vector b and inserts in into matrix M at column col.
! Don't forget to set MatOut = M before you substitute the column in.
end subroutine ColumnInsert
function Determinant(M, n) result(Det)
! This function computes the determinant of matices of size 2 or 3. This
! should just be an if...else....endif. One is the formula for a 2x2
! and the other is the formula for the 3x3 determinant.
end function Determinant.
comments : language to be used is Fotran 95 and show output of code too , cotents of data2.txt and cramer.f95 are given
Example: A set of equations (n=2) : The corresponding input data file: 3x+4y=82x+12y=123.02.04.012.08.01.0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
