Day 03 - Getting Started with VS Code

\[v_i \approx \frac{x_{i+1} - x_i}{\Delta t}\]

Today we'll set up VS Code for scientific computing and practice numerical differentiation.

PHY 321 Classical Mechanics I - Spring 2026

bg right:40% width:500px height:auto

PHY 321 Classical Mechanics I - Spring 2026

Today’s Goals#

After this session, you should be able to:

  • Set up VS Code for Python scientific computing

  • Use Jupyter notebooks in VS Code

  • Perform numerical differentiation using finite difference methods

  • Compare numerical results with analytical solutions


Step 1: Download VS Code#

Go to: https://code.visualstudio.com/download

Choose your operating system:

  • macOS: Universal download (Intel + Apple Silicon)

  • Windows: User Installer

  • Linux: .deb, .rpm, or .tar.gz

The download is ~200 MB and takes just a minute or two.


Step 2: Install VS Code#

On macOS:

  1. Open the downloaded .zip file

  2. Drag Visual Studio Code.app to Applications

  3. Launch from Applications

On Windows:

  1. Double-click the .exe installer

  2. Follow the wizard (default settings are fine)

  3. VS Code launches automatically


Step 3: Install Required Extensions#

Click the Extensions icon (4 squares) on the left sidebar

  • Or press Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (Mac)

Install these two extensions:

  1. Jupyter by Microsoft

  2. Python by Microsoft

Click Reload when prompted.


Step 4: Verify Python#

Open VS Code’s Terminal: View → Terminal (or Ctrl+`)

Type:

python --version

or

python3 --version

You should see something like Python 3.10.0 or newer.

If not, install Python from: https://www.python.org/downloads/


Download Today’s Activity#

Open the notebook for today’s activity:

https://dannycaballero.info/phy321msu/resources/vscode-setup-numerics-activity.html

Or find it in the course website under Resources.

Download the .ipynb file and open it in VS Code.


Libraries We’ll Use#

import numpy as np          # Numerical computations
import matplotlib.pyplot as plt  # Plotting
import pandas as pd         # Data manipulation

Run the first code cell to load these libraries.

If you get an error, you may need to install them:

pip install numpy matplotlib pandas

The Physics: Numerical Derivatives#

In experiments, we measure positions at different times.

To get velocities or accelerations, we numerically differentiate our data.

\[v_i \approx \frac{x_{i+1} - x_i}{\Delta t}\]

This is the forward difference method.


Finite Difference Methods#

Three common methods:

  • Forward: \(f'(x_i) \approx \dfrac{f(x_{i+1}) - f(x_i)}{\Delta x}\)

  • Backward: \(f'(x_i) \approx \dfrac{f(x_i) - f(x_{i-1})}{\Delta x}\)

  • Central: \(f'(x_i) \approx \dfrac{f(x_{i+1}) - f(x_{i-1})}{2\Delta x}\)

Central difference is more accurate but uses both neighboring points.


The Synthetic Data#

We’ve generated position data for a ball tossed with air resistance:

\[\mathbf{F}_{\text{net}} = -m\mathbf{g} - C_D |\mathbf{v}|\mathbf{v}\]

Run the data generation cells in the notebook to create:

  • t - time array (seconds)

  • x - x-position array (meters)

  • y - y-position array (meters)


Your Task#

Using only finite difference methods:

  1. Compute and plot the numerically-derived velocities (\(v_x\), \(v_y\))

  2. Compute and plot the numerically-derived accelerations (\(a_x\), \(a_y\))

  3. Compare your numerical derivatives with the “true” values

  4. What do you notice about the accuracy?

Work through the notebook with your neighbors!


Hints#

To compute velocity from position:

dt = t[1] - t[0]  # time step
vx = (x[1:] - x[:-1]) / dt  # forward difference

Note: Your derivative arrays will be one element shorter than the original!

To plot against the right time values:

plt.plot(t[:-1], vx)  # use t[:-1] not t

Exporting Notebooks to PDF#

For homework submissions on Gradescope:

Option A: VS Code Extension

  • Install “Jupyter PDF Export” extension

  • Right-click notebook → Export as PDF

Option B: Command Line

jupyter nbconvert --to pdf your_notebook.ipynb

Option C: Print to PDF

  • Use Ctrl+P / Cmd+P → Print


Reminders#


Get Started!#

  1. Download the notebook from the course website

  2. Open it in VS Code

  3. Work through the cells

  4. Complete the analysis task with your neighbors

Ask questions as you go!