Bird
Raised Fist0
MATLABdata~10 mins

Curve fitting (polyfit, fit) in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Curve fitting (polyfit, fit)
Start with data points x,y
↓
Choose fitting method: polyfit or fit
↓
Calculate coefficients or fit object
↓
Evaluate fitted curve at desired points
↓
Plot original data and fitted curve
↓
End
Start with data points, choose polyfit or fit, compute the fit, evaluate the curve, then plot results.
Execution Sample
MATLAB
x = 1:5;
y = [2.2 2.8 3.6 4.5 5.1];
p = polyfit(x,y,1);
y_fit = polyval(p,x);
Fits a straight line to points (x,y) using polyfit and evaluates fitted values.
Execution Table
StepActionInput/ConditionResult/Output
1Define xx = [1 2 3 4 5]x vector created
2Define yy = [2.2 2.8 3.6 4.5 5.1]y vector created
3Call polyfitpolyfit(x,y,1)p = [0.73 1.49] (coefficients)
4Call polyvalpolyval(p,x)y_fit = [2.22 2.95 3.68 4.41 5.14] (fitted y)
5Plot data and fitplot(x,y,'o',x,y_fit,'-')Graph shows points and fitted line
6EndAll steps doneCurve fitting complete
💡 All steps completed successfully; fitted line computed and plotted.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
xundefined[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
yundefinedundefined[2.2 2.8 3.6 4.5 5.1][2.2 2.8 3.6 4.5 5.1][2.2 2.8 3.6 4.5 5.1][2.2 2.8 3.6 4.5 5.1]
pundefinedundefinedundefined[0.73 1.49][0.73 1.49][0.73 1.49]
y_fitundefinedundefinedundefinedundefined[2.22 2.95 3.68 4.41 5.14][2.22 2.95 3.68 4.41 5.14]
Key Moments - 3 Insights
Why does polyfit return two numbers for a linear fit?
polyfit returns coefficients for the polynomial starting with highest degree: here p(1) is slope and p(2) is intercept, as shown in step 3 of execution_table.
Why are fitted y values slightly different from original y?
Because polyfit finds the best line that approximates data, not exact points. Step 4 shows fitted y values close but not identical to original y.
What does the number 1 mean in polyfit(x,y,1)?
It means fitting a polynomial of degree 1, which is a straight line. This is the key input to polyfit in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the first number in p represent?
ASlope of the fitted line
BIntercept of the fitted line
CNumber of data points
DDegree of polynomial
💡 Hint
Check step 3 in execution_table where p = [0.73 1.49]; first number is slope.
At which step do we get the fitted y values for the original x points?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at step 4 in execution_table where y_fit is calculated.
If we change polyfit(x,y,1) to polyfit(x,y,2), what changes in the execution_table?
AStep 4 y_fit will be same as before
BStep 3 coefficients p will have 3 values
CStep 2 y vector changes
DStep 5 plot will not show data points
💡 Hint
Degree 2 polynomial means 3 coefficients; check step 3 for p.
Concept Snapshot
polyfit(x,y,n) fits polynomial degree n to data x,y
Returns coefficients p starting with highest degree
polyval(p,x) evaluates polynomial at x
fit function offers more options and fit types
Plot data and fit to visualize curve fitting
Full Transcript
This example shows how to fit a straight line to data points using MATLAB's polyfit and polyval functions. First, vectors x and y hold the data points. Then polyfit(x,y,1) computes the best line coefficients, slope and intercept. Next, polyval evaluates the fitted line at each x. Finally, plotting shows original points and the fitted line. The execution table traces each step, showing variable values and results. Key moments clarify why polyfit returns two numbers, what the degree means, and why fitted values differ slightly from original data. The visual quiz tests understanding of coefficients, evaluation step, and effect of changing polynomial degree. The snapshot summarizes syntax and behavior for quick reference.

Practice

(1/5)
1. What does the MATLAB function polyfit(x, y, n) do?
easy
A. It returns a fit object that can be used to predict new values.
B. It plots the data points x and y on a graph.
C. It finds the coefficients of a polynomial of degree n that fits the data points x and y.
D. It calculates the correlation coefficient between x and y.

Solution

  1. Step 1: Understand the purpose of polyfit

    polyfit calculates polynomial coefficients that best fit the data points given by x and y.
  2. Step 2: Differentiate from other functions

    Unlike fit, polyfit returns just coefficients, not a fit object or plot.
  3. Final Answer:

    It finds the coefficients of a polynomial of degree n that fits the data points x and y. -> Option C
  4. Quick Check:

    polyfit = polynomial coefficients [OK]
Hint: Remember: polyfit returns coefficients, fit returns a fit object [OK]
Common Mistakes:
  • Confusing polyfit with fit function
  • Thinking polyfit plots data
  • Assuming polyfit returns a fit object
2. Which of the following is the correct syntax to fit a quadratic polynomial to data vectors x and y using polyfit?
easy
A. p = polyfit(x, y, 2);
B. p = polyfit(y, x, 2);
C. p = polyfit(x, y, 3);
D. p = polyfit(x, 2, y);

Solution

  1. Step 1: Identify correct argument order

    The syntax is polyfit(x, y, n) where n is the polynomial degree.
  2. Step 2: Confirm degree for quadratic

    Quadratic means degree 2, so the third argument must be 2.
  3. Final Answer:

    p = polyfit(x, y, 2); -> Option A
  4. Quick Check:

    polyfit(x, y, 2) fits quadratic [OK]
Hint: Remember order: x, y, degree in polyfit [OK]
Common Mistakes:
  • Swapping x and y arguments
  • Using wrong polynomial degree
  • Incorrect argument order
3. What is the output of the following MATLAB code?
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);
medium
A. y_fit = 12.1
B. y_fit = 12.3
C. y_fit = 13.0
D. y_fit = 10.2

Solution

  1. Step 1: Fit a line to data

    Using polyfit with degree 1 fits a line y = mx + b to points.
  2. Step 2: Calculate predicted value at x=6

    Using polyval, we evaluate the fitted line at x=6. The slope and intercept from polyfit give approximately y_fit = 12.1.
  3. Final Answer:

    y_fit = 12.1 -> Option A
  4. Quick Check:

    polyval(polyfit(x,y,1),6) ≈ 12.1 [OK]
Hint: polyval evaluates polynomial at given x [OK]
Common Mistakes:
  • Confusing polyfit output with predicted value
  • Using wrong degree in polyfit
  • Calculating y_fit without polyval
4. Identify the error in this MATLAB code snippet for fitting data with fit:
x = (1:5)';
y = [2 4 6 8 10]';
f = fit(x, y, 'poly2');
medium
A. The fit type string should be 'poly1' for linear fit, not 'poly2'.
B. The fit function requires the Curve Fitting Toolbox to be installed.
C. The input vectors x and y must be row vectors, not column vectors.
D. The syntax is correct; there is no error.

Solution

  1. Step 1: Check syntax and input shapes

    Vectors x and y as column vectors are valid inputs for fit.
  2. Step 2: Verify toolbox requirement

    The fit function requires the Curve Fitting Toolbox; without it, the code will error.
  3. Final Answer:

    The fit function requires the Curve Fitting Toolbox to be installed. -> Option B
  4. Quick Check:

    fit needs toolbox installed [OK]
Hint: fit needs Curve Fitting Toolbox installed [OK]
Common Mistakes:
  • Assuming fit works without toolbox
  • Confusing poly1 and poly2 as errors
  • Thinking vector orientation causes error
5. You have noisy data vectors 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?
hard
A. p = polyfit(x, y, 3); y_pred = p(7);
B. f = fit(x, y, 'poly2'); y_pred = feval(f, 7);
C. p = polyfit(y, x, 3); y_pred = polyval(p, 7);
D. f = fit(x, y, 'poly3'); y_pred = feval(f, 7);

Solution

  1. Step 1: Choose correct fit function and degree

    Using fit with 'poly3' fits a cubic polynomial to x and y.
  2. Step 2: Predict value at x=7 using fit object

    Use feval(f, 7) or f(7) to get predicted y value from the fit object.
  3. Final Answer:

    f = fit(x, y, 'poly3'); y_pred = feval(f, 7); -> Option D
  4. Quick Check:

    fit with 'poly3' and feval predicts y at 7 [OK]
Hint: Use fit with 'poly3' and feval to predict [OK]
Common Mistakes:
  • Swapping x and y in polyfit
  • Using wrong polynomial degree
  • Using poly2 instead of poly3 for cubic fit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard