Docsity

Fortran90 Exam Questions - Mech Eng (BE (Hons) Stage 1) - Summer 2007 - Computing, Exams of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
1
Cork Institute of Technology
Bachelor of Engineering (Honours) in Mechanical Engineering-
Stage 1
(NFQ Level 8)
Summer 2007
Computing and Numerical Methods
(Time: 3 Hours)
Instructions:
Answer Question 1 and two others Examiners: Ms. J.English
Prof. M. Gilchrist
Mr. P. Clarke
Q1 (40 marks) Please answer each of the following questions:
Q1 (a) (8 marks)
What output is produced from the following Fortran90 program given the following input values
4, 7, 3, 12?
NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.
PROGRAM s2007_q1a
implicit none
!-----------------------------------------------------------
integer::O_Gara
do
write(*, '(a)', advance = 'no') 'gimmee an integer --->'
read*, O_Gara
if ((O_Gara < 0) .or. (O_Gara > 10) )exit
if((O_Gara == 2) .or. (O_Gara > 3))then
print*, '…O_Callaghan’
else if(O_Gara /= 1)then
print*, '…ODriscoll'
else
print*, '…O_Connell'
end if
if(O_Gara == 4)then
print*, ‘Horgan...'
else if(O_Gara == 3)then
print*, ‘Stringer...'
end if
end do
!-----------------------------------------------------------
stop 's2007_q1a has now ended …'
END PROGRAM s2007_q1a
pf3
pf4
pf5

Partial preview of the text

Download Fortran90 Exam Questions - Mech Eng (BE (Hons) Stage 1) - Summer 2007 - Computing and more Exams Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Cork Institute of Technology

Bachelor of Engineering (Honours) in Mechanical Engineering-

Stage 1

(NFQ Level 8)

Summer 2007

Computing and Numerical Methods

(Time: 3 Hours)

Instructions: Answer Question 1 and two others Examiners: Ms. J.English Prof. M. Gilchrist Mr. P. Clarke

Q1 (40 marks) Please answer each of the following questions:

Q1 (a ) (8 marks)

What output is produced from the following Fortran90 program given the following input values 4, 7, 3, 12? NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

PROGRAM s2007_q1a implicit none !----------------------------------------------------------- integer::O_Gara do write(, '(a)', advance = 'no') 'gimmee an integer --->' read, O_Gara if ((O_Gara < 0) .or. (O_Gara > 10) )exit if((O_Gara == 2) .or. (O_Gara > 3))then print, '…O_Callaghan’ else if(O_Gara /= 1)then print, '…ODriscoll' else print, '…O_Connell' end if if(O_Gara == 4)then print, ‘Horgan...' else if(O_Gara == 3)then print*, ‘Stringer...' end if end do !----------------------------------------------------------- stop 's2007_q1a has now ended …' END PROGRAM s2007_q1a

Q1 (b) (8 marks)

What is the output of the following Fortran90 program? NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

Q1 (c) (8 marks)

What is the output of the following Fortran90 program? NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

PROGRAM s2007_q1b

implicit none

integer :: m,n,p

p = 7

do m = 1,p, do n = m,p, 1 if((m == 1).or.(m == p).or.(n == m).or.(n == p)) then write(,'(i1)',advance = 'no') n else write(,'(a)',advance = 'no') '+' end if end do print * end do !------------------------------------------------------------------- stop 'q1_b.f90 ends ...'

END PROGRAM s2007_q1b

PROGRAM s2007_q1c implicit none !------------------------------------------------------------------- integer::a, b, c c = 7 do a = 1, c, 1 do b = 1, c, 1 if( (a == 1) .or. (a == c) .or. (b == 1) .or. (b == c) )then write(, 14, advance='no')a else if(a == b)then write(, 12, advance='no')'' else if(a + b == c + 1)then write(, 12, advance='no')'#' else write(, 14, advance='no')b end if end do print end do print* print* 12 format(a) 14 format(i1) !------------------------------------------------------------------- stop'q1_c ends ...' END PROGRAM s2007_q1c

Q2 (30 marks)

The data for an important experiment are stored in the file inputData.txt. The data is raw (without any explanatory text or headings) and each line contains the following data: x , y as shown in fig. 2.1 below. We use x and y to preserve confidentiality.

Please write a Fortran90 program to connect to the file, to read the data and to transfer them to the new file myResults.dat in tabular form as shown in fig 2.2 and append below the table details of the least-square line( or the regression line) and the correlation coefficient as shown in fig 2.

The relevant formulae also included.

Fig. 2.

Equation of least-squares line is

y = 3.34x -15.

The correlation coefficient ---> 0.

Fig. 2.

For data

( x 1 , y 1 ),( x 2 , y 2 ),...,( xn , yn )

the least-squares line y = a+bx is given by:

1 1 1

2

1 1 1

1 1

n n n

k k k k k k k n n n

k k k k k k

n n

k k k k

n x y x y

b

n x x x

a y bx

where

y x

y x

n n

= = =

= = =

= =

and the correlation coefficient is given by:

1 1 1 2 2 2 2

1 1 1 1

n n n

k k k k k k k

n n n n

k k k k k k k k

n x y x y

n x x n y y

− = = =

= = = =

Q3(30 marks) Please answer each of the following questions.

Q3(a) (4 marks)

Write a Fortran90 (^) function module for the Fortran90 function FUNCTION f(x)

y = f ( ) x = x^3 − 4 x + 2

Q3(b) (13 marks)

Write a Fortran90 subroutine module to implement the Secant Method Root Finding Algorithm,

SUBROUTINE SecantMethod(x1,x2,epsilon,MaIter,root)

Note:

  • two initial approximations are entered as x1 and x.
  • epislon is the user-specified degree of accuracy(We’re happy with a tolerance level of 1.0E-
  • that we put a limit , maxIter , on the number of iterations the subroutine performs
  • the final approximation of the root is returned by root

Secant method formula:

1 1 1

i i i i i i i

x x

x x f x

f x f x

−

−