0
0
MATLABdata~10 mins

Curve fitting (polyfit, fit) in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fit a polynomial of degree 2 to data points.

MATLAB
p = polyfit(x, y, [1]);
Drag options to blanks, or click blank then click option'
A1
B2
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using degree 1 fits a straight line, not a curve.
Using degree 0 fits a constant, which is not a curve.
2fill in blank
medium

Complete the code to evaluate the polynomial fit at points x_new.

MATLAB
y_fit = polyval(p, [1]);
Drag options to blanks, or click blank then click option'
Ax_new
By_new
Cp
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using y-values instead of x-values as input to polyval.
Passing the coefficients vector again instead of points.
3fill in blank
hard

Fix the error in the code to create a fit object for a linear fit.

MATLAB
f = fit(x, y, [1]);
Drag options to blanks, or click blank then click option'
A'poly2'
B'linear'
C'exp1'
D'poly1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'linear' which is not a valid model name for fit.
Using 'poly2' which fits a quadratic, not linear.
4fill in blank
hard

Fill both blanks to create a quadratic fit and plot it.

MATLAB
f = fit(x, y, [1]);
plot(f, [2]);
Drag options to blanks, or click blank then click option'
A'poly2'
Bx
Cx_new
D'poly1'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'poly1' instead of 'poly2' for quadratic fit.
Plotting against original x instead of new x-values.
5fill in blank
hard

Fill all three blanks to create a cubic polynomial fit, evaluate it, and plot the results.

MATLAB
p = polyfit(x, y, [1]);
y_fit = polyval(p, [2]);
plot([3], y_fit, '-r');
Drag options to blanks, or click blank then click option'
A3
Bx_new
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong degree for cubic fit.
Plotting against original x instead of new x-values.