Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Scientific Computation

Spring 2023

Project 2

Due: Monday March 6th, 1pm

In addition to this project description, there are 2 files for this assignment:

• project2.py: a Python file which you will complete and submit on Blackboard (see below for details)

• report2.tex: a Latex template file for the short report which you will submit. The discussion and figure(s) described below should be placed in this report.

Part 1

1.  (3 points) Analyze the function part1q1 included in project2.py which works with a list (Hlist), a dictionary (Hdict), and the heapq package.  There are three options. Setting option=0 leads to an initialization step which returns an Hlist and Hdict which can then be used as input when option=1 or option=2.  Carefully explain the tasks carried out by the function when  option=1 or option=2, and estimate the asymptotic running time when option=2.  When analyzing the running time, assume that the length of Hlist is n = 2m  − 1 where m is a positive integer.  You do not need to discuss the option=0 case.

2.  (5 points)

(a) Now analyze the function part1q2 which takes a weighted NetworkX graph as

input along with two integers corresponding to nodes in the graph (see function documentation for more details).  Explain both the problem that the code is attempting to solve and the strategy that it is using to solve this problem. You may refer directly to particularly important parts of the code, but a line-by- line analysis is not needed. A constraint is needed for the graph edge-weights for the code to reliably produce useful results.  Provide such a constraint in your report along with a concise explanation of why it is needed.

(b) The implementation in part1q2  contains one or more tangible inefficiencies

which negatively affect the running time when the function is applied to large sparse graphs. Develop an improved version of the function which resolves the most important inefficiency (or inefficiencies) that you identify.  Place your improved code in part1q3, and add a brief explanation of the changes you have made and how they improve the asymptotic running time of the code. If you identify several inefficiencies, choose the two you consider to be the most important when deciding how to change the code. Note: You should not use any NetworkX functions, and you should not use built-in functions to improve the walltime of the code; the focus here should be on improving the running time.

Part 2

Consider the following model for opinion dynamics in a group of n individuals:

 =   f(xi xj ), i = 1, 2, ...,n              (1)

f(xi xj ) = (xi xj )exp [−(xi xj )2] .               (2)

Here, xi  is the real-valued opinion of individual i; xi   = 0 indicates a neutral opinion, xi  > 0 indicates a positive opinion, and xi  < 0 indicates a negative opinion.

1.  (2 pts) Complete the function RHS in part2q1  so that numerical solutions to the model are efficiently computed using solve ivp; the initial condition is set within the function.

2.  (3 pts) Set n = 50, and find an equilibrium solution, x, which satisfies the following conditions:

i. The magnitudes of the elements in x should satisfy, 0  <  |xi | ≤ 1000  (i = 1, 2, ...,n)

ii. x should contain at least n/2 = 25 distinct values

Add the code used to generate x to the function part2q2.  The function includes code for saving your solution, and you should submit the file that is created.  In your report, briefly explain your approach to finding x and include a description of the Python tool(s) that you used along with an explanation of how you checked the accuracy of your solution.  Note:  a simple but acceptable approach is to attempt to generate several equilibrium solutions and select the first one that satisfies the given conditions.

3.  (3 pts) Analyze the behavior (or types of behavior) that can occur when a small perturbation is added to the equilibrium solution that you found in question 2 (for example, are oscillatory dynamics expected?).  You should focus on providing a qualitative description of how the system responds to small perturbations, and full solutions for the response to a specific perturbation do not need to be computed. Add the code you develop for your analysis to the function, part2q3, and add a clear and concise explanation of your analysis and conclusions to your report.  Note:  if you were unable to find a solution to 2.   which satisfied the given constraints, you should relax the constraints so that a non-trivial equilibrium solution can be generated for this question.

4.  (4 pts) Now consider the following stochastic opinion model:

dxi  = dt  f(xi − xj ) + µdWi (t), i = 1, 2, ...,n             (3)

f(xi xj ) = (xi xj )exp [(xi xj )2] .                       (4)


(a)  Complete the function part2q4  so that it uses the Euler-Maruyama method

to carry out m simulations of this model for t = j∆t, j  = 1, 2, ...,Nt.  See the function documentation for further details on the function input and out- put. Code for generating the initial condition has been provided. Your code’s efficiency will not be assessed (though you should still aim to make it efficient).

(b) Investigate the influence of µ on the resulting opinion dynamics. You should

compare computed results for the deterministic model with results for the stochastic model with µ = 0.2 and tf = 50. In your report, discuss if there are significant changes in the dynamics (aside from individual solutions becoming “noisy”). Your analysis should be supplemented by 1-2 well-designed figures. Add a concise discussion of your findings and figure(s) to your report.  You may consider other values of µ and tf if it is helpful for your analysis, but this is not required.

Notes for Part 2: It may be helpful to develop and test your codes with a small value of n rather than n = 50.  Figures are not required for questions 2 and 3 but may be included in your report to supplement your discussion.

Further guidance

• You should submit both your completed python file (project2.py),a pdf containing your discussion and figure(s) (report2.pdf ). You are not required to use the provided latex template,  any well-organized pdf is fine.   You should also submit the file containing your solution for part 2, question 1 (xeq.txt). To submit your assignment, go to the module Blackboard page and click on“Project 2” . There will be an option to attach your files to your submission.  (these should be named project2.py and report2.pdf). After attaching the files, submit your assignment.

• Please do not modify the input/output of the provided functions without permis- sion.  You may use numpy, scipy, matplotlib, networkx, time, timeit, collections, and heapq as needed except as noted above for part 1 question 2.   Otherwise, please do not import any modules without permission. You may create additional functions as needed, and you may use any code that I have provided during the term.  Some functions have input variables with specified default values.  You are not required to use these values unless the relevant question explicitly states that you are.

• Marking will be based on the correctness of your work and the degree to which your submission reflects a good understanding of the material covered up to the release of this assignment.  Excluding figures, you should aim to keep the pdf version of your report to less than 5 pages.

• Open-ended questions require sensible time-management on your part.   Do not spend so much time on this assignment that it interferes substantially with your other modules.  If you are concerned that your approach to the assignment may require an excessive amount of time, please get in touch with the instructor.

• Questions on the assignment should be asked in private settings.  This can be a “private” question on Ed (which is distinct from anonymous”) or by arrangement with the instructor.

• Please regularly backup your work. For example, you could keep an updated copy of your files on OneDrive.

• In order to assign partial credit, we need to understand what your code is doing, so please add comments to the code to help us.

• You have been asked to submit code in Python functions, but it may be helpful to initially develop code outside of functions so that you can easily check the values of variables in a Python terminal.

• The weightings for assignments can be found in the lecture 1 slides.