ARBAMINCH UNVERSITY
SCHOOL OF
POST GRDUTAE STUDIES
COMPUTER-PROGRAMING & NUMERICAL METHOD
ASSIGNMENT NO. IV
ID.NO: MSc/KP/H/014/03
Submitted by:-
Eshetie Tesfaye
Dept: HYDRULICS & HYDROPOWER ENGINEERING
October, 2012
1. State whether each of the following program names is valid or not. If not, state why the name
is invalid.
a) Junk
Answer: Valid Integer Variable
b) 3rd
Answer: Invalid → Program Name Must Begin With a Letter
c) Who_are_you?
Answer: Valid Character Constant
d) time_to_intercept
Answer: Valid Real Variable
2.Which of the following expressions are legal in FORTRAN? If an expression is legal, evaluate it.
a. 2.**3/3**2 Legal Expression
Evaluate exponents first: 8. /9
Evaluate divisions: 0.8889 (Real Result)
b. 2*6 + 6**2 / 2 Legal expression
Evaluate exponents first: 2*6 + 36 /2
Evaluate multiplications and divisions: 12 + 18
Evaluate addition: 30 (Integer Result)
c. 2* (-10)**-3 Illegal Expression ( two adjacent operators )
d. 2 / (-10.)**3 Legal Expression
Evaluate exponents first: 2 / (-1000.)
Evaluate divisions: -0.002 (Real Result)
e. 23 / (4 / 8) Illegal Expression ( division by zero )
3.Write the Fortran statements required to calculate y (t) from the equation
for a user-supplied value of t.
SOLUTION
The function y (t) is evaluated differently, depending on the sign of the variable t. To determine the proper
equation to apply, it will be necessary to check for the sign of the t value supplied by the user.
State the problem
This problem statement is very simple: Evaluate the function y (t) for any user supplied value of t.
Define the Inputs and Outputs
The inputs required by this program are the values of the variable t. The output from the program will be
the value of the function y (t).
Design the algorithm.
This task can be broken down into three major sections, whose functions are input, processing, and
output:
Read the input values t
Calculate y (t)
Write out y (t)
We will now break each of the above major sections into smaller, more detailed pieces.
There are four possible ways to calculate the function y (t), depending on the values of t, so it is logical to
implement this algorithm with a two-branched IF statement. The resulting pseudo code is:
Prompt the user for the values t
Read t
Echo the input coefficients
IF t ˃ 0 THEN
fun ← (-3)*t**2 +5
ELSE IF t ˂ 0 THEN
fun ← 3*t**2 +5
END IF
Write out y (t)
Turn the algorithm into FORTRAN statements
The final FORTRAN code is shown below
FUNCTION Lagrange(x, y, n, x)
Sum = 0
DO i = 0, 1, 2
Product f(xi)
DO j = 0, 1, 2
IF i /= j THEN
Product = Product*(x-xj)/(xi-xj)
ENDIF
END DO
sum = sum + product
END DO
Lagrange = sum
END Lagrange
PROGRAM funt
!
! Purpose:
! This Program solves the function y(t) for a user specified t,
! Where y(t) is defined as:
! _
! |
! Y(t) = | (-3)*t**2 + 5 t >= 0
! | 3*t**2 + 5 t < 0
! |_
!
! Record of revisions:
! Date Programmer Description of change
! ========== ============= =======================
! 14/10/2012 Eshetie Tesfaye Original code
!
IMPLICIT NONE
! Data dictionary: declare variable types, definitions, & units
REAL :: t ! Independent variable
REAL :: fun ! Resulting function
! Propt the user for the values t
WRITE (*) 'Enter the coefficient t: '
READ (*) t
! Calculate the function y(t) based upon the signs of t.
IF ( t >= 0. ) THEN
fun = (-3)*t**2 + 5
ELSE IF ( t < 0. ) THEN
fun = 3*t**2 + 5
END IF
! Write the value of the function.
WRITE (*) 'The value of the function is: ', fun
END PROGRAM funt
The flow chart for this program is shown in figure below
Start
READ t
WRITE t
.FALSE.
t>0
.TRUE.
fun ← (-3)*t**2 +5 fun ← 3*t**2 +5
WRITE fun
Stop
Flowchart of Program fun t
4.Write a Fortran program to evaluate the equation y�x� � x 2 � 3x � 2 for all values of x between ↸1 and
3, in steps of 0.1.
SOLUTION
State the problem
This problem statement is very simple: Evaluate the function y (x) for any user supplied value of x
between -1 and 3 in steps of 0.1.
Define the Inputs and Outputs
The inputs required by this program are the values of the variable x. The output from the program will be
the value of the function y (x).
Design the algorithm.
This task can be broken down into three major sections, whose functions are input, processing, and
output:
Read the input values x
Calculate y (x)
Write out y (x)
There is one possible way to calculate the function y (x), depending on the values of x, so it is logical to
implement this algorithm with one IF statement. The resulting pseudo code is:
Prompt the user for the values x
Read x
Echo the input coefficients
IF -1 < x ˂ 3 THEN
𝑓𝑢𝑛 ← x 2 � 3x � 2
END IF
Write out y (x)
Turn the algorithm into FORTRAN statements
The final FORTRAN code is shown below
PROGRAM funx
!
! Purpose:
! This Program solves the function y(x) for a user specified x,
! where y(x) is defined as:
!
! Y(x) = x**2 - 3*x + 2 -1 < x < 3
!
! Record of revisions:
! Date Programmer Description of change
! ========== ============= =======================
! 14/10/2012 Eshetie Tesfay Original code
!
IMPLICIT NONE
! Data dictionary: declare variable types, definitions, & units
REAL :: x ! Independant variable
REAL :: fun ! Resulting function
! Propt the user for the values x
WRITE (*) 'Enter the coefficient x: '
READ (*) x
! Calculate the function y(x).
IF ( -1 < x < 3 ) THEN
fun = x**2 - 3*x + 2
END IF
! Write the value of the function.
WRITE (*) 'The value of the function is: ', fun
END PROGRAM funx
The flowchart for this program is shown below
Start
READ x
WRITE x
.FALSE.
-1 < x < 3
.TRUE.
x ← x + 0.1
𝑓𝑢𝑛 ← 𝑥 ∗∗ 2 � 3 ∗ 𝑥 � 2
Flowchart of Program fun x
WRITE fun
Stop
5.Write a Fortran program to implement the quadratic Lagrange polynomial given by the following
equation
The program must define the data set, and print the data set and the solution.
Solution
Each of the three terms in the given equation passes through one of the data points and is zero at the other
two. The summation of the three terms must, therefore, be the unique second order polynomial f2(x) that
passes exactly through one of the data points.