1 of 18

Unit 3 - Oscillation

Lab 3B: Complex fitting

UCLA Physics Department

1

University of California, Los Angeles

Department of Physics and Astronomy

​

Physics 4AL

Fall 2026

2 of 18

Announcement: Project Proposal

  • The Project Proposal is due by Wes, Nov 4th at 11:59 pm, and you will submit this proposal as a group.
  • In your groups, you will create a 3-4 page project proposal to describe your experiment, the materials you’ll need, and a general timeline for your project
  • You can find more information on the Project Proposal on the course website under. You can view this information here.
  • You can also view the template for the timeline here.
  • Your project should be complex enough to use the 3 weeks you have.

2

3 of 18

Outline of Lab 3B

  • 3B In-Lab
    • Introduction to complex fitting with scipy curve_fit
    • Finding best fit position data
    • Finding best fit acceleration data

​

​

​

UCLA Physics Department

3

4 of 18

Complex Curve Fitting

UCLA Physics Department

4

5 of 18

Define Fit Function

5

  • The curve_fit function is a generalization of the polyfit function that we used for polynomials - curve_fit can find fit parameters for any function that you provide

​

​

​

​

​

  • Before we use curve_fit, we first have to define our fit function. Since our data is for a harmonic oscillator, we will fit our data to a sine function, as depicted

6 of 18

Fit Function Parameters

6

  • The first parameter of the function should be the x-axis data. Since we are plotting distance/acceleration vs. time, this is the time data

​

​

​

​

​

​

  • The other parameters are constants to be determined from the fit. The returned value is the fit to the y-axis data (distance or acceleration)

7 of 18

Guess Parameters

7

  • For complex fitting, we need to make guesses for the constant parameters for our fit function
  • Reasonable guesses for these parameters based on our data
  • Store guessed values into an array in the order they appear in our fit function

8 of 18

Curve Fit

8

  • The curve_fit function belongs to a new Python module - scipy. We can import this module to gain access to complex fitting:

​

  • We can then use the curve_fit function to find the optimal parameters for our fit function

​

  • We pass in four different items:
    • The function that we want to fit our data to
    • The x-axis data (time in this example)
    • The y-axis data (distance in this example)
    • Our guess fit parameters, using “p0 = ” followed by the array for our guess parameters

9 of 18

Curve Fit Outputs

9

  • After we give curve_fit the data required to complete the fit, we can store the output parameters and covariance matrix:

​

​

  • Here, optimal_params : the best possible fit parameters for our function, in the order they appear in the function

​

  • Param_covariance: the covariance matrix for the fit parameters for our function. -> Use this for error calculation

10 of 18

Obtaining the Fitted Function

10

  • Use the best-fit parameters + Your defined function

​

​

​

​

​

  • The asterisk (*) in *optimal_params
    • Extracts each of the elements in the optimal_params array and passes them into the function as if you typed them out separated by commas

11 of 18

Data and Complex Function Fit Result

  • We can see that the fitted function matches the data!
  • Make plot best fit parameters and not guess parameters
  • When fit is not good…

Adjust your guess parameters

​

11

12 of 18

Fitting Process and Notebook

12

  • The general process for curve fitting is as follows:
    • Clip your data so that you have 3-4 high-quality periods
    • Plot your data to make sure that your data is properly clipped and has little to no systematic noise
    • Use this plot to make a guess for the fit parameters
    • Plot your data with a fit using your guess parameters to ensure that your guess parameters fit your data well
    • Scipy curve_fit function -> get the fit parameters
    • Plot your data:
      • Use the fit parameters + your defined function
      • Ensure that the fitted function matches your data!
  • You can find a notebook to walk you through this process in the practice notebooks:
    • Physics_4AL_2023-24/Practice Notebooks/Simple_harmonic_motion_in_lab.ipynb

13 of 18

TA Checkpoint 1 - Best Fit of Position

  • We will be working with the ultrasound and accelerometer data that you collected in Lab 3A.
  • You already know the mass (m) and the spring constant (k).
  • Convert your position data to unit [m] instead of [cm].
  • Plot 3-4 oscillations of the position data and the best fit sine function in the same plot window.
  • Use the best fit angular frequency “omega” and the mass m to find the spring constant k.
  • Compare with the spring constant value from Lab 3A.

13

14 of 18

Numerical and Theoretical Differentiation

UCLA Physics Department

14

15 of 18

Numerical Differentiation

15

  • Numpy gradient:
    • Numerical derivatives
    • We will be using this function to take derivatives of position and compare our results to our fitted acceleration

​

​

​

​

  • Velocity = the derivative of distance with respect to time
  • Acceleration = the derivative of velocity with respect to time

16 of 18

Theoretical Differentiation

16

  • We assumed that our position as a function of time had the form:

​

​

  • If we differentiate this expression with respect to time, we can obtain the theoretical velocity and acceleration:

​

​

​

​

  • Using this, we can predict the result for acceleration vs. time based on our fit parameters for position vs. time

17 of 18

TA Checkpoint 2 - Acceleration Plots

17

  • Plot 1: Obtain acceleration data from position data (by numerical differentiation)

​

  • Plot 2: Calculate the theoretical acceleration using your fit parameters for distance vs. time (refer to slide 17)

​

  • Plot 3: Using your accelerometer x-axis output, convert this to acceleration in m/s2 and fit this to a sinusoid. Plot this acceleration (in m/s^2) vs. time with its best-fit

​

  • Compare your three resulting plots - plot them on the same graph

18 of 18

Post-Lab Requirements for lab 3B

  • Please complete the post lab assignment found in your JupyterHub Repo.
    • Physics_4AL/Assignments/Post-Labs/Post-Lab-Week-5.ipynb
  • Plot 3-4 oscillations of position vs. time with its best-fit.
  • Compare your resulting angular frequency to the expected angular frequency based on your spring constant from lab 3A
  • Plot 3-4 oscillations of acceleration (from accelerometer output) vs. time with its best-fit. Be sure to include a title, axis labels, and a legend
  • Plot 3-4 oscillations of acceleration best-fit (from accelerometer output), numerically differentiated position, and theoretically differentiated position. Be sure to include a title, axis labels, and a legend

18