1.
Math Assainment
Math Assignment Question: 1. Consider f(x,y))
Write a Fortran program which find fxy(0,0) and fyx(0,0) and print fxy(0,0)=fyx(0,0)
Solution:
Real::H,K,X,Y
Parameter(H =.0001,K= .0001)
F(X,Y)=(2.0*x*y*/SQRT(x**2+y**2))
F0=0
y=0.001
Fx=(F(H,Y)-F(0,Y))/H
Fxy=(FX-F0)/Y
PRINT10,FXY
10 Format('Fxy=',F10.2)
X=0.001
Fy=(F(X,K)-F(X,0))/K
Fyx=(Fy-F0)/X
PRINT20,Fyx
20 Format('Fxy=',F10.2)
IF(Fxy.NE.Fyx)Then
Print*, 'Fxy is not equal to Fyx'
Else
Print*, 'Fxy is equal to Fyx'
ENDIF
STOP
END
Math Assignment Question:2. Write a Fortran program to find the nature of the roots and solve the equation x2-x+1=0
Solution:
READ*, A,B,C
IF(A.EQ.0.0) STOP
D=B**2-4.0*A*C
IF(D.GT.0.0) THEN
x1=-B+SQRT(D)/(2.0*A)
x2=-B-SQRT(D)/(2.0*A)
PRINT*,'This equation has two real roots and the roots are'
PRINT*, 'x1=',x1,'x2=',x2
ELSEIF(D.EQ.0.0) THEN
x1=-B/(2.0*A)
x2=-B/(2.0*A)
PRINT*,'This equation has two equal roots and the roots are'
PRINT*, 'x1=x2=',x1
ELSE
p=-B/(2.0*A)
q=SQRT(ABS(D))/(2.0*A)
PRINT*,'This equation has complex roots and the roots are'
PRINT*, 'x1=',p,'+i',q
PRINT*, 'x1=',p,'-i',q
ENDIF
STOP
END
Result:
1
-1
1
This equation has complex roots and the roots are
x1= 0.500000 +i 0.866025
x1= 0.500000 -i 0.866025
Math Assignment Question:3. Write a Fortran Program to find the positive root of the equation e-x-x=0 using Newton Rapson method
Solution:
f(x)=EXP(-x)-x
fd(x)= -EXP(-x)-1
q=0.1E-4
10 read(5,*)x0
n=1
print*,' n x '
write(6,*)
20 a=f(x0)
if(a.eq.0.0)goto 40
b=fd(x0)
if(b.eq.0.0)goto 10
write(6,30)n,x0
30 format(3x,i3,2x,f13.10)
write(6,*)
x1=x0-a/b
if(abs(x0-x1).lt.q) goto 40
n=n+1
x0=x1
goto 20
40 write(6,50)x0
50 format(3x,'The solution x = ',f13.10)
write(6,*)
end
Result:
0.5
n x
1 0.5000000000
2 0.5663110018
3 0.5671431422
The solution x = 0.5671431422
Math Assignment Question: 4. write a Fortran program to find sqrt(155) using lagrange’s interpolate formula from the data given below
|
x |
150 |
152 |
154 |
156 |
|
y |
12.247 |
12.329 |
12.410 |
12.490 |
Solution:
REAL X(0:3),F(0:3),M
DATA X/150.0,152.0,154.0,156.0/
DATA F/12.247,12.329,12.410,12.490/
DO 50 K=2,4
Y=155.0
SUM=0.0
DO 20 I=0,K-1
M=1.0
DO 10 J=0,K-1
IF(I.EQ.J)GOTO 10
M=M*(Y-X(J))/(X(I)-X(J))
10 CONTINUE
SUM=SUM+M*F(I)
20 CONTINUE
WRITE(6,*)
WRITE(6,30)K
30 FORMAT(3X,'FOR ',I3,'-POINTS LAGRANGES INTERPOLATION FORMULA')
WRITE(6,*)
WRITE(6,*)
WRITE(6,40)Y,SUM
40 FORMAT(3X,'F(',F5.1,') =',F12.6//)
50 CONTINUE
END
Result:
FOR 2-POINTS LAGRANGES INTERPOLATION FORMULA
F(155.0) = 12.452003
FOR 3-POINTS LAGRANGES INTERPOLATION FORMULA
F(155.0) = 12.450123
FOR 4-POINTS LAGRANGES INTERPOLATION FORMULA
F(155.0) = 12.450125
Math Assignment Question:5. write a fortran program to evaluate by using Simpson’s 1/3 rule.
f(x)=SQRT(x)
real I
a=1
b=1.3
n=12
h=(b-a)/real(n)
sum1=0.0
sum2=0.0
x=a
fa=f(a)
fb=f(b)
do 5 k=1,n-1
if(mod(k,2).ne.0)sum1=sum1+f(x+k*h)
if(mod(k,2).eq.0)sum2=sum2+f(x+k*h)
5 continue
I=h*(fa+fb+4.0*sum1+2.0*sum2)/3.0
print 10,I
10 format(3x,'Integral value =',f10.5)
stop
End
Result:
Integral value = 0.32149
Math Assignment Question:6. Write a Fortran program to find prime numbers from 1 to 500 .the output should have 5 numbers in each line.
Solution:
integer m(500)
j=0
do 10 n=2,500
do k=2,n/2
if(mod(n,k)==0)goto 10
enddo
j=j+1
m(j)=n
10 continue
write(6,*)
print*,' List of Prime Numbers from 2 to 500 '
write(6,*)
print 20,(m(i),i=1,j)
20 format(3x,5i7/)
write(6,*)
end
Result:
List of Prime Numbers from 2 to 500
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
179 181 191 193 197
199 211 223 227 229
233 239 241 251 257
263 269 271 277 281
283 293 307 311 313
317 331 337 347 349
353 359 367 373 379
383 389 397 401 409
419 421 431 433 439
443 449 457 461 463
467 479 487 491 499
Math Assignment Question:7.write a Fortran program , which reads 5×5 matrix and sum its
(i) diagonal elements
(ii)upper triangular elements and
(iii) Lower triangular elements.
Solution:
INTEGER A(3,3),DSUM,LSUM,USUM
READ(*,*)((A(I,J),J=1,3),I=1,3)
DSUM=0
USUM=0
LSUM=0
DO I=1,3
DO J=1,3
IF(I.EQ.J)DSUM=DSUM+A(I,J)
IF(I.LT.J)USUM=USUM+A(I,J)
IF(I.GT.J)LSUM=LSUM+A(I,J)
ENDDO
ENDDO
WRITE(*,*)
WRITE(*,*) 'MATRIX A'
WRITE(*,*)
PRINT 20,((A(I,J),J=1,3),I=1,3)
20 FORMAT(3X,10I6//)
PRINT*,'Sum Of Diagonal Elements=',DSUM
WRITE(*,*)
PRINT*,'Sum Of Upper Diagonal Elements=',USUM
WRITE(*,*)
PRINT*,'Sum Of Lower Diagonal Elements=',LSUM
WRITE(*,*)
END
Result:
1
2
3
4
5
6
7
8
9
MATRIX A
1 2 3 4 5 6 7 8 9
Sum Of Diagonal Elements= 15
Sum Of Upper Diagonal Elements= 11
Sum Of Lower Diagonal Elements= 19
Math Assignment Question:8. Write a Fortran Program to find the first 20 terms of Fibonacci sequence
Solution:
DIMENSION N(20)
N(1)=1
N(2)=1
DO 20 J=3,20
N(J)=N(J-2)+N(J-1)
20 CONTINUE
PRINT 10,(N(K),K=1,20)
10 FORMAT(1X,5I15)
END
1 1 2 3 5
8 13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765
Math Assignment Question:9.Write a Fortran Program to calculate the sum of the following series:
INTEGER FACT
READ*,X
SUM=0.0
DO 40 K=1,10
SUM=SUM+X**K/REAL(FACT(K))
40 CONTINUE
WRITE(6,10) SUM
10 FORMAT(2X,'Sum=',F12.4)
END
INTEGER FUNCTION FACT(N)
FACT=1
DO 30 J=1,N
FACT=FACT*J
30 CONTINUE
RETURN
END
Result:
2
Sum= 6.3890
Math Assignment Question:10. Write a Fortran program which determine a year is leap year or not .
INTEGER YEAR
READ*, YEAR
IF (MOD(YEAR,100).EQ.0)THEN
IF (MOD(YEAR,400).EQ.0)THEN
PRINT*, YEAR, 'YEAR IS LEAP YEAR'
ELSE
PRINT*, YEAR, 'YEAR IS NOT LEAP YEAR'
ENDIF
ELSE
IF(MOD(YEAR,4).EQ.0)THEN
PRINT*, YEAR, 'YEAR IS LEAP YEAR'
ELSE
PRINT*, YEAR, 'YEAR IS NOT LEAP YEAR'
ENDIF
ENDIF
STOP
END

