Some exercises Dr.Entesar Ganash
Exercise 1 Write a program to re-calculate the problem 1 but now using Newton’s method to solve numerically the last equation, starting with x=0 .5 and stopping either when the absolute value of the function is less than or after 20 repetitions. Using IF statements & GO TO statement instead of Do While statement. Compare the results from both problems. Solving PROGRAM NEWTON IMPLICIT NONE Integer::k ! a counter Real :: Eps ,x ! required accuracy (epsilon) & staring guess k=0 Eps=1.0E-6 x=0.5 write(*,*)' ','x',' ', 'f(x)' write(*,*)'----------------------------------‘
continue Solving 1 10 If ((ABS(f(x))<=Eps) .OR. (k> 20)) then write(*,*)x, f(x) Else x=x-f(x)/df(x) k=k+1 Go to 10 End if write(*,*)'----------------------------------' IF (ABS(f(x))<=Eps)THEN Write(*,*)'Newton converged' ELSE Write(*,*)'Newton diverged' END IF Contains !-------------------------- !Function Subprogram f is to solve f(x)=0 REAL FUNCTION f(x) Implicit none Real::f,x f=x-0.2*sin(x)-0.5 End FUNCTION f continue Solving 1
continue Solving 2 !-------------------------- !Anther Function Subprogram df REAL FUNCTION df(x) Implicit none Real::df,x df=1.0-0.2*cos(x) End FUNCTION df END PROGRAM NEWTON continue Solving 2
The result:
Exercise 2 Solving Write a program to calculate in radians for ten terms, When Then calculates the sine using the sine intrinsic function Hint: n! = n*(n-1)*(n-2)*….*3*2*1. Solving ROGRAM SUMSIN Implicit none Integer:: I,K ! counters Real :: theta ,factn,sum,PI ! angle, factorial value, summation & Constant Real :: inf ! avariable symbol PI=3.141592654 THETA=45.0 !in degree THETA=THETA*PI/180.0 ! to convert to rad
continue Solving sum=0.0 ! intial value DO K=1,10 ! number of term factn=1.0 DO I=2*K-1,1,-1 factn=factn*I ENDDO sum=sum+(-1)**(k-1)*theta**(2*k-1)/factn print*,K,SUM ! FROM intrinsic function inf=SIN(THETA) print*,'---------------------------------' PRINT*, 'SIN(THETA)from intrinsic function' PRINT*, 'SIN(THETA)=',inf END PROGRAM SUMSIN continue Solving
The result: