Showing posts with label fortran program. Show all posts
Showing posts with label fortran program. Show all posts

Mixing variable types

Mixing variable types

Exercise 3.1

Copy   divide.f95 from Downloads
Make sure you understand the following program thoroughly!
  program divide
   implicit none
   integer  :: x
   real  :: y
   x  = 1
   y = x/3
   print  *, y
   end program divide
Run it. This program produces the following output:
  0.00000
Something odd is happening. The problem is the line:
  y=x/3
FORTRAN evaluates the right  hand side of the assignment first using integer arithmetic, because both x and 3 are integer. 1 divided by 3 cannot be stored as an integer, and so the value 0 is returned. The result, 0, is then converted to a real number and the assigned to y.
Replace the line in program divide
  
  x = 1       
by  
  
  x = 10
Your output should now be:
  3.00000
Can you see what is happening? FORTRAN is keeping the integer part of the answer and throwing the rest away.
To get over this problem, we have to signal to FORTRAN that we want it to calculate the right hand side of the expression using real arithmetic. If we want  to keep x as integer data type, we could re-write our expression as follows:
                y=x/3.0
The presence of a real number on the right hand side causes the right hand side of the expression to be evaluated using floating point arithmetic.
Actually, the problem is even more complicated! Where we have an expression like
                y=x * ((2**i)/3)
where x and y are real and i is integer, FORTRAN computes the result in stages:
First it calculates (2**i)/3 and evaluates it as an integer number, then multiplies the result by x and evaluates it as real.

Exercise 3.2

Copy check.f95 to your filestore.
    
  program check
    !Integer and real arithmetic
    implicit none
    real :: x,y
    integer i
    x=2.0
    i=2
    y=x*((2**i)/3)
    print *,y
    y=x*((2.0**i)/3)
    print *,y
    end program check 
   
… and examine its output. Make sure you understand why this is happening.

The do loop

Unless we are able to re-execute code, we might as well use a calculator… Now we start to take advantage of the power of the computer.

Exercise 3.3

Copy program loop.f95
      
  program  loop
  implicit none     
    integer  :: i
    do  i=0,20
     print  *,i
    end  do
    end  program loop
  
  
Run the program. It prints out the numbers from 0 to 20 in steps of 1.
Note:
  • is called a loop counter. In this example, it has a start value of zero.
  • All the statements within the do and end do are executed. In this example there is just the one statement, ie print.
  • Each time the statements are executed, the loop counter, i, is incremented by 1.
  • When the value of i is 20, the loop terminates, and the program resumes after the end do.
Change the do statement in program loop as follows:
  do i = 50,70,2
Run the program. What happens?
The third argument in the do statement, is the increment step.  If omitted, the value is taken as 1.
Loops can also decrement: try this
 
  do i = 5,-5,-2
 

Exercise 3.4

Using a do loop to generate integer values of x between –10 and 10 in steps of 1, write a program that constructs a table of values of
  y = 1.0/x 
What happened when x had the value zero? Use an if, end if to test for the condition that gives the incorrect value, and print out an appropriate message. Compare your result with divbyzero.f95.
Division by zero
is one of the commonest reasons for a program to fail

Nested Do Loops

We want to construct a table of values for z where
  z = xy           
for values of          x in the range 1 to 2 in steps of 0.5 and
y in the range 1 to 2 in steps of 0.5
Work through the next exercise which illustrates this:

Exercise 3.5

Copy program xytab.f95 to your filespace.
  program  xytab
    implicit none
    !constructs a table of z=x/y for values of x from 1 to 2 and 
    !y from 1 to 4 in  steps of .5
    real         ::   x, y, z 
    print *, '           x           y           z'
    do  x = 1,2
     do y = 1,4,0.5
      z = x/y
      print *, x,y,z
     end do
    end  do
    end  program xytab
  
Examine its output. Notice the use of the first print  to give a heading to the table.

Using loops to do summation

Earlier on, we discussed the idea of assignments.
  x = 1.0
means store the value 1.0 in the memory location called x.
If we had the following code:
  x = 1.0
  x = x +  1.0
  print *, x
        
Can you guess what value would be printed out for x?
The answer would be 2.0.
Really important!
Bearing in mind the definition of an assignment,  the statement
  x = x  + 1.0
        
means “add 1.0 to the value currently stored in memory location x and then store the result in memory location x”.

Exercise 3.6

Copy file increment.f95 to your file space and examine its output.
    program increment
    implicit none
    integer :: i
    real :: x
    x = 1.0
    do i=1,10
     x = x + 1.0
     print *, i,x
    end do
    end program increment
  • Note carefully that we have set the initial value of x outside of the do loop. Why have we done this? If you aren't sure – change the code to put the line x = 1.0 inside the loop – then examine the output.
  • It is important to understand that if we use constructions such as x = x + 1.0, then it is vital to initialise x to some value. If we don't, it is possible that the value might be set to any random number. Run the program, make a note of the final value of x then put an exclamation mark in front of the x = 1.0 statement and run the program again.

Exercise 3.7

Edit the line x = x + 1.0 in program increment.f95, and change it to x = x * i. Re-run the program and examine the output. What is significant mathematically about the sequence of numbers that has been generated?

SIMPSON 3/8TH RULE

!        SIMPSON 3/8 RULE
         WRITE(*,*)'GIVE THE LIMTS'
         READ(*,*)TL,TU
         WRITE(*,*)'GIVE THE NO OF INTERVAL'
         READ(*,*)N
         H=(TU-TL)/N
         SUM=F(TU)+F(TL)+3*F(TU-H)+3*F(TU-2*H)
         DO 10 I=1,N-5,3
         SUM=SUM+3*F(TL+I*H)+3*F(TL+(I+1)*H)+2*F(TL+(I+2)*H)
  10     CONTINUE
         VALUE=3*(SUM*H)/8
         WRITE(*,20)VALUE
  20     FORMAT(5X,'THE VALUE OF INTEGRAL IS==',F8.3)
         STOP
         END
       
       
         FUNCTION F(X)
         FN=X**3+2*X**2-3*X+4
         FD=X**2+5*X-6
         F=FN/FD
         RETURN
         END

SIMPSON'S 1/3RD RULE

!        SIMPSON 1/3 RULE
         WRITE(*,*)'GIVE THE LIMTS'
         READ(*,*)TL,TU
         WRITE(*,*)'GIVE THE NO OF INTERVAL'
         READ(*,*)N
         H=(TU-TL)/N
         SUM=F(TU)+F(TL)+4*F(TU-H)
         DO 10 I=1,N-3,2
         SUM=SUM+4*F(TL+I*H)+2*F(TL+(I+1)*H)
  10     CONTINUE
         VALUE=(SUM*H)/3
         WRITE(*,20)VALUE
  20     FORMAT(5X,'THE VALUE OF INTEGRAL IS==',F8.3)
         STOP
         END
       
       
         FUNCTION F(X)
         FN=X**3+2*X**2-3*X+4
         FD=X**2+5*X-6
         F=FN/FD
         RETURN
         END

FALSE POSITION METHOD

!         PROGRAM FALSE POSITION METHOD
          WRITE(*,*)'GIVE THE INITIAL GUESS VALUES'
          READ(*,*)X1,X2
          WRITE(*,*)'GIVE THE ERROR ALLOWED VALUE'
          READ(*,*)ER
          FX1=F(X1)
          FX2=F(X2)
          FP=FX1*FX2
          WRITE(*,10)FP
10        FORMAT(5X,E8.6)
          IF(FP.GT.0.0) GO TO 100
15        X3=(X1*FX2-X2*FX1)/(FX2-FX1)
          FX3=F(X3)
          IF (ABS(FX3).LE.ER) THEN
            WRITE (*,20)X3
20          FORMAT(5X,'THE ROOT IS='F8.4)
            WRITE(*,30)FX3
30          FORMAT(5X,'THE VALUE OF FUNCTION AT ROOT',F8.4)
            GO TO 200
          ELSE
            FP=FX1*FX3
            WRITE(*,*)FP
          IF (FP.LT.0)THEN
            X2=X3
            FX2=FX3
            GO TO 15
          ELSE
            X1=X3
            FX1=FX3
            GO TO 15
          ENDIF
          ENDIF
          GO TO 200
100     WRITE(*,*)'THE ROOT DOES NOT LIE IN THE INTERVAL'
200     STOP
        END
       
       
       
        FUNCTION F(X)
        F=X**4+5*X**3-2*X**2-3*X-10
        RETURN
        END
        

Bisection Method in FORTRAN 77

!       BISECTION METHOD
        WRITE(*,*)'VALUES OF X1,X2'
        READ (*,*)X1,X2
        WRITE(*,*)'ENTER ERROR YOU WANT'
        READ(*,*)ER
        FX1=F(X1)
        WRITE(*,10)FX1
        FX2=F(X2)
        WRITE(*,10)FX2
10     FORMAT(F8.3)
        FP=FX1*FX2
        IF (FP.GT.0) GO TO 100
15      X3=(X1+X2)/2
        FX3=F(X3)
        IF (ABS(FX3).LE.ER) THEN
         WRITE (*,20)X3
20       FORMAT(5X,'THE ROOT IS='F8.4)
         WRITE(*,30)FX3
30       FORMAT(5X,'THE VALUE OF FUNCTION AT ROOT',F8.4)
         GO TO 200
        ELSE
          FP=FX1*FX3
         WRITE(*,*)FP
          IF (FP.LT.0)THEN
           X2=X3
           FX2=FX3
           GO TO 15
          ELSE
           X1=X3
           FX1=FX3
           GO TO 15
          ENDIF
        ENDIF
        GO TO 200
100     WRITE(*,*)'THE ROOT DOES NOT LIE IN THE INTERVAL'
200     STOP
        END
       
        FUNCTION F(X)
        F=X**3-5*X+3
        RETURN
        END
       
       
       
        

SOLUTION OF AX^2+BX+C

   WRITE(*,*) 'ENTER VALUE OF A,B,C'
       READ(*,3) A,B,C
    3  FORMAT(3F5.2)
       D=B*B-4*A*C
       IF(D.LT.0) GO TO 16
       IF(D.GT.0) THEN
       ROOT1=(-B+SQRT(D))/2*A
       ROOT1=(-B-SQRT(D))/2*A
       WRITE(*,10) ROOT1,ROOT2
   10  FORMAT(2F5.2)
       ELSE
       ROOT=(-B)/(2*A)
       WRITE(*,14) ROOT
  14  FORMAT(F5.2)
       END IF
 16  STOP
       END

FORTRAN PROGRAM 2 Trapezoidal Rule

Q write a program using fortram to find area using trapezoidal rule
Ans  
         program trap
implicit none
         REAL :: H,Y1,YLAST,y,A,S
       
         INTEGER :: i,N
       
         WRITE(*,*) "ENTER NUMBER OF TERMS"
         READ(*,*) N
         WRITE(*,*) "ENTER STEP SIZE"
         READ(*,*) H
         WRITE(*,*) "ENTER FIRST TERM"
         READ(*,*) Y1
         WRITE(*,*) "ENTER LAST TERM"
         READ(*,*) YLAST
         S=Y1+YLAST
         DO i=1,N-1
         WRITE(*,*) "ENTER L",i,"TH TERM"
         READ(*,*) y
         S=S+2*y
   
         END DO
       
         A=(H*S)/2
         WRITE(*,*) "AREA IS"
         WRITE(*,*) A
         READ(*,*)
         end program trap 

FORTRAN PROGRAM 1. Solution of Simpson's 1/3 Rule

Q. write a program using fortran to find area using simpson's 1/3 rule
A.
 program simp3
       implicit none
       integer :: n,i
       real :: h,y,y1,ylast,s,a
       write(*,*) "Enter Number of trems"
       read(*,*) n
       write(*,*) "Enter Step size"
       read(*,*) h
       write(*,*) "Enter first term"
       read(*,*) y1
       write(*,*) "Enter last term"
       read(*,*) ylast
       s=y1+ylast
       do  i=2,n-1
       write(*,*) "enter y",i,"th term"
       read(*,*) y
       continue
       IF(mod(i,2).EQ.0) then
          s=s+4*y
          else
          s=s+2*y
       end IF
       end do
       a=h*s/3
       write(*,*) "area is :-  ",a
       read(*,*)
       end program simp3

OLDER POST

Head Constable (Ministerial)

Head Constable (Ministerial) Apply Now   Notification