? 498DS_HW8.jl — Pluto.jl

pdf

School

University of Illinois, Urbana Champaign *

*We aren’t endorsed by this school

Course

498

Subject

Astronomy

Date

Dec 6, 2023

Type

pdf

Pages

14

Uploaded by DukeTurtleMaster954

Report
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 1 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a Drag coe ! cent inversion for spring system Table of Contents Drag coe f ficent inversion for spring system Exercise 1 Exercise 2 Exercise 3 The data below represents the results of a simulation a 1kg mass attached to two ideal springs in the presence of gravity (g = 9.8 m/s ² ) and atmosphere. The first column of the matrix represents the x- location of the mass and the second column represents the y-location. Each row of the matrix represents the position of the mass at a particular time, where measurements are taken every 0.2 seconds for a total of 10 seconds. begin using PlutoUI using ModelingToolkit using DifferentialEquations using Unitful using Plots using Statistics using ForwardDiff end
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 2 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a data 51 × 2 Matrix{Float64}: 0.25 1.0 0.437747 0.682967 0.552829 -0.0460413 0.577587 -0.841117 0.534314 -1.50408 0.449681 -1.95172 0.346577 -2.15179 0.283551 -1.2047 0.316922 -1.15042 0.3542 -1.14277 0.384657 -1.18264 0.399403 -1.257 0.394624 -1.34251 =
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 3 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a As mentioned above, measurements are taken every 0.2 seconds for ten seconds total: data = [ 0.25 1.0 0.4377465924013494 0.6829673488509393 0.5528288794955407 - 0.046041337245350866 0.5775866273254653 - 0.8411174896349062 0.534314036488778 - 1.50408426800897 0.44968064574833044 - 1.951720235095879 0.34657746037387643 - 2.1517872350254046 0.24738068204253338 - 2.096965970447781 0.1811337770708425 - 1.8298781268443733 0.1657060426545275 - 1.4633203431935093 0.19776735080175256 - 1.11626963493877 0.26229982187604595 - 0.8706947941310296 0.34089325684218746 - 0.7705885907666817 0.4134846767811591 - 0.8287952577953572 0.45858456727293473 - 1.0167458649461154 0.4643233205849137 - 1.264422767444129 0.43330593019067953 - 1.4950626709885648 0.3773036087859072 - 1.6507271649465327 0.31213691012836975 - 1.6986611182008937 0.2555152259510201 - 1.6323710560893412 0.22422809329469673 - 1.4781379272790591 0.22611012324430624 - 1.289143344424287 0.25782705117566795 - 1.1216697841550674 0.308741006882215 - 1.0184011847968226 0.36455605739566155 - 1.0024478351451087 0.40954689136165146 - 1.0743192792785576 0.4304803768231829 - 1.2082578313977814 0.42255836747726494 - 1.3603156240736176 0.38994661340111286 - 1.4866298916837077 0.34287245607428213 - 1.5547486332830547 0.2945266178635357 - 1.5491421094957891 0.25883841115227313 - 1.4740542441372286 0.24616132074107142 - 1.3551614462521346 0.25895170040642995 - 1.2297499422980673 0.29223169759815437 - 1.133379393040509 0.3359631143590702 - 1.090721902149426 0.37783032450182374 - 1.1116148988129861 0.40559398903042393 - 1.1879217873598134 0.41146751587853175 - 1.2944866092313017 0.3947439398064326 - 1.3988949468021286 0.3612311242545805 - 1.47203044495147 0.3207060256595176 - 1.4951236206958454 0.2847293685918732 - 1.4631391598513115 0.2638488352968759 - 1.387261449508944 0.2637554452815365 - 1.29159842144958 0.28355072296098927 - 1.204695596335689 0.316921734295927 - 1.1504155736222133 0.3542002496433684 - 1.142772760609613 0.38465735635346693 - 1.1826441004770756 0.3994028064168229 - 1.256996620802001 0.3946241735837883 - 1.3425133439213541 ]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 4 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a times 0.0:0.2:10.0 = The plot below provides some information on the simulation that was performed and its results. As mentioned above, the mass is 1kg. It is attached to two springs with spring constants k = 5.0 N/m and k = 2.5 N/m, respectively. The two springs are located at points x = 0.0 m and x = 1.0 m, respectively. The y-location of both anchors is 0.0. At the beginning of the simulation, the mass is released from the location (x=0.25 m, y=1.0 m) with a velocity in the x-direction (vx) of 1.0 m/s and a velocity in the y-direction (vy) of 0.0 m/s. Note that the initial location given here matches the first row in the data matrix above. times = 0 : 0.2 : 10
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 5 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a let if !( "GRADER" keys ( ENV )) anim = @animate for i 1 : length ( times ) # Get the x and y location for this time step. x = data [ i , 1 ] y = data [ i , 2 ] # Plot the current location plus other relevant information. plot ( data [ 1 : i , 1 ], data [ 1 : i , 2 ], c = :black , alpha = 0.2 , lab = :none ) scatter! ([ x ], [ y ], xlim =(- 0.25 , 1.25 ), ylim =(- 2.5 , 1.25 ), c = :fuchsia , ms = 8 , lab = "m = 1kg" , xlabel = "x (m)" , ylabel = "y (m)" , aspect = :equal , ) scatter! ([ 0.0 ], [ 0.0 ], c = :black , lab = "anchor 1 location = (0, 0) m" ) scatter! ([ 1.0 ], [ 0.0 ], c = :black , lab = "anchor 2 location = (1, 0) m" ) plot! ([ x , 0.0 ], [ y , 0.0 ], ls = :dashdotdot , c = :black , lab = "spring 1 constant: k = 5.0 N/m" ) plot! ([ x , 1.0 ], [ y , 0.0 ], ls = :dashdotdot , c = :black , lab = "spring 2 constant: k = 2.5 N/M" ) annotate! ( (- 0.24 , - 2.4 , text ( "time = $(times[i])s" , :bottom , :left , 10 )), ( 1.25 , - 2.4 , text ( "initial conditions:\nx = 0.25m\ny =1.0m\nvx =1.0m/s\nvy =0.0m/s" , :bottom , :right , 10 )), ) end gif ( anim , fps = 5 ) end end
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 6 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a Recall from Hooke's law that the force needed to extend or compress a spring by some distance scales linearly with respect to that distance—that is, , where is a constant factor characteristic of that spring. (The values for our two springs are given above.) Newton's laws of motion also apply here. In addition to the forces associated with gravity and the two springs, we also have a drag force acting on our mass because it is immersed in the atmosphere. We will use a simplified representation of drag force: where is the drag coe f ficient (units = ) and is speed (units = ). The direction of the drag force is always the opposite of the direction of the velocity. If you review the information provided so far, you may notice that you have been given all of the information needed to fully describe the motion of this spring system except for the drag coe f ficient . Your task for this homework question is to estimate based on the data given above and use it to predict how the system will act under di f ferent initial conditions. Exercise 1 Create a ModelingToolkit.ODESystem named spring_mass representing the spring-and-mass system described above. One of the parameters in the system should be named c_d ; this is the drag coe f ficient we will want to optimize to match the trajectory in the data given above.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 7 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 8 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a begin @variables t [ unit = u"s" ] @variables x ( t ) [ unit = u"m" ] @variables y ( t ) [ unit = u"m" ] @variables delta1 ( t ) [ unit = u"m" ] @variables delta2 ( t ) [ unit = u"m" ] @variables F1 ( t ) [ unit = u"N" ] @variables F2 ( t ) [ unit = u"N" ] @variables vx ( t ) [ unit = u"m/s" ] @variables vy ( t ) [ unit = u"m/s" ] @variables Fx ( t ) [ unit = u"N" ] @variables Fy ( t ) [ unit = u"N" ] @variables s ( t ) [ unit = u"m/s" ] @variables d ( t ) [ unit = u"N" ] @parameters g = 9.8 [ unit = u"m/s^2" ] @parameters m = 1.0 [ unit = u"kg" ] @parameters c_d [ unit = u"N*s^2/m^2" ] @parameters k1 = 5.0 [ unit = u"N/m" ] @parameters k2 = 2.5 [ unit = u"N/m" ] @parameters x10 = 0 [ unit = u"m" ] @parameters y10 = 0 [ unit = u"m" ] @parameters x20 = 1.0 [ unit = u"m" ] @parameters y20 = 0 [ unit = u"m" ] Dt = Differential ( t ) eqs = [ delta1 ~ ( ( x - x10 )^ 2 + ( y - y10 )^ 2 ), delta2 ~ ( ( x - x20 )^ 2 + ( y - y20 )^ 2 ), F1 ~ k1 * delta1 , F2 ~ k2 * delta2 , s ~ ( vx ^ 2 + vy ^ 2 ), d ~ c_d * s ^ 2 , Fx ~ - d * vx / s - k1 *( x - x10 ) - k2 *( x - x20 ), Fy ~ - d * vy / s - m * g - k1 *( y - y10 ) - k2 *( y - y20 ), Dt ( vx ) ~ Fx / m , Dt ( vy ) ~ Fy / m , Dt ( x ) ~ vx , Dt ( y ) ~ vy ] @named spring_mass = ODESystem ( eqs ) end
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 9 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a Looks promising! PrairieLearn will grade your answer. (So far we've only done some basic checks.) Exercise 2 Estimate the e f fective drag coe f ficient and save it as the variable c_d_effective . spring_mass_simplified = Enter cell code... Enter cell code... spring_mass_simplified = structural_simplify ( spring_mass )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 10 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a train! (generic function with 1 method) model_f (generic function with 1 method) uu 51 × 2 Matrix{Float64}: 0.25 1.0 0.437747 0.682967 0.552829 -0.0460413 0.577587 -0.841117 0.534314 -1.50408 0.449681 -1.95172 0.346577 -2.15179 0.283551 -1.2047 0.316922 -1.15042 0.3542 -1.14277 0.384657 -1.18264 0.399403 -1.257 0.394624 -1.34251 = function train! ( model_f , k ::T , y ::Matrix{T} , η ::T , nsteps ::Int ) where T <: AbstractFloat p = k function err ( p ) yhat = model_f ( p ) mean (( y .- yhat ).^ 2 ) end for i in 1 : nsteps g = ForwardDiff . derivative ( err , p ) p -= η * g end return p end function model_f ( p ) prob = ODEProblem ( spring_mass_simplified , [ vx => 1.0 , vy => 0.0 , x => 0.25 , y => 1.0 ], ( 0.0 , 10.0 ), [ c_d => p ]) sol = solve ( prob , saveat = 0.2 ) return [ sol [ x ] sol [ y ]] end uu = model_f ( 0.5 ) Enter cell code... Enter cell code...
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 11 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a c_d_effective 0.4999597522744089 = Looks promising! PrairieLearn will grade your answer. (So far we've only done some basic checks.) Exercise 3 Using the spring_mass system you created in Exercise 1, and the drag coe f ficient c_d_effective you estimated in Exercise 2, perform a simulation of the trajectory of the mass if the initial conditions are: vy = 10.0 m/s vx = 0.0 m/s x = 0.0 m y = 0.0 m Make a plot of your simulation trajectory (saving outputs every 0.2 seconds like above) where the x- axis represents the x-location of the mass and the y-axis represents the y-location of the mass, similar to the animation at the beginning of this notebook, except it doesn't need to be an animation and you only need to plot the equivalent of the grey trajectory line in that plot, not any of the other stu f f. Save your plot as the variable new_trajectory_plot . Enter cell code... c_d_effective = train! ( model_f , 0.5 , data , 0.001 , 1000 )
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 12 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a 0.0 0.0 10.0 0.0 0.0 0.2 0.378736 3.02884 0.0405371 1.19384 0.4 0.675052 -1.15755 0.147588 1.37007 0.6 0.650534 -3.97438 0.286614 0.817527 0.8 0.399499 -4.54908 0.392873 -0.0647949 1.0 0.146455 -3.90627 0.446703 -0.922457 1.2 -0.0511938 -2.7991 0.455248 -1.59744 1.4 -0.192672 -1.52115 0.429976 -2.03111 1.6 -0.282765 -0.183172 0.381554 -2.20196 1.8 -0.292063 1.08295 0.32217 -2.10751 1 2 3 4 5 6 7 8 9 10 more timestamp vx(t) vy(t) x(t) y(t) begin prob1 = ODEProblem ( spring_mass_simplified , [ vx => 0.0 , vy => 10.0 , x => 0.0 , y => 0.0 ], ( 0.0 , 10.0 ), [ c_d => 0.5 ], ) sol1 = solve ( prob1 , saveat = 0.2 ) end
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 13 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a new_trajectory_plot = Looks promising! PrairieLearn will grade your answer. (So far we've only done some basic checks.) That's it! Utility functions are below. not_defined (generic function with 1 method) new_trajectory_plot = plot ( sol1 [ x ], sol1 [ y ])
11/29/21, 4 : 16 PM 498DS_HW8.jl — Pluto.jl Page 14 of 14 http://localhost:1234/edit?id=6f665a8e-5161-11ec-11cd-e95af94f1a7a