联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2021-01-26 11:47

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:

for a 2x2 array a with elements a[0][0]=1, a[0][1]=0, a[1][0]=0,

a[1][1]=1. The function print_matrix(…) does not return any value.

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:

(??)????

?? = ??????.

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

| = ??00??11 ? ??01??10.

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

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.

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.

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.

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.

6

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<N; i++)

for(j=0; j<N; j++)

ainv[i][j] = a[i][j]; // 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:

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.

0.000000 0.000000

1.000000 0.000000

0.000000 1.000000

1.000000

0.000000

0.000000

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/2012_United_States_presidential_election.

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.

b) Write a structure votes which has the following members:

char state[100]; // state name

// democrats popular votes

// democrats electoral votes

// republicans popular votes

// republicans electoral votes

long dempv;

long demev;

long reppv;

long repev;

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.

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);

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.

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.:

795696 0 1255925 9

122640 0 164676 3

1025232 0 1233654 11

Alabama

Alaska

Arizona

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

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.

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.

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));

}

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

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 ????1??2

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 ? ????1??2 = ??1(??1

, ??2)

????2

???? = ????1??2 ? ????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(…).

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

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.

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).

An example of incomplete function main(…) could look like this:

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define Ti 0.0 // initial time

#define Tf 30.0 // final time

#define dT 0.001 // time step

#define Y01 70.0 // initial condition 1

#define Y02 50.0 // initial condition 2

#define A 0.7

#define C 0.007

#define B 1

int main()

{

for(i=0; i<=N; i++)

{

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.

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.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp