Julia代写 cannon

27/04/2023, 21:06
Assignment : training a cannon to hit a target
Table of Contents
Assignment 3: training a cannon to hit a target
Set up the dual data type and support functions The dual type
Write dual methods for the main operators used Dene the main cannon methods
Training the cannon using partial derivatives Plot the trajectory
Test ring
Set up the dual data type and support functions
localhost:1234/edit?id=df72acc0-e534-11ed-1fc9-2bf64d0e07b7 1/6

27/04/2023, 21:06
The dual type
1 struct D 2f
Write dual methods for the main operators used
3 4 5 6 7 8 9
import Base: +, /, -, *, convert, promote_rule, sin, cos, exp
+(x ,y /(x ,y -(x ,y *(x ,y
sin(x ) = cos(x ) = exp(x ) =
)=D(x.f.+y.f)
) = D((x.f[1]/y.f[1], (y.f[1]*x.f[2] – x.f[1]*y.f[2])/y.f[1]^2)) )=D(x.f.-y.f)
) = D((x.f[1] * y.f[1], (x.f[1] * y.f[2] + x.f[2] * y.f[1])))
D((sin(x.f[1]), cos(x.f[1]))) D((cos(x.f[1]), -sin(x.f[1]))) D((exp(x.f[1]), exp(x.f[1])*x.f[2]))
-(x )= convert(
D((-x.f[1], -x.f[2]))
promote_rule( Base.show(io
¦Å = D((0,1))
) = D((x,zero(x))) ) = D
) = print(io, x.f[1], x.f[2]>0 ? ” + ” : ” “, x.f[2],
Dene the main cannon methods
Float version
projectile_distance (generic function with 1 method)
1 2 3 4 5 6
function projectile_distance(v0 g = 9.81
t = 2*v0*sin(¦È)/g d = v0*cos(¦È)*t return d
Float vector version
localhost:1234/edit?id=df72acc0-e534-11ed-1fc9-2bf64d0e07b7 2/6
2s/m ni ytivarg ot eud noitarelecca #
46taolF:: 46taolF:: 46taolF::
)¦Å+x ,.g.e( noitidda hguorht laud ot snoisrevnoc ticilpmi rof lufesu #
noitaton ¦Å eht gnisu rebmun lauD eht yalpsiD #
}rebmuN:<{epyT:: }D{epyT:: laeR:: }D{epyT:: lauD ot sepyt tnereffid fo noitomorp dna noisrevnoc rof seluR # sevitavired edocne ot lauD eht fo dleif ¦Å eht esu ot sdohtem rotarepo eht etadpU # sepyt laud rof sdohtem rieht etadpu nac ew os srotarepo esab eht tropmi # }46taolF ,46taolF{elpuT:: riap evitavired-laer a si D # rebmuN :< 程序代写 CS代考 加微信: cstutorcs 27/04/2023, 21:06 projectile_distance (generic function with 2 methods) 1 function projectile_distance(params ) 3 v0 = params[1] 4 ¦È = params[2] 5 t = 2*v0*sin(¦È)/g 6 d = v0*cos(¦È)*t 7 return d Dual version projectile_distance (generic function with 3 methods) 1 2 3 4 5 6 function projectile_distance(v0 g = 9.81 t = 2*v0*sin(¦È)/g d = v0*cos(¦È)*t return d Float and vector versions return the output distance 149.79236806637945 1 projectile_distance(50.0, 0.1¦Ð) 149.79236806637945 1 projectile_distance([50.0, 0.1¦Ð]) Use the gradient function from the ForwardDiff module to calculate the partial derivatives for velocity and angle. This should only be used for debugging the aim function (see below) and should not be used directly. [5.99169, 412.343] 2 using ForwardDiff: gradient 3 gradient(projectile_distance, [50.0, 0.1¦Ð]) Training the cannon using partial derivatives Plot the trajectory localhost:1234/edit?id=df72acc0-e534-11ed-1fc9-2bf64d0e07b7 3/6 2s/m ni ytivarg ot eud noitarelecca # 2s/m ni ytivarg ot eud noitarelecca # Programming Help, Add QQ: 749389476 27/04/2023, 21:06 projectile_trajectory (generic function with 1 method) 1 2 3 4 5 6 7 8 9 using Plots function projectile_trajectory(v g=9.81 title="Projectile Trajectory", legend=false) t = range(0, 2*v*sin(¦È)/g, 1000) x = v*cos(¦È)*t y = v*sin(¦È)*t - 0.5*g*t.^2 plot(x, y, xlabel="Horizontal Distance (m)", ylabel="Vertical Distance 1 projectile_trajectory(50.0, 0.1¦Ð) Test ring Notice that the dual version of projectile_distance returns the distance travelled and the combined gradients for velocity and angle with respect to distance. However, if we call the gradient function we can get the partial derivatives, which when added gives the combined gradient. Your task is to gure out how to unpack these two partial derivatives from the dual version of projectile distance. Note that when we add ¦Å to a real number (e.g., velo+¦Å ) it is automatically converted to dual type. localhost:1234/edit?id=df72acc0-e534-11ed-1fc9-2bf64d0e07b7 4/6 ecnatsid lacitrev # ecnatsid latnoziroh # lavretni emit # 2^s/m ni ytivarg ot eud noitarelecca # 46taolF:: 46taolF:: 27/04/2023, 21:06 (149.792 + 418.335¦Å, [5.99169, 412.343], 418.335) 2 velo = 50.0 3 angle = 0.1¦Ð 4 result = projectile_distance(velo+¦Å, angle+¦Å) 5 grad = gradient(projectile_distance, [50.0, 0.1¦Ð]) 6 result, grad, sum(grad) The aim function takes exit velocity, angle, target distance, error tolerance, and learning rate as input (the last two have defaults). The core of the algorithm involves looping until a minimum error is reached. The velocity and angle parameters are adjusted by small amounts computed from the partial derivatives calculated. Your task is to implement a function aim that takes a velocity and angle as input and uses a simple gradient descent loop to accurately land the projectile on the target. Note that the additional arguments with default values are ¦Å (how close you want to land near the target in metres) and ¦Ç (the learning rate), which determines the size of the steps you take in adjusting the velocity and angle parameters. aim (generic function with 1 method) 1 2 3 4 5 6 7 8 9 function aim(velo return result end ; ¦Å = 1.0, ¦Ç = 0.001) A call to aim: aim(velo+¦Å, angle+¦Å, 100.0) 100.96020014869059 + 6.416092158996619¦Å localhost:1234/edit?id=df72acc0-e534-11ed-1fc9-2bf64d0e07b7 5/6 tegrat - ]1[f.tluser = rorre # ¦Å > )rorre(sba elihw #
程序代写 CS代考 加QQ: 749389476
27/04/2023, 21:06
A few points to note:
In order to do gradient descent correctly, you will need to extract partial derivatives of the output with respect to (a) the exit velocity and (b) the barrel angle and use them to adjust these parameters separately.
You must use the dual number denition provided here to obtain the gradients and not the ForwardDif module or any similar module provided by Julia.
For additional marks provide a visualisation of the increasing accuracy of the model as it’s trained.
Test your implementation on a target of 100 metres.
localhost:1234/edit?id=df72acc0-e534-11ed-1fc9-2bf64d0e07b7 6/6