📖 Scenario: You are working as a data analyst. You have collected some measurements of temperature and ice cream sales. You want to find a simple curve that best matches the data points to understand the trend.
🎯 Goal: Build a MATLAB script that fits a straight line to the data points using polyfit and fit functions, then display the fitted line.
📋 What You'll Learn
Create vectors for temperature and sales data
Create a variable for the degree of the polynomial fit
Use polyfit to find the coefficients of the best fit line
Use fit function to fit a linear model
Plot the original data and the fitted line
Display the coefficients and the fit result
💡 Why This Matters
🌍 Real World
Curve fitting helps understand trends in data like sales, temperature, or scientific measurements.
💼 Career
Data analysts and engineers use curve fitting to model relationships and make predictions.
Progress0 / 4 steps
1
Create the data vectors
Create two vectors called temperature and sales with these exact values: temperature = [15, 18, 21, 24, 27, 30] and sales = [200, 220, 250, 270, 300, 320].
MATLAB
Hint
Use square brackets to create vectors and separate values with commas.
2
Set the polynomial degree
Create a variable called degree and set it to 1 to indicate a linear fit.
MATLAB
Hint
Use a simple assignment to create the variable degree.
3
Fit the data using polyfit and fit
Use polyfit with temperature, sales, and degree to get coefficients in a variable called coeffs. Then use fit with temperature', sales', and 'poly1' to get a fit object called fitresult.
MATLAB
Hint
Remember to transpose vectors for fit using '.
4
Display and plot the results
Use disp to show coeffs and fitresult. Then plot temperature vs sales as points and overlay the fitted line using plot with temperature and polyval(coeffs, temperature). Add a legend and labels.
MATLAB
Hint
Use disp to show variables and plot to draw points and lines. Use hold on and hold off to combine plots.
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
Step 1: Understand the purpose of polyfit
polyfit calculates polynomial coefficients that best fit the data points given by x and y.
Step 2: Differentiate from other functions
Unlike fit, polyfit returns 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 C
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
Step 1: Identify correct argument order
The syntax is polyfit(x, y, n) where n is 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 A
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
Step 1: Fit a line to data
Using polyfit with degree 1 fits a line y = mx + b to points.
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.
Final Answer:
y_fit = 12.1 -> Option A
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
Step 1: Check syntax and input shapes
Vectors x and y as column vectors are valid inputs for fit.
Step 2: Verify toolbox requirement
The fit function 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 B
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
Step 1: Choose correct fit function and degree
Using fit with 'poly3' fits a cubic polynomial to x and y.
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.
Final Answer:
f = fit(x, y, 'poly3'); y_pred = feval(f, 7); -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.