Professional Documents
Culture Documents
? Answerbook - JL - Pluto - JL
? Answerbook - JL - Pluto - JL
jl
Answerbook
Candidate no. 276299
1 begin
2 using LinearAlgebra,PlutoUI,Pluto, Distributions, Interact, Plots,
DifferentialEquations, Symbolics
3 end
Selection deleted
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 1/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Table of Contents
Answerbook
Candidate no. 276299
Question 1
Part (a)
Part (b)
Part (c)
Part (d)
Part (e)
Part (f)
Part (g)
Question 2
Part (a)
Part (b)
Part (c)
Part (d)
Question 3
Selection deleted
Game 1
Game 2
Question 4
Part (a)
Part (b)
Part (c)
Part (d)
Part (e)
Part (f)
Part (g)
Part (h)
Part (i)
Question 5
Part (a)
Part (b)
Part (c)
Part (d)
Part (e)
Part (f)
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 2/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Question 1
Part (a)
my_transf (generic function with 1 method)
1 # (a) Defining my_transf(A)
2
3 function my_transf(A)
4 A_transpose = transpose(A) #transpose of matrix A
5 sum_matrix = A + A_transpose #sum
6 return sum_matrix
7 end
Part (b)
eigprod (generic function with 1 method)
1 # (b) Defining eigprod(A::Matrix,i::Number,j::Number)
Selection
2 deleted
3 function eigprod(A::Matrix, i::Number, j::Number)
4 eigenvalues, eigenvectors = eigen(A)
5 eigenvec_i = eigenvectors[:, i]
6 eigenvec_j = eigenvectors[:, j]
7 dot_product = dot(eigenvec_i, eigenvec_j)
8 return dot_product
9 end
Part (c)
5
1 # (c) Creating a 4x4 Matrix using a normal distribution
2 begin
3 μ = 10 #mean
4 σ = 5 #standard deviation
5 end
A = 4×4 Matrix{Float64}:
16.9724 5.75398 6.3386 0.497352
16.1124 16.2369 11.1407 5.06605
9.04005 7.42089 13.351 1.30722
8.33227 5.19501 11.9601 14.2458
1 A = μ .+ σ * rand(Normal(), 4, 4)
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 3/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
-1.6653345369377348e-16
1 begin
2 i = 2
3 j = 3
4 partc = eigprod(my_transf(A), i, j)
5 end
The answers are almost close to zero. This may show that the eigenvectors are nearly orthogonal or
close to perpendicular to each other. If the dot product is zero, the eigenvectros are interpreted to be
perpendicular.
Part (d)
[-0.319736+0.0im, 0.234666+0.0im, -0.0409907+0.0im, 0.917072+0.0im]
1 begin
2
3 eigenvalues, eigenvectors = eigen(A)
4 eigenvec_i = eigenvectors[:, i]
Selection
5 deleted
eigenvec_j = eigenvectors[:, j]
6
7 vi = transpose(eigenvec_i)
8 vj= eigenvec_j
9 end
4×4 Matrix{Float64}:
0.0 10.3585 2.70145 7.83492
-10.3585 0.0 -3.7198 0.128961
-2.70145 3.7198 0.0 10.6529
-7.83492 -0.128961 -10.6529 0.0
1 begin
2 A_transpose = transpose(A)
3 sub= A_transpose - A
4 end
2.5459671863987667 + 1.7540483520627983im
1 partd
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 4/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
This complex result implies a non-zero value with both real and imaginary components, indicating
that the operations performed on these vectors and matrices have led to a result that's not purely
real.
Part (e)
1 begin
2
3 v1 = eigenvectors[:, 1] # assuming v1 is the first eigenvector obtained
4
5 #extarcting eigenvectors
6 vn = eigenvectors[:, 2:end]
7
8 g = randn(size(vn, 2))
9
10 # calculating the weighted sum
11 weighted_sum = vn * g
12
13
14 dotprod = dot(v1, weighted_sum)
15
16 # checking if the dot product is approximately zero
17 if isapprox(dotprod, 0.0)
Selection
18 deleted
println("v1 is linearly dependent on the other eigenvectors.")
19 else
20 println("v1 is linearly independent on the other eigenvectors.")
21 end
22
23 end
Part (f)
The linear independence of all eigenvectors is a crucial property in determining the dimension of the
vector space spanned by these eigenvectors. In this case, if all the eigenvectors v1, v2 ...v3 are linearly
independent, the dimension of the vector space they span will be equal to the number of linearly
independent eigenvectors.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 5/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (g)
The 3 vectors are :
These vectors will form a basis of if and only if they are linearly independent. This means that
the only solution to:
is .
Reference: https://medium.com/swlh/how-to-understand-linear-independence-linear-algebra-
8bab1d918509
In matrix form;
Selection deleted
Simplifying;
The three vectors will form basisof if the determinant is non-zero. This would imply that the
vectors are linearly independent.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 6/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
The determinant will be zero if . This means the value of must not be an odd multiple
of since
Question 2
Part (a)
The overuse and factory faults are given by;
The probability of CPT surviving the first 100 months without a fault can be expressed as;
P(S): 0.5591545797410881
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 7/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (b)
The overuse and factory faults are given by;
The plot below shows simulated results from the function factory_simulation . The function
calculates faults based on the probability formulas and , removes the faulty CPTs each
month, and stores the remaining functioning units in the vector .
Selection deleted
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 8/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
1 begin
2 function factory_simulation(n0)
3 n = n0 #number of functioning cpts
4 v = Vector{Int64}(undef, 100)
5
Selection deleted
6 for t in 1:100
7 p_overuse = 0.0001 * (t^2)/(1+t)
8 p_factory = 0.01 * (1 + (1-t)/(1+t))
9 faults = rand(Binomial(n, p_overuse + p_factory), 1)[1]
10
11 n = n - faults
12 v[t] = n
13 end
14 scatter(1:100, v, xlabel="Month", ylabel="Functioning CPTs", ylims=(0,n0),
label=false)
15 end
16
17 factory_simulation(300)
18 end
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 9/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (c)
The overuse and factory faults are given by;
Below is the Code block for function profit(x,y) that calculates the expected profit for 100 items if
are invested in reducing factory faults, and are invested in reducing overuse faults.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 10/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (d)
The maximization the profit function while adhering to the constraint of investing exactly
£10 per CPT in fault reduction can be expressed as;
where and are the investments in reducing factory faults and overuse faults respectivey.
To maximize the proit function , the aim is to locate the values and where the
gradient/Jacobian
This condition signifies a stationary point where altering investments in any direction does not
further enhance profit.
Selection deleted
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 11/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Question 3
Game 1
Part (a)
The outcome space consists of the possible values for the two uncertain events:
The box containing the money, which can take values { , , } with equal probability:
The chosen box, which can also take values { , , } with equal probability:
Part (b)
To calculate the expected reward and variance :
Therefore,
For
For
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 12/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Game 2
Part (c)
With the stick strategy:
Part (d)
With K boxes containing money out of N total:
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 13/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Question 4
data = 1001×3 Matrix{Float64}:
0.0 0.825371 0.820219
0.01 1.25126 0.880475
0.02 1.07447 1.19519
0.03 1.14595 1.36702
0.04 0.74069 0.820193
0.05 0.803285 0.802094
0.06 1.04234 0.996427
⋮
9.95 0.960251 2.89815
9.96 0.958667 2.57985
9.97 1.14899 2.78071
Selection deleted
9.98 1.09621 3.08052
9.99 0.635545 2.79623
10.0 0.857159 2.38937
1 basic_model = parameterise_basic_model([1,4,1,4]);
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 14/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (a)
To find the maximum predator population for which prey births still outstrip deaths, we need to set
the prey population growth rate equal to zero and solve for the predator population.
Where;
Therefore, the maximum predator population for which prey births still exceed deaths is
.
Part (b)
The predator population is given by:
Where;
The predator population is shrinking when the predator death rate is greater than the predator birth
rate. In the given model, this happens when .
Part (c)
In the absence of predators ( ), the dynamics of the prey population over time are
governed by the equation:
Selection deleted
Since this is an exponential function of time, the solution exhibits exponential growth. The rate of
growth is dictated by the parameter . The prey population increases exponentially over time. This is
reasonable in the absence of predators, as prey experience unlimited growth when there are no
predators to control their population.
Part (d)
Here;
1.
2.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 16/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
1.
2.
The parameter can be interpreted as the maximum prey population size that can exist in the
absence of predators. It is also to be noted that the differential equation yield a solution with no 'a'
parameter, which indicates that the prey growth rate does not increase. This may indicate a state of
equilibrium or stability.
Part (e)
The original model is:
Selection deleted
Where and are the number of prey and predators as individual animals.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 17/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Therefore, in terms of the new variables and , the updated basic model becomes:
Part (f)
Selection deleted
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 18/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
(1001, 2)
1 begin
2
3 function simulation(p)
4
5 preybirth_r,predation_r,predatorbirth_r,predatordeath = p
6
7 function predpreyrelation(du,u,parameter,t)
8 x, y = u
9 du[1] = preybirth_r*x - predation_r*x*y
10 du[2] = predatorbirth_r*x*y - predatordeath*y
11 end
12
13 trange = (0.0,10.0)
14 init = [1,1] #initialising population
15
16 t = range(trange[1],stop=trange[2],length=1001)
17
18 prob = ODEProblem(predpreyrelation,init,trange,p)
19
20 solution = solve(prob)
21
22 predpreyrelation= hcat(solution(trange[1]:0.01:trange[2])...)
23 #populations = hcat(sol(t)...)' #Reference
https://discourse.julialang.org/t/hcat-with-matrices-as-elements/59817
24 return predpreyrelation'
25 end
Selection deleted
26
27 p = [1,2,3,4]
28 population_size = simulation(p)
29
30 @show size(population_size)
31
32 end
size(population_size) = (1001, 2)
Part (g)
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 19/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
data_convert = 1001×2 Matrix{Float64}:
0.825371 0.820219
1.25126 0.880475
1.07447 1.19519
1.14595 1.36702
0.74069 0.820193
0.803285 0.802094
1.04234 0.996427
⋮
0.960251 2.89815
0.958667 2.57985
1.14899 2.78071
1.09621 3.08052
0.635545 2.79623
0.857159 2.38937
1 data_convert= data[:, 2:3]
2 #extracting only 2nd and 3rd column of the original data set, so that the arrays
can be broadcast to a common size.
1 begin
2 function mse(p)
3
4 simulated_data = simulation(p)
5
6 mse_val= sum((data_convert .- population).^2)/size(data_convert,1)
7
8 return mse_val
9 end
10
Selection
11 deleted
parameters = [1, 2, 3, 4]
12 error = mse(parameters)
13
14 println("Mean Squared Error: $error")
15
16 end
Part (h)
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 20/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
1 begin
2
3 function gradient(f::Function, x::Vector)
4 n = length(x)
5
Selection deltax = 0.01
deleted
6
7 function diffx(i::Integer)
8 z = zeros(n)
9 z[i] = deltax
10 return z
11 end
12
13 dfdx = zeros(n)
14 for i in 1:n
15 dfdx[i] = (f(x + diffx(i)) - f(x))/deltax
16 end
17 return dfdx
18 end
19
20 function gradient_descent(f, initial_params, stepsize, max_iter)
21 params = initial_params
22 mse_values = Float64[]
23
24 for iter in 1:max_iter
25 grad = gradient(f, params)
26 params -= stepsize * grad
27
28
29 mse_value = f(params)
30 push!(mse_values, mse_value)
31
32
33 if mse_value < mse_values[1] / 3
34 break
35 end
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 21/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
36 end
37
38 return params, mse_values
39 end
40
41
42 initial_params = [1, 4, 1, 4]
43 stepsize = 0.01
44 max_iter = 1000
45
46 gradmse(params) = mse(params)
47
48
49 optimized_params, mse_values = gradient_descent(gradmse, initial_params,
stepsize, max_iter)
50
51 simulated_data_at_p0 = simulation(initial_params)
52 simulated_data_at_pend = simulation(optimized_params)
53 scatter_data = data
54
55 p1 = plot()
56 plot!(p1, simulated_data_at_p0[:, 1], label="Simulation at p0")
57 scatter!(p1, scatter_data, label="Data")
58 title!("Comparison at p0")
59
60 p2 = plot()
61 plot!(p2, simulated_data_at_pend[:, 1], label="Simulation at pend")
Selection
62 deleted
scatter!(p2, scatter_data, label="Data")
63 title!("Comparison at pend")
64
65
66 p3 = plot(mse_values, xlabel="Iterations", ylabel="Mean Squared Error",
legend=false)
67 title!("Mean Squared Error over Iterations")
68
69
70 plot(p1, p2, p3, layout=(3, 1))
71
72 d
Part (i)
There can be multiple sets of parameters that can perfectly fit the given data.
If MSE=0, it suggests that the chosen parameter values result in a model that exactly reproduces the
given data.
Other sets can be searched by exploring the parameter space. This can be done by optimisation
techniques.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 22/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Question 5
Part (a)
The equation is ;
Equation becomes;
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 23/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Biologically, ρ represents the growth rate of species 2. The factor being in the denominator
indicatesthe inhibitory effect of species 1 on species 2.
The factor controls the population growth rate of species 2. A higher means species 2 has a faster
replication rate and can grow more rapidly.
Part (b)
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 24/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
1 begin
2
3 function simulate(du,u,t,param)
4 du[1] = u[1]*(1 - u[1] - a12*u[2])
5 du[2] = rho*u[2]*(1 - u[2] - a21*u[1])
6 end
7
8 a12 = 0.9 # parameters
9 a21 = 1.1
10 rho = 1.6
11 u0 = [0.1, 0.1] #defining initial condition
12 tspan = (0,20)
13
14 diffeq = ODEProblem(simulate,u0,tspan,(a12,rho,a21))
15 sol = solve(diffeq)
16
17 plot(sol, xlabel="τ", ylabel="Population", label=["u1" "u2"],
18 title="Predator-Prey Relation")
19 end
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 25/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (c)
If the growth rate is held constant, the species with the lower numerical value for its competition
coefficient or seems to have an advantage and grows to a larger population over time.
For example:
If rho = 1.6 for both species, and = 0.9, = 1.1, then species 1 grow while species 2 declines
since a21 can be interpreted as suppresive effects of species 1 on species 2.
If rho is held at 1.6 again, but = 1.1, = 0.9, then species 2 grows while species 1 declines.
So in other words, all else being equal, the species that is suppressed LESS by its competitor tends to
grow more successfully, since it experiences less negative pressure.
The degree of suppression mediated by and seems to play a key role in the population
dynamics whereas the value of rho facilitates the growth of species 2 from the oberservations.
Therefore, the supressive effects are more important.
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 26/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
1 begin
2
3 function simulatec(du,u,t,param)
4 du[1] = u[1]*(1 - u[1] - a12c*u[2])
5 du[2] = rhoc*u[2]*(1 - u[2] - a21c*u[1])
6 end
7
8 a12c = 1.1 # parameters
9 a21c = 0.9
10 rhoc = 1.6
11 u0c = [0.1, 0.1] #defining initial condition
12 tspanc = (0,20)
13
14 diffeqc = ODEProblem(simulatec,u0c,tspanc,(a12c,rhoc,a21c))
15 solc = solve(diffeqc)
16
17 plot(solc,label=["u1" "u2"],legend=:bottomright)
18
19 end
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 27/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (d)
Given the differential equations:
1. For :
Setting :
2. For :
Setting :
Case 1:
If , eq 4 becomes, .
So, the fixed point is and . This means that species 2 survives while species 1 goes
extinct.
Case 2:
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 28/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
So, the fixed point is and . This implies that species 1 survives while species 2 goes
extinct
Case 3: and
Case 4: and
Rearranging eq 2 gives;
Rearranging terms:
So,
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 29/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (e)
Given system of differential equations:
Rewriting as;
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 30/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
Part (f)
The Jacobian Matrix is given by:
To figure out the conditions on when the Jacobian matrix has appropriate eigenvalues, we will
calculate the trace and determinant of the jacobian matrix.
1-Trace:
2-Determinant:
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 31/32
1/16/24, 2:42 PM 🎈 answerbook.jl — Pluto.jl
localhost:1234/edit?id=29e85990-b45f-11ee-244a-43965294723c# 32/32