FEEG6002C1 计算方法

FEEG6002C1
UNIVERSITY OF SOUTHAMPTON FEEG6002C1
__
SEMESTER 1 ASSESSMENT PAPER 2020/21
Advanced Computational Methods 1
DURATION – 24 hours
__
This paper contains five questions. Answer all questions.
This is an online assessment. The solution document must be
submitted via Blackboard only. Please submit your answers to
all questions using a single word document.
Please use Quincy (free download from below link)
http://www.codecutter.net/too...
or use any of the following freely available on-line C compilers
to write, compile and execute C programmes.
https://www.onlinegdb.com/onl...
https://www.programiz.com/c-p...
A total of 100 marks are available for this paper.
2 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 2 of 15
Question 1:
a). Why do you want to work on a remote system (or remote server)?
(3 marks)
b). What is a terminal?
(2 marks)
c). Why do you need a terminal in the Linux operating system?
(2 marks)
d). What is the motivation of using shell scripts in the Linux operating system?
(3 marks)
e). List three different Linux shell types?
(3 marks)
f). What is the motivation of using make files in the Linux operating system?
(3 marks)
g). Explain with steps how to create make files in the Linux operating system?
(4 marks)
h). What is a function in C programming?
(2 marks)
i). C programming offers two types of functions. What are they, and how are they
different?
(2 marks)
j). Show two methods for defining a symbolic constant named MAXIMUM that has a
value of 200.
(2 marks)
k). If a function does not return a value, what type should be used to declare it?
(2 marks)
l). In a one-dimensional array declared with n elements, what is the subscript of the
last element?
(2 marks)
(30 marks)
3 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 3 of 15
Question 2:
Fill in the blanks in each of the following.
a). The first element of an array has index ______.
b). The __ operator returns the number of bytes used to store a variable type t.
c). To declare a pointer of type char, you need to use _______.
d). Memory allocation through _ takes place at run time.
e). C programs consist only of _______.
f). __ need to be given before the function can be called.
g). If you use constants in a C-program, it is good practice to define a _____.
h). __ is a special macro representing End Of File.
i). __ is a valuable function for allowing user input to a C program.
j). With the ______, C program can use alias names of data types that are machine
specific.
(10 Marks)
4 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 4 of 15
Question 3:
In this question you will develop a set of functions to manipulate 3x3 matrices in
various ways.
a) Write a function print_matrix(…) which takes as an input a two-dimensional
array of size nxn of type double representing a matrix, and an integer n
corresponding to the size of this array. The function should print the elements of this
array to the standard output displaying it in the form of a matrix. For example, we
expect:
1 0
0 1
for a 2x2 array a with elements a0=1, a0=0, a1=0,
a1=1. The function print_matrix(…) does not return any value.
(2 marks)
b) Write a function transpose(…) which takes as an input a 3x3 array a of type
double representing a matrix, and returns a transpose of this matrix stored in the
same input variable a. Note the transpose of a square matrix is defined as:
()
= .
(2 marks)
c) Write a function det2(…) which takes as an input a 2x2 array a of type
double representing a matrix, and returns a variable of type double equal to the
determinant of the input matrix:
|| = |
00 01
10 11
| = 0011 ? 0110.
(2 marks)
d) Write a function cofactor(…) which takes as an input a 3x3 array a of type
double representing a matrix, and two integers i and j, and returns a variable of
type double equal to the cofactor of the matrix a. The cofactor is defined as:
5 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 5 of 15
where is the minor of the element of the 3x3 matrix a, equal to the
determinant of the reduced 2x2 matrix obtained by removing all elements of the ith
row and jth column of the matrix a. Thus the notation
()
in the above equation
means the elements of the matrix remaining after removing the ith row and jth
column from the matrix a. Use function det2(…) developed above to simplify your
code.
(4 marks)
e) Write a function det3(…) which takes as an input a 3x3 array a of type
double representing a matrix, and returns a variable of type double equal to the
determinant of the input matrix a. To evaluate the determinant || of a matrix use
the Laplace expansion method:
|| = ∑
2
=0
= 0 1 2
where is the cofactor of the matrix as defined above. Note that the index i is
fixed and the result is independent of the choice of i. Use the function
cofactor(…) developed as part of the previous question to simplify your code.
(4 marks)
f) Write a function inverse(…) which takes as an input a 3x3 array a of type
double representing a matrix, and returns an inverse of this matrix stored in the
same input variable a. To calculate the inverse matrix
?1 of a matrix , note that the
elements of the inverse matrix can be expressed as:
where denotes the cofactor of the matrix a and || is the determinant of the matrix
a. Use your functions det3(…) and cofactor(…) developed above to simplify
your code.
(4 marks)
g) Write a function multiply(…) which takes as an input two 3x3 arrays of type
double representing two matrices. The function should calculate the matrix product
of the two matrices and print it to the standard output using the function
print_matrix(…) developed above. The function multiply(…) does not
return any value.
(2 marks)
6 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 6 of 15
Example of incomplete function main() could look like this:
int main(void)
{


printf("\nOriginal matrix:\n");
print_matrix(3, a);
printf("Determinant of the matrix:\n");
printf("%f\n", det3(a));
printf("\nTranspose of the matrix:\n");
transpose(a);
print_matrix(3, a);
printf("Inverse of the matrix:\n");
transpose(a); // transpose back to obtain original
matrix
for(i=0; i for(j=0; j ainvi = ai; // copy of a to invert
inverse(ainv);
print_matrix(3, ainv);
printf("Check that the product gives unit
matrix.\n");
multiply(a, ainv);

}
and assuming the given choice of the matrix the expected output if the missing parts
have been populated is:
Original matrix:
1.000000 0.000000 1.000000
0.000000 1.000000 1.000000
0.000000 0.000000 1.000000
Determinant of the matrix:
7 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 7 of 15
1.000000
Transpose of the matrix:
1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
1.000000 1.000000 1.000000
Inverse of the matrix:
1.000000 -0.000000 -1.000000
-0.000000 1.000000 -1.000000
0.000000 -0.000000 1.000000
Check that the product gives unit matrix.
1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
0.000000 0.000000 1.000000
(20 marks)
8 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 8 of 15
Question 4:
In this question you will evaluate the statistics of the United States presidential
election in 2012. You are given a file uselecton2012.txt which has 5 tabdelimited
columns:
Alabama 795696 0 1255925 9
Alaska 122640 0 164676 3
Arizona 1025232 0 1233654 11
Arkansas 394409 0 647744 6


The 1st column represents the state name, 2nd and 3rd columns the democratic popular
and electoral votes, and 4rd and 5th columns represent the republican popular and
electoral votes. Reference:
https://en.wikipedia.org/wiki...
a) Write a function count_lines(…) which takes as input a file name string and
returns an integer equal to the count of the number of lines in the input file with the
given name.
(2 marks)
b) Write a structure votes which has the following members:
char state[100]; // state name
long dempv; // democrats popular votes
long demev; // democrats electoral votes
long reppv; // republicans popular votes
long repev; // republicans electoral votes
(2 marks)
c) In function main(…) call the function count_lines(…) to determine the
number of lines nlines in the file uselection2012.txt. Allocate dynamically
the array arr to be nlines long with elements being of type structure votes.
(2 marks)
d) Write a function initialise_votes(…) which takes as input a file name
string, a pointer to an array of structures of type votes, and an integer
corresponding to the length of this array. The function will be called in main(…) for
example as:
initialise_votes("uselection2012.txt", arr, nlines);
9 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 9 of 15
and populate the members of each structure given in the array arr by information
contained in each corresponding line in uselection2012.txt. Thus, after
calling the function initialise_votes(…) the members of the structure
arr[0] should be:
state=‘Alabama’; dempv=795696; demev=0; reppv=1255925;
repev=9;
the members of the structure arr[1] should be:
state=‘Alaska’; dempv=122640; demev=0; reppv=164676;
repev=3;
and so on. The function initialise_votes(…) does not return any value, and
the output should be available through the input pointer arr.
(4 marks)
e) Write a function print_list(…) which takes as an input a pointer to an array
of structures of type votes and an integer corresponding to the length of this array.
The function will be called in main(…) for example as:
print_list(arr, nlines);
The function does not return any value and will only print the content of the file to
the standard output. You can use this function to check if your initialisation of the
array of structures arr correctly reflects the format of the file
uselecton2012.txt, i.e.:
Alabama 795696 0 1255925 9
Alaska 122640 0 164676 3
Arizona 1025232 0 1233654 11

(2 marks)
f) Write a function print_vote_state(…) which takes as an input a pointer to
an array of structures of type votes and an integer corresponding to the length of
this array. The function will be called in main(…) for example as:
maxstate = print_vote_state(arr, nlines);
10 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 10 of 15
where the output variable maxstate is a structure of type votes, and corresponds
to the state with the highest counts of the popular votes determined from among both
democrat and republican electoral votes.
(4 marks)
g) Write a function print_vote_total(…) which takes as an input a pointer to
an array of structures of type votes and an integer corresponding to the length of
this array. The function will be called in main(…) for example as:
print_vote_total(arr, nlines);
and will print to standard output four numbers corresponding to the total sum of
popular and electoral votes for democrats, and for republicans. The function
print_vote_total(…) should return a string “dem. win” or “rep. win”
depending on the higher value of the total values of electoral votes.
(4 marks)
An example of incomplete function main(…) could look like this:
int main(void){

nlines = count_lines("uselection2012.txt");


initialise_votes("uselection2012.txt", arr, nlines);
print_list(arr, nlines);
printf("\n");
maxstate = print_vote_state(arr, nlines);
printf("%s wins with %ld (dem) and %ld (rep) popular
votes.\n", maxstate.state, maxstate.dempv,
maxstate.reppv);
printf("\n");
printf("%s\n", print_vote_total(arr, nlines));
11 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 11 of 15


}
and the expected output if the missing parts have been populated correctly is:
Alabama 795696 0 1255925 9
Alaska 122640 0 164676 3
Arizona 1025232 0 1233654 11
Arkansas 394409 0 647744 6
California 7854285 55 4839958 0



WestVirginia 238269 0 417655 5
Wisconsin 1620985 10 1407966 0
Wyoming 69286 0 170962 3
California wins with 7854285 (dem) and 4839958 (rep)
popular votes.
Dem. pop. vote total: 65915794
Dem. el. vote total: 332
Rep. pop. vote total: 60933504
Rep. el. vote total: 206
Dem. wins
(20 marks)
12 FEEG6002C1
Copyright 2021 v01 ? University of Southampton Page 12 of 15
Question 5:
This question will consider solving the predator-and-prey population dynamics
problem. The problem can be mathematically stated as follows. Let 1() be the
number (population) of rabbits and 2() the number of foxes. The time-dependence
of 1 and 2
is governed by the following conditions:

  • Rabbits proliferate at a rate , and per unit time a number 1
    is born.
  • Number of rabbits is reduced by collisions with foxes. Per unit time 12
    rabbits are eaten.
  • Birth rate of foxes depends only on food intake in form of rabbits.
  • Foxes die a natural death at a rate .
    This dynamics can be expressed by the following system of first order differential
    equations (ODE):
    1
    = 1 ? 12 = 1(1
    , 2)
    2
    = 12 ? 2 = 2(1
    , 2)
    The task is to solve this system of equations. To obtain the solution, assume:
  • Rabbit birth rate = 0.7
  • Rabbit-fox collision rate = 0.007
  • Fox death rate = 1
  • Initial conditions 1
    (0) = 70 and 2
    (0) = 50
  • Time interval from 0 to 30 (dimensionless)
    a) Write a function fode2(…) which returns type void and takes as input two
    arrays of type double. Each of the arrays has two elements. The first array stores the
    variables 1
    , 2
    . The second array stores the values of the right hand sides 1 and 2
    ,
    and serves as the output of the function fode2(…).
    (5 marks)
    b) Extend the function euler(…) given in lecture 8 as:
    void euler(double t, double y, double y0,
    double (*f)(double, double), int n)
    {
    int i;
    float dt = t[1] - t[0]; // step
  • FEEG6002C1
    Copyright 2021 v01 ? University of Southampton Page 13 of 15
    y[0] = y0; // initial condition
    for (i = 0; i < n; i++)
    {
    y[i + 1] = y[i] + dt * f(t[i], y[i]);
    }
    }
    to incorporate the arrays for populations of both rabbits and foxes, including the
    function fode2(…) developed above.
    (10 marks)
    c) In main(…) run the function euler(…) with appropriate inputs, and export the
    data for time and populations 1 and 2
    to the file data.txt. Use your favourite
    software to plot the data (see figure 1 below).
    (5 marks)
    An example of incomplete function main(…) could look like this:
    includeincludeincludedefine Ti 0.0 // initial timedefine Tf 30.0 // final timedefine dT 0.001 // time stepdefine Y01 70.0 // initial condition 1define Y02 50.0 // initial condition 2define A 0.7define C 0.007define B 1【FEEG6002C1 计算方法】

    int main()
    {


    for(i=0; i<=N; i++)
  • FEEG6002C1
    Copyright 2021 v01 ? University of Southampton Page 14 of 15
    {
    t[i] = Ti + dT*i; // define time
    }
    euler(y1, y2, fode2, N); // y1, y2 to store
    solutions
    // no explicit time
    dependence
    // print to file
    for(i=0; i<=N; i++)
    {
    fprintf(fw, "%f\t%f\t%f\n", t[i], y1[i], y2[i]);
    }


    }
    and when the missing parts have been populated the expected output is as shown in
    Figure 1 below. You can use this figure to compare with your solution.
  • FEEG6002C1
    Copyright 2021 v01 ? University of Southampton Page 15 of 15
    Figure 1 – Example of the plot calculated using the extended Euler algorithm
    assuming parameters listed in the figure title. Populates of rabbits and foxes reach a
    steady state with a phase lag.
    (20 marks)
    END OF PAPER

    推荐阅读