Unit 4 / 11

Experimental Data Analysis and Curve Fitting

Gains:

  • Ability to fit experimental data with curve_fit by selecting the model from physics and report the parameters with their uncertainties
  • Ability to honestly evaluate fit quality by examining residuals and avoid overfitting
  • Ability to check the physical plausibility of the parameters found and distinguish a 'good looking' fit from a 'physically correct' fit

Physics ultimately depends on experimentation. No matter how elegant a theory is, if it does not agree with measured data, it is invalid. Curve fitting lies at the heart of working with experimental data: fitting a model predicted by theory (a line, an exponential, an oscillation) to the noisy measurement points you have in the best way possible and finding the parameters of the model (slope, constant, frequency). In this unit, you'll learn how to use artificial intelligence (AI) in experiment data analysis, how to set up curve fitting correctly, and—most critically—how to tell the difference between a fit "looking good" and "being physically correct." The standard tool is scipy.optimize.curve_fit (SciPy function that fits a model function to the data).

The logic of curve fitting and the role of AI

Curve fitting does this: you choose a model function (e.g. y = a·x + b), and the algorithm finds the values ​​of a and b that fit the data with the least error. AI is fast at coding the correct model function, setting up the curve_fit call, and visualizing the result. But two critical decisions are yours: (1) Which model is physically correct? (2) Is the quality of the fit really good? The AI ​​could also fit a 10th-order polynomial to the data and draw a curve that looks almost perfect — but this would be physically meaningless overfitting.

physical event

expected model

Found parameter

constant speed motion

y = a·x + b

slope = speed

radioactive decay

N = N₀·e^(−λt)

λ = decay constant

spring release

x = A·cos(ωt + φ)

ω = angular frequency

Ohm's law

V = R·I

R = resistance (slope)

free fall

y = ½·g·t²

g = gravitational acceleration

Step by step: fitting an honest curve

1. Choose the model from physics, not from data. First determine which physical law the event must obey. Choosing a model by looking at the data and choosing "which one fits well" is trying to make the physics fit the data — dangerous.

2. Enter data and uncertainty. Along with the measurement points, also enter the measurement error (sigma parameter) of each point, if any. This is necessary for accurate calculation of fit and parameter uncertainty.

3. Read the parameters and their uncertainties. curve_fit returns both parameter values ​​and their uncertainties derived from the covariance matrix. Reporting a parameter without its uncertainty is incomplete: saying "g = 9.7 ± 0.3 m/s²" is much more informative than just saying "9.7".

4. Evaluate the fit with residuals. The residual is the deviation of each measurement point from the model. Random distribution of residues is a sign of good fit; a systematic curve (e.g. all in one place above the model) indicates that the model is wrong.

5. Check physical reasonableness. Is the g value found in the order of 9.8 or 50? Did a resistor go negative? Physically impossible parameters indicate a problem, even if the fit “looks good.”

Tip: Don't judge the quality of the fit by relying solely on eyesight. Always plot the residuals on a separate graph. In a good fit, the residuals disperse like noise around zero; With a bad fit, a visible pattern (wave, slope, U-shape) appears. This pattern is the most honest sign that your model is physically deficient.

three mini cases

Case 1 — The overfitting trap. One student fitted an AI-suggested 7th degree polynomial to 8 data points; the curve passed through all the points and looked "perfect". But between the two points the curve oscillated absurdly. The student turned to physics: it was a linear relationship. When he fitted a simple line, the parameters took on physical meaning; the higher order polynomial had just memorized the noise.

Case 2 — Uncertainty forgotten. A researcher found and reported the gravitational acceleration g = 9.73 with the code written by YZ. His advisor asked about the uncertainty. The researcher did not use curve_fit's covariance output; When calculated, the uncertainty turned out to be ±0.4, meaning the result was consistent with the standard value of 9.81. Without ambiguity, the result could be misinterpreted as "deviates from 9.81".

Case 3 — Residuals showed the error. A teacher fitted a straight line into a cooling experiment and it seemed reasonable. But when he plotted the scraps, a distinct curved pattern emerged. What actually happened was an exponential cooling. When he used the correct model (exponential), the residuals became random and the fit became physically meaningful.

Four copyable templates

1) Fitting with physical model:

Write a curve_fit code that fits the physical modely = [model function] to the following experimental data ([x and y values, in units]). Print the parameters and 1-sigma uncertainties derived from the covariance. Plot the data, the fitted curve, and the residuals on a SEPARATE graph. I made the model selection from physics; you just apply it.

2) Quality of fit assessment:

Add code that evaluates the quality of the fit below: plot the residuals, calculate a measure such as chi-square or R², and comment in writing if there is a systematic pattern in the residuals. Fit code: [here]

3) Model comparison:

Write code that fits and compares two different physical models to the same data: [model A] and [model B]. Show the parameters, uncertainties, and residual patterns of both. It's up to me to decide which one is PHYSICALLY more suitable; Just present the two side by side. Remember to avoid overfitting.

4) Uncertainty reporting:

Add code that prints each parameter of the following fit with the appropriate number of significant digits in the format "value ± uncertainty (unit)". Fit if a parameter takes a physically impossible value (e.g. negative mass). Code: [here]

Weak prompt / Strong prompt

Weak: "Find the curve that best fits these data."
Conclusion: AI can give a "perfect" but physically meaningless fit to a high-order polynomial; uncertainty and no more.
Strong: "Write a curve_fit code that fits the radioactive decay model N = N₀·e^(−λt) to this (t, N) data. Print λ and N₀ and their uncertainties. Plot the residuals in a separate graph and comment if there is a systematic pattern. Warn if λ is negative."
Result: Physical model, uncertain parameters, residual checking and plausibility checking.

Common mistakes

  • Selecting the model from the data. Choosing the "best fit" instead of the physical law is forcing physics to fit the data and leads to overfitting.
  • Not reporting uncertainty. A parameter value without uncertainty is scientifically incomplete; Whether two results are compatible or not can only be understood through uncertainty.
  • Not scratching the leftovers. Declaring compliance "good" by looking at the eye hides systematic deviations.
  • Overestimate the significant digit. Writing g = 9.73418 ± 0.4 is a false precision; Uncertainty determines how many digits are significant.
  • Bypassing physical reasonableness. Ignoring impossible consequences like negative resistance or 50 m/s² gravity.
Attention: Just because a curve fits the data well does not prove that the underlying physics is correct. With enough free parameters, a "good" curve can be fit to any data. Scientific integrity requires choosing the model from physics, reporting uncertainty, and examining the residuals honestly. A nice AI-generated graphic is no substitute for these controls.

In summary

In experiment data analysis, AI quickly establishes curve fitting code and visualization; but it is the physicist's responsibility to choose the physical model, accurately report uncertainty, and honestly evaluate the quality of fit. A good analysis takes the model from physics, reports the parameters with their uncertainties, examines the residuals, and tests the physical plausibility of the result. In the next unit, we will discuss this concept of uncertainty in more depth—measurement error and error propagation.

Application task

Fit a physical model to a small set of experimental data you have or have produced (for example, length-period measurements of a pendulum or current-voltage data of a resistor). Print the code with the 1st template to the AI, run it; Read the parameters with their uncertainty and plot the residuals. Check if there is a systematic pattern in the residuals, whether the parameters are physically reasonable. Write down in 5-6 sentences: was the fit really good, what did the uncertainty say?

checklist

  • [ ] I chose the model from physics, not from data.
  • [ ] I included measurement uncertainty, if any, in the fitting.
  • [ ] I reported the parameters with their uncertainty and appropriate significant digit.
  • [ ] I plotted the residuals on a separate graph and looked for systematic patterns.
  • [ ] I checked that the parameters are physically reasonable.
  • [ ] I avoided overfitting (unnecessarily high freedom).