Math Lab The solution of FORTRAN Mathematica Both Practical & Lab Assignment

Math lab 2 Practical www.nu.edu.bd




National University Honours (Mathematics) Math lab-2 Practical Solutions
Question:  001. Regular Falsi Method F(x )=Cos(x)-xExp(x)

Ans. to the question No. 01



        F(X)=COS(X)-X*EXP(X)
        S=0.1E-5
        N=1
 14     READ*,A,B
        IF(F(A)*F(B).GE.0.0)GOTO 14
 13     PRINT*,N,A,B
        C=(A*F(B)-B*F(A))/(F(B)-F(A))
        IF(F(C).EQ.0.0)GOTO 11
        IF(F(A)*F(C).GT.0.0)THEN
        A=C
        ELSE
        B=C
        END IF
        IF(ABS(A-B).LT.S)GOTO 11
        N=N+1
        GOTO 13
 11     PRINT*,'A Real Root of f(x)=0 is',C
        STOP
        END

Question: 002. Fibonacci Sequence

 Ans. to the question No. 02



        DIMENSION N(30)
        N(1)=1
        N(2)=1
        DO 7 K=3,30
        N(K)=N(K-2)+N(K-1)
    7   CONTINUE
        PRINT 5,N
    5   FORMAT (5(2X,I10))
        END


003. Gaussion Elimination Method

        PARAMETER(N=3)
        DIMENSION A(N,N),B(N),X(N)
       OPEN(UNIT=13,FILE='8(B).IN',STATUS='OLD')
        READ(13,*)((A(I,J),J=1,N),B(I),I=1,N)
        DO 7 K=1,N-1
        P=A(K,K)
        DO 9 I =K+1,N
        F=A(I,K)/P
        DO 11 J=K+1,N
        A(I,J)=A(I,J)-F*A(K,J)
  11    CONTINUE
        B(I)=B(I)-F*A(K,J)
  9     CONTINUE
  7     CONTINUE
        X(N)= B(N)/A(N,N)
        DO 13 K=N-1,1,-1
        SUM=0.0
        DO 15 J=K+1,N
        SUM=SUM+A(K,J)*X(J)
 15     CONTINUE
        X(K)=(B(K)-SUM)/A(K,K)
 13     CONTINUE
        WRITE(6,*) 'The Solutions Are'
        PRINT*,'X=',X(1)
        PRINT*,'Y=',X(2)
        PRINT*,'Z=',X(3)
        END


005. Transpose and Inverse Matrix

        PARAMETER(N=6,M=3)
        DIMENSION A(N,M),AI(M,M)
        DATA AI/1,2,4,2,1,-2,-3,0,5/
        PRINT*,'Matrix A'
        PRINT 20,((AI(I,J),J=1,M),I=1,M)
 20     FORMAT(3X,3F8.2//)
        PRINT*,'Transpose of the Matrix A'
        PRINT 20,((AI(I,J),I=1,M),J=1,M)
        DO I=1,M
        DO J=1,N
        IF(J.LE.M)THEN
        A(I,J)=AI(I,J)
        ELSE
        IF(J.EQ.I+M)THEN
        A(I,J)=1.0
        ELSE
        A(I,J)=0.0
        ENDIF
        ENDIF
        ENDDO
        ENDDO
        WRITE(6,*)
        PRINT*,'AUGMENTED MATRIX IS'
        WRITE(6,*)
        PRINT 10,((A(I,J),J=1,N),I=1,M)
 10     FORMAT (3X,3F6.2//)
        DO 7 I=1,M
        IF(A(I,I).NE.0.0)THEN
        P=A(I,I)
        ELSE
        PRINT*,'PIVOT ELEMENT IS ZERO'
        STOP
        ENDIF
        DO JJ=1,N
        A(I,JJ)=A(I,JJ)/P
        ENDDO
        DO 3 J=1,M
        IF(J.EQ.I)GOTO 3
        COM=A(J,I)
        DO K=1,N
        A(J,K)=A(J,K)-A(I,K)*COM
        ENDDO
 3      CONTINUE
 7      CONTINUE
        WRITE(6,*)
        PRINT*,'AUGMENTED ECHELON MATRIX'
        WRITE(6,*)
        PRINT 11,((A(I,J),J=1,N),I=1,M)
 11     FORMAT(3X,6F8.2//)
        DO I=1,M
        DO J=M+1,N
        K=J-M
        AI(I,K)=A(I,J)
        ENDDO
        ENDDO
        WRITE(6,*)
        PRINT*,'INVERSE OF THE MATRIX'
        PRINT 30,((AI(I,J),J=1,M),I=1,M)
 30     FORMAT(3X,3F8.2//)
        END

006. Simpsons 3 by 8 rule

        PARAMETER (N=12)
        REAL I
        F(X)=1.0/(1.0+X**2)
        A=0.0
        B=3.14159/4
        H=(B-A)/REAL(N)
        SUM=F(A)+F(B)
        X=A
        DO K =1,N-1
        IF (MOD(K,3).EQ.0) THEN
        SUM=SUM+2.0*F(X+REAL(K)*H)
        ELSE
        SUM=SUM+3.0*F(X+REAL(K)*H)
        END IF
        END DO
        I=3.0*SUM*H/8.0
        WRITE(6,10)I
  10    FORMAT(3X,'Value of the integration=', f15.7/)
        print*, 'Actual Value of the integration=',ATAN(3.14158/4)
        END

008. Value of f(x) between -10 to 10increments of 0.5

       PRINT *,'X F(X)'
       WRITE(*,*)
       X=-10.0
 10    IF(X.LT.0.0)F=1.0+X/SQRT(1.0+X**2)
       IF(X.EQ.0.0)F=0.0
       IF(X.GT.0.0)F=1.0-X/SQRT(1.0+X**2)
       Y=F
       PRINT 20,X,Y
 20    FORMAT(2X,2F12.2/)
       X=X+0.5
       IF(X.LE.10.0)GOTO 10
       END


010. Prints Inverse Matrix Only

        DIMENSION A(3,6), AI(3,3)
        DATA AI/1,2,4,2,1,-2,-3,0,5/
        PRINT*,'MATRIX A'
        WRITE(6,*)
        PRINT 20, ((AI(I,J),J=1,3),I=1,3)
  20    FORMAT(3X,3F8.2//)
        DO I=1,3
        DO J=1,6
        IF(J.LE.3)THEN
        A(I,J)=AI(I,J)
        ELSE
        IF(J.EQ.I+3)THEN
        A(I,J)=1.0
        ELSE
        A(I,J)=0.0
        ENDIF
        ENDIF
        ENDDO
        ENDDO
        PRINT*,'AUGMENTED MATRIX'
        WRITE(6,*)
        PRINT 10, ((A(I,J),J=1,6),I=1,3)
  10    FORMAT(3X,6F8.2//)
        DO 7 I=1,3
        IF(A(I,I).NE.0.0)THEN
        P=A(I,I)
        ELSE
        PRINT*,'PIVOT ELEMENT IS ZERO'
        STOP
        ENDIF
        DO JJ=1,6
        A(I,JJ)=A(I,JJ)/P
        ENDDO
        DO 3 J=1,3
        IF(J.EQ.I)GOTO 3
        COM=A(J,I)
        DO K=1,6
        A(J,K)=A(J,K)-A(I,K)*COM
        ENDDO
  3     CONTINUE
  7     CONTINUE
        WRITE(6,*)
        PRINT*,'AUGMENTED ECHELON MATRIX'
        WRITE(6,*)
        PRINT 11,((A(I,J),J=1,6),I=1,3)
 11     FORMAT(3X,6F8.2//)
        DO I=1,3
        DO J=4,6
        K=J-3
        AI(I,K)=A(I,J)
        ENDDO
        ENDDO
        WRITE(6,*)
        PRINT*,'INVERSE OF MATRIX A'
        WRITE(6,*)
        PRINT 30, ((AI(I,J),J=1,3),I=1,3)
  30    FORMAT (3X, 3F8.2//)
        END
Share:

No comments:

Post a Comment

Popular Posts

Powered by Blogger.

Translate

Labels

Recent Posts

Unordered List

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  • Vestibulum auctor dapibus neque.

Pages

Theme Support

Need our help to upload or customize this blogger template? Contact me with details about the theme customization you need.