Starting from:

$25

CS-SE4X03 - Assignment 4 - Solved

Study the slides about deep learning and sections 1 to 4 and 6 of https://arxiv.org/abs/1801.05894.
[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
 

 
[4 points] Run the function
function main_nn(neurons, learning_rate, niter)
with various parameters. Try to find parameters to this function such that in the plot classified.eps the grey region contains only A training points, and the white region con-tains only B training points. Note: your regions can be quite different from the regions in Figure 1.
[1 point] Summarize in 4 bullets what you have found useful/interesting in this problem. 
 
 
Figure 1: Classified points
Problem 2 [7 points] Implement in Matlab the bisection method for finding a root of a scalar equation.
Consider the polynomial
f(x) = (x − 2)9
= x9 − 18x8 + 144x7 − 672x6 + 2016x5 − 4032x4 + 5376x3 − 4608x2 + 2304x − 512.
(a)    [2 points] Write a Matlab script to evaluate this function at 161 equidistant points in the interval [1.92, 2.08] using two methods:
– evaluate (x − 2)9 directly
– evaluate x9 − 18x8 + 144x7 − 672x6 +2016x5 − 4032x4 + 5376x3 − 4608x2 + 2304x −512 using Horner’s method
Plot the results in two different plots. Explain the differences between the two plots.
(b)    [2 points] Apply your bisection method to find a root starting with [1.92, 2.08], tolerance 10-6, and using the Horner’s evaluation.
Obviously, r = 2 is a root of multiplicity 9. Can you determine a value for r such that |r − 2| < 10-6?
(c)    [2 points] If you cannot find a root with accuracy of 10-6 can you explain why?
(d)    [1 point] Apply Matlab’s fsolve to find a root of
x9 − 18x8 + 144x7 − 672x6 + 2016x5 − 4032x4 + 5376x3 − 4608x2 + 2304x − 512 using initial guess 1.9 and of (x − 2)9 with initial guess 1.9.
Report your results.

Problem 3 [8 points] Implement Newton’s method for systems of equations.
Each of the following systems of nonlinear equations may present some difficulty in com¬puting a solution. Use Matlab’s fsolve and your own implementation of Newton’s method to solve each of the systems from the given starting point.
In some cases, the nonlinear solver may fail to converge or may converge to a point other than a solution. When this happens, try to explain the reason for the observed behaviour.
Report for fsolve and your implementation of Newton’s method and each of the systems below, the number of iterations needed to achieve accuracy of 10_6 (if achieved).
(a)    
x1 + x2(x2(5 − x2) − 2) = 13 x1 + x2(x2(1 + x2) − 14) = 29 starting from x1 = 15, x2 = −2.
(b)    
x2 1 + x2 2 + x2 3 = 5
x1 + x2 = 1 
x1 + x3 = 3
starting from x1 = (1 + √3)/2, x2 = (1 − √3)/2, x3 = √3.
(c)    
x1 + 10x2 = 0

5(x3 − x4) = 0
(x2 − x3)2 = 0

10(x1 − x4)2 = 0
starting from x1 = 1, x2 = 2, x3 = 1, x4 = 1.
(d)    
104x1x2 = 1
e_X1 + e_X2 = 1.0001
starting from x1 = 0, x2 = 0.

Problem 4 [4 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
u1 + µ    (u1 − ˆµ)
u00 1 = u1 + 2u0 2 − µˆ ((u1 + µ)2 + u2 2)3/2 − µ ((u1 − ˆµ)2 + u2 )3/2
2
u2    u2
u00 2 = u2 − 2u0 1 − µˆ     )3/2 .
((u1 + µ)2 + u2 2)3/2 − µ ((u1 − ˆµ)2 + u2 2
The initial values are
u1(0) = 0.994,
u'1(0) = 0,
u2(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, i.e. u2 versus u1 for each case. How many uniform steps are needed before the orbit appears to be qualitatively correct.

 

Problem 7 [10 points] For this problem use the file nbody.dat from Assignment 3. Write the body of the Matlab function
function T = findPeriod(t, x, y, z)
% FINDPERIOD finds the period of an object in a periodic
% motion.
% t is a vector with time instants.
% The position at time t(i) is x(i), y(i), z(i).
% If the data is not periodic, or if a period cannot be computed,
% then -1 is returned.
% The spacing between t(i) and t(i+1) is not necessarily
% uniform.
%
% The initial position is at time t(1) and it is given by
% x(1), y(1), z(1).
% This function finds (an approximation) for the period T such that
% at times t(1), t(1)+T, t(1)+2*T ...
 
°h the object is at position x(1), y(1), z(1) again. °h Calculate T here
T = -1;
end
(a)    [5 points] Describe your algorithm in words and pseudo code.
(b)    [5 points] Run the script script main_nbody.m. Validate your computations against the data at
http://www.windows2universe.org/our_solar_system/planets_table.html.
You will receive full marks if your results are accurate up to the first digit after the decimal point. E.g. the period of Uranus is given as 84.01; full mark is if you obtain 84.0.
Submit
•    PDF: the output of this script, findPeriod.m.
•    Avenue: all your MATLAB code.
Problem 8 [4 points] The Kermack-McKendrick model for the course of an epidemic in a closed population is given by the system of ODEs
 
= —βSI    (3)
= βSI — γI    (4)
= γI,    (5)
 
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 (3,4,5) we have
d dt(S+I+R) =0.
 
That is, the total population does not change over time. Suppose we have initial values, at time t = 0, S(0) = S0 > 0, I(0) = I0 > 0, and R(0) = 0. Then at any t,
S(t)+I(t)+R(t)=S0+I0.
Let
β
R0 = .
γ
From (4), I(t) will increase from its initial value when
I'(0) = βS(0)I(0) − γI(0) = γI0(R0S0 − 1) > 0.
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 (3,4,5) for t E [0, 200] with a population of size n = 1000 and initial conditions I0 = 1, S0 = n − I0, R0 = 0. Use γ = 1/14. Experiment with various values for β. For each of R0S0 E {0.9, 1, 3, 5}, plot S(t), I(t), R(t) versus t.

More products