Docsity

Purpose of Programming - Numerical Methods and Computing - Old Exam Paper, Exams of Mathematical Methods for Numerical Analysis and Optimization

Partial preview of the text

Download Purpose of Programming - Numerical Methods and Computing - Old Exam Paper and more Exams Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Cork Institute of Technology

Bachelor of Engineering (Honours) in Structural Engineering - Stage 1

(NFQ – Level 8)

Summer 2007

Computing & Numerical Methods

(Time : 3 Hours)

Answer Question 1 and 2 other questions. Examiners :

Prof. P.E.O’Donoghue

Mr. P. Anthony

Dr. M.G. Murphy

Q1 continues

Q1[ 40 marks]

Please answer each of the following questions.

Q1a[ 8 marks] What is the output from the following Fortran90 program if the values 8, 0, 2, 5 are input in turn at the prompts? NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

PROGRAM q1a implicit none integer value, k !---------------------------------------------------- DO k = 1, 4, 1 write(, '(a)', advance='no')'gimmee a value --->' read,value if( (value > 2) .and. (value < 7))then print, 'Wexford' else if(value /= 0)then print, 'Waterford' else print*, 'Cork' end if

if(value == 5)then print, 'Kerry' else if(value >= 3)then value = value + 1 print, 'Limerick' end if END DO !---------------------------------------------------- stop 'q1a ends ...' END PROGRAM q1a

Q1 continues

Q1 continues

Q1c[ 8 marks] What is the purpose of the following programming? Test it with the values 12, 15, 17, 35 and others if necessary. Find what is IT in each case. Deduce what the code does from your findings. NOTE: Trace your working so that partial credit may be given for incomplete or incorrect work.

PROGRAM q1c

integer::m, k logical::gotIT = .false. !---------------------------------------------------- write(*,'(A)',advance='no')'gimmee an integer m --->' read *, m

k = 2 do if(k >= m)exit if(mod(m, k) == 0)then gotIT = .true. exit end if k = k + 1 end do print* if(gotIT)then print, 'so there it is --->', k, ' is IT' else print, 'so there is NO IT at all' end if print* !---------------------------------------------------- stop'q1c.f90 ends ...' END PROGRAM q1c

Q1 continues

Q1 continues

Q1d[ 8 marks]

What is the output of the following piece of Fortran90 code?

PROGRAM q1_d implicit none

integer::a, b, c = 7 !------------------------------------------------------------------- do a = c, 1, - do b = c, 1, - if( (a == 1) .or. (a == c) .or. (b == 1) .or. (b == c) )then write(, 12, advance='no')'$' else if(a == b)then write(, 12, advance='no')'' else if(a + b == c + 1)then write(, 12, advance='no')'#' else write(, 12, advance='no')' '!this is a blank end if end do print end do print* print*

12 format(a) !------------------------------------------------------------------- stop'q1_d ends ...' END PROGRAM q1_d

Q2 ends

Q2[ 30 marks]

The experimental data for an important experiment are stored in the file f:\myExperiment\myData.dat. The data is raw(without any explanatory text or headings) and each line contains the following data: time, temperature, pressure, volume as shown in fig 2.1 below.

Please write Fortran90 code / program

  • to connect to this file,
  • to read the data and to transfer them to the new file f:\myExperiment\myDataFormatted.dat in tabular form with headings as shown in fig. 2.2 below ,
  • to calculate the mean and standard deviation( fig 2.3 may help here ), the maximum and minimum of the volume
  • to append that statistical detail below the table as shown in fig. 2.2 below.

Fig 2.

time temperature pressure volume

average volume: 156.

standard deviation volume: 12.

maximum volume: 234.

minimum volume: 123.

Fig 2.

For data

the mean x and the standard deviation s are given by

x 1 , x 2 , x 3 ,..., x n

n

x

x

n

i

∑ i

x

n

x

n

x x

s

n

i

i

n

i

i

Fig 2.

Q3 ends

Q3[ 30 marks]

Write a Fortran90 program to estimate the maximum value of the function ( 2 )

2 2

x

x xy y

f xy

over a rectangular region a ≤ x ≤ b , c ≤ y ≤ d in the (x, y) plane by evaluating f(x, y) at the (n + 1)^2 nodes of a

grid spanning the region. (see Fig 3.1 below)

The code should feature

  • user specification of the search area a ≤ x ≤ b , c ≤ y ≤ d ,
  • user specification of the grid size n,
  • output of the minimum value and its location,
  • user option to repeat the search over different area or different grid size

Fig. 3.

d

c

a b

n = 3

16 nodes

Q5 ends

Q5[ 30 marks]

Please answer each of the following questions.

Q5a(15 marks) Write , in module form, a function reverse(anInteger) to accept an integer as parameter and to return the reverse of that integer

anInteger = 12345 getTheResult = reverse(anInteger) will result in the variable getting the value 54321

Q5b(15 marks) Write, in module form, a function apply(left, symbol, right) which will return the result of applying an operator, symbol , to the real numbers, left and right , The valid operators are

  • for addition
  • for subtraction
  • for multiplication / for division If division by zero is encountered, the operator is not applied and 0.0 is returned. If an invalid symbol is encountered, the operator is not applied and 0.0 is returned e.g. apply(1.2, ‘+’, 3.4) Î^ 4. apply(4.2, ‘/’, 2.1) Î 2. apply(4.2, ‘/’, 0.0) Î 0. apply(4.2, ‘-’, 5.3) Î -1. apply(4.2, ‘*’, 2.1) Î 8.