Curve fitting (polyfit, fit) in MATLAB - Time & Space Complexity
When we use curve fitting functions like polyfit or fit in MATLAB, it's helpful to know how the time to get the result changes as we use more data points.
We want to understand how the work grows when the input data size increases.
Analyze the time complexity of the following code snippet.
x = linspace(0,10,n); % n data points
y = sin(x) + randn(1,n)*0.1; % noisy data
p = polyfit(x, y, 3); % fit a cubic polynomial
This code fits a cubic polynomial to noisy data points using polyfit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating sums and products of the input data points to build matrices for solving polynomial coefficients.
- How many times: These operations happen once over all
ndata points, so roughlyntimes.
As the number of data points n grows, the time to compute sums and products grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 sums and products |
| 100 | About 100 sums and products |
| 1000 | About 1000 sums and products |
Pattern observation: Doubling the data points roughly doubles the work.
Time Complexity: O(n)
This means the time to fit the curve grows linearly with the number of data points.
[X] Wrong: "Fitting a polynomial always takes the same time no matter how many points I have."
[OK] Correct: The fitting process needs to look at every data point to calculate sums and build the system, so more points mean more work.
Understanding how curve fitting time grows helps you explain performance when working with large datasets, showing you know how algorithms scale in real tasks.
"What if we increase the polynomial degree instead of the number of data points? How would the time complexity change?"
Practice
polyfit(x, y, n) do?Solution
Step 1: Understand the purpose of
polyfitpolyfitcalculates polynomial coefficients that best fit the data points given byxandy.Step 2: Differentiate from other functions
Unlikefit,polyfitreturns just coefficients, not a fit object or plot.Final Answer:
It finds the coefficients of a polynomial of degree n that fits the data points x and y. -> Option CQuick Check:
polyfit= polynomial coefficients [OK]
- Confusing polyfit with fit function
- Thinking polyfit plots data
- Assuming polyfit returns a fit object
x and y using polyfit?Solution
Step 1: Identify correct argument order
The syntax ispolyfit(x, y, n)wherenis the polynomial degree.Step 2: Confirm degree for quadratic
Quadratic means degree 2, so the third argument must be 2.Final Answer:
p = polyfit(x, y, 2); -> Option AQuick Check:
polyfit(x, y, 2) fits quadratic [OK]
- Swapping x and y arguments
- Using wrong polynomial degree
- Incorrect argument order
x = [1 2 3 4 5]; y = [2.2 4.1 6.0 8.1 10.2]; p = polyfit(x, y, 1); y_fit = polyval(p, 6);
Solution
Step 1: Fit a line to data
Usingpolyfitwith degree 1 fits a line y = mx + b to points.Step 2: Calculate predicted value at x=6
Usingpolyval, we evaluate the fitted line at x=6. The slope and intercept from polyfit give approximately y_fit = 12.1.Final Answer:
y_fit = 12.1 -> Option AQuick Check:
polyval(polyfit(x,y,1),6) ≈ 12.1 [OK]
- Confusing polyfit output with predicted value
- Using wrong degree in polyfit
- Calculating y_fit without polyval
fit:x = (1:5)'; y = [2 4 6 8 10]'; f = fit(x, y, 'poly2');
Solution
Step 1: Check syntax and input shapes
Vectorsxandyas column vectors are valid inputs forfit.Step 2: Verify toolbox requirement
Thefitfunction requires the Curve Fitting Toolbox; without it, the code will error.Final Answer:
The fit function requires the Curve Fitting Toolbox to be installed. -> Option BQuick Check:
fit needs toolbox installed [OK]
- Assuming fit works without toolbox
- Confusing poly1 and poly2 as errors
- Thinking vector orientation causes error
x and y. You want to fit a cubic polynomial and then predict the value at x = 7. Which sequence of MATLAB commands correctly does this?Solution
Step 1: Choose correct fit function and degree
Usingfitwith'poly3'fits a cubic polynomial toxandy.Step 2: Predict value at x=7 using fit object
Usefeval(f, 7)orf(7)to get predicted y value from the fit object.Final Answer:
f = fit(x, y, 'poly3'); y_pred = feval(f, 7); -> Option DQuick Check:
fit with 'poly3' and feval predicts y at 7 [OK]
- Swapping x and y in polyfit
- Using wrong polynomial degree
- Using poly2 instead of poly3 for cubic fit
Ready to prove it?
Pass the test and this pattern moves unknown
