DATA 420 — Modeling and Simulation — Spring 2024

Numerical calculus practice

Possible experience: +10XP (or even +15XP)

Due: Thu, Feb 1st, midnight

Overview

In this mini coding assignment, you'll play around with the numerical calculus code we did in class, and add a bit to it.

Supporting files

The file roadtrip.py contains the code we wrote in class, polished up a little. Assuming you cloned the class github repo for the previous assignment, you should be able to just type "git pull" and it will populate your directory with this file and any other recent changes. If you didn't, and you still don't want to do things the real-world way, you can again choose not to clone the repo and again do a copy/paste of the code to your machine.

Your mission

After obtaining the file named roadtrip.py via any of the above strategies, make a copy of it named yourumwid_roadtrip.py. You can use a copy command, OS right-click operation, "Save as..." operation in your IDE, or any other way you can think of to make this copy. (Please do not name your copied file anything else. Please do not omit the suffix, or change the capitalization, or add/remove underscores, or do any other clever or creative things. Please call it exactly yourumwid_roadtrip.py, with your actual (lowercase) UMW Net ID substituted for "yourumwid". For instance, "jsmith19_roadtrip.py" is a correct name.)

You'll write all your code in the newly-created yourumwid_roadtrip.py file. The original roadtrip.py file can and should stay put.

Now, run your yourumwid_roadtrip.py file. (In an IDE, it might be a little green arrow button, or the F5 key. On the command line, it will be the command "python yourumwid_roadtrip.py".) Make sure you get a plot similar to the one I displayed in class:

Yours won't look exactly like this, of course, since you'll have generated different random driving speeds on line 12. But it should look somewhat like this.

You are now ready to begin thinking in earnest.

Requirements

First, open a new text file (in vim, Notepad++, Spyder, Microsoft Word, or anything else) called "roadtrip_answers.md". You'll put all your written answers to the questions below in this file.

Now make the following additions/enhancements/changes to the simulation:

  1. Increase the simulation's resolution by taking speed samples every minute of the 9-hour drive, instead of every hour of the 9-hour drive. (Reading the documentation on the np.random.uniform function might come in handy.) Produce this plot, title it "Item #1," and save it in a file called "roadtrip1.png". (You can add a title to a plot with the code "plt.title('some compelling title')" somewhere before plt.show().)

    Note that this should still be a 9-hour trip and should still go for about 500 miles. If your plot shows the drive taking hundreds of hours or only seconds, or going thousands of miles or only down the block, you've done it wrong.

    In your roadtrip_answers.md file, answer this question: (1) is the distance line smoother or less smooth now, and why?

  2. Add code to differentiate the speed and thereby compute the car's acceleration over time. Add this to the plot and legend in the color orange. Title this plot "Item #2" and save it in "roadtrip2.png".

    In your roadtrip_answers.md file, answer these questions: (2a) what units is this acceleration measured in? (2b) in this simulation, is distance ever negative? is speed? is acceleration? Explain all three answers.

  3. Extra credit: the cops on I-70 are fierce. Modify the simulation so that any time Stephen drives over 75 mph for 4 minutes in a row or more, he's pulled over by a cop and has zero speed for the next half hour. Title this plot "Item #3" and save it in "roadtrip3.png". (Obviously Stephen is not guaranteed to get pulled over every time he drives to Wichita, so make sure that your plot does show at least pullover. If it doesn't, run the simulation again until you get at least one.)

    In your roadtrip_answers.md file, answer this question. (3) If Stephen gets enough traffic tickets, he's not going to make it all the way to Wichita. How would the simulation have to change if it were to guarantee that Stephen always makes it 500 miles, no matter how many times he's pulled over? (Describe this change in words; don't code it.)

Turning it in

Send an email to data420submissions@gmail.com with subject line "DATA 420 numerical calculus practice turnin". Attached to this email should be your yourumwid_roadtrip.py file, your roadtrip_answers.md file, and all two (or three) of your plots.

Getting help

Come to office hours, or send me email with subject line "DATA 420 numerical calculus help!!"