CS计算机代考程序代写 matlab CS/SE 4X03 — Assignment 4

CS/SE 4X03 — Assignment 4
Ned Nedialkov 29 March 2021
Due date: 12 April 2021
Problem 1 [9 points] Study sections 1 to 4 and 6 of https://arxiv.org/abs/1801. 05894 and also the corresponding lecture slides.
Unzip the file NN_code.zip
[2 points] Modify the netbp.m code so you can pass parameters as follows
function cost = netbp2(neurons, data, labels, niter, lr, file)
%Trains a neural network with 4 layers.
%Layer 1 and layer 4 have 2 neurons, layer 2 has neurons(1) and
%layer 3 has neurons(2)
%data is a matrix with two rows. data(:,i) contains the (x,y)
%coordinates of point i.
%labels is a matrix with two rows. labels(:,i) is [1;0]
%if data(:,i) is in category A and [0;1] if it is in category B.
%lr is learning rate
%niter is maximum number of iterations
%file is a filename where the file is created by
%save(file,’W2’,’W3’,’W4’,’b2’,’b3’,’b4’)
%cost is a vector storing the value of
%the cost function after each iteration; cost(j) is the cost at
%iteration j
[2 points] Implement the Matlab function
function category = classifypoints(file, points)
%Reads parameters from file and classifies points into two
%categories, A or B. This file is created by netbp2.m.
%points is a matrix with two rows, where points(:,i) contains the
%(x,y) coordinates of point i.
%Returns vector category, where category(i) is 1 if
%points(1,i) >= points(2,i) and 0 otherwise.
1

[3 points] Experiment with the values for X, Y and Z in the script main_nn.m. Try to find values such that in the plot classified.eps the grey region contains only A training points, and the white region contains only B training points. Note: your regions can be quite different from the regions in Figure 1.
[2 points] Summarize in 4 bullets what you have found useful/interesting in this problem.
Submit
• PDF: netbp2.m, classifypoints.m, the two plots produced by main_nn.m • Avenue: netbp2.m, classifypoints.m
1
10
0
10
-1
10
10
Problem 2 [5 points] To draw a circle of radius r on a graphics screen, one may proceed to evaluate pairs of values x(θ) = r cos θ, y(θ) = r sin θ for a succession of values θ. This can be computationally expensive because of the evaluation of the cos and sin functions.
cost
-2
0 2 4 6 8 10
iteration 105
Figure 1: Learning rate vs iteration number and classified points
Since
a cheaper method may be obtained by considering the ODE
dx =−rsinθ=−y and dy =rcosθ=x, dθ dθ
x′ =−y, x(0)=r y′=x, y(0)=0
Carry out this integration using a uniform step size h = .02 for 0 ≤ θ ≤ 120 and r = 1, applying forward Euler, backward Euler, and the implicit trapezoidal method. For each of the methods, plot the computed y versus x.
Can you provide an intuitive explanation why the trapezoidal works so much better? 2

Submit
• PDF: the 3 plots, your discussion.
• Avenue: all your Matlab code. Provide a main program main2.m such that when
executed these plots will be produced.
Problem 3 [5 points]
The Kermack-McKendrick SIR model
The Kermack-McKendrick model for the course of an epidemic in a closed population is given by the system of ODEs
dS = −βSI (1) dt
dI = βSI − γI (2) dt
dR = γI, (3) dt
where t is time, and at time t,
• S(t) is the number of people that are susceptible of being infected
• I(t) is the number of infected people
• R(t) is the number of people who are not infections (e.g. vaccinated or developed immunity)
The parameter β > 0 is infection rate and the parameter γ > 0 is recovery rate (see below). This is an SIR (Susceptible, Infected, Recovered) model, and one of the fundamental
models in epidemiology.
If we add the equations (1,2,3) we have
d (S + I + R) = 0. dt
That is, the total population does not change over time. Suppose we have initial values, at timet=0,S(0)=S0 >0,I(0)=I0 >0,andR(0)=0. Thenatanyt,
Let
S(t)+I(t)+R(t)=S0 +I0.
R0 = β. γ
From (2), I(t) will increase from its initial value when
I′(0) = βS(0)I(0) − γI(0) = γI0(R0S0 − 1) > 0.
3

That is, when R0S0 > 1, and will decrease when R0S0 < 1. The constant γ is the fraction of the population that will recover per day. For example, if for COVID-19 there are d = 14 days to recover, γ = 1/d = 1/14 of the infected population will recover per day. Using Matlab’s ode45, simulate (1,2,3) for t ∈ [0, 200] with a population of size n = 105 and initial conditions I0 = 100, S0 = n−I0, R0 = 0. Use γ = 1/14. Experiment with various values for β. For each of R0S0 ∈ {0.9, 2, 3, 5.7}, plot S(t), I(t), R(t) versus t. Submit • PDF: the four plots in your PDF • Avenue: all your code. Provide a main program main3.m such that when executed these plots are produced. Problem 4 [5 points] Consider two bodies of masses μ = 0.012277471 and μˆ = 1 − μ (Earth and Sun) in a planar motion, and a third body of negligible mass (moon) moving in the same plane. The motion is given by u′′ = u + 2u′ − μˆ 1 1 2 u′′ = u − 2u′ − μˆ u1 + μ ((u1 + μ)2 + u2)3/2 u2 ((u1 + μ)2 + u2)3/2 − μ − μ (u1 − μˆ) 􏰀(u1 − μˆ)2 + u2􏰁3/2 u2 . 􏰀(u1 − μˆ)2 + u2􏰁3/2 2 2 1 The initial values are u1(0) = 0.994, u2(0) = 0, u′1(0) = 0, u′2(0) = −2.001585106379082522420537862224. Implement the classical Runge-Kutta method of order 4 and integrate this problem on [0,17.1] with uniform stepsize using 100, 1000, 10,000, and 20,000 steps. Plot the orbits for each case. How many uniform steps are needed before the orbit appears to be qualitatively correct? Submit • PDF: plots and discussion. 4