0
0
MATLABdata~20 mins

Curve fitting (polyfit, fit) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Curve Fitting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of polyfit polynomial coefficients
What is the output of the following MATLAB code?
x = [1 2 3 4 5]; y = [2.2 4.8 7.1 8.9 11.3]; p = polyfit(x, y, 1); disp(round(p,1))
MATLAB
x = [1 2 3 4 5];
y = [2.2 4.8 7.1 8.9 11.3];
p = polyfit(x, y, 1);
disp(round(p,1))
A[2.3 0.7]
B[1.8 2.2]
C[2.2 1.8]
D[2.2 0.2]
Attempts:
2 left
💡 Hint
Recall polyfit returns coefficients starting with highest degree term.
Predict Output
intermediate
2:00remaining
Result of fit with custom model
What is the value of the coefficient 'a' after running this MATLAB code?
x = (1:5)'; y = [2.2 4.8 7.1 8.9 11.3]'; ft = fittype('a*x+b'); f = fit(x,y,ft); disp(round(f.a,1))
MATLAB
x = (1:5)';
y = [2.2 4.8 7.1 8.9 11.3]';
ft = fittype('a*x+b');
f = fit(x,y,ft);
disp(round(f.a,1))
A4.8
B1.7
C2.2
D0.7
Attempts:
2 left
💡 Hint
Coefficient 'a' is the slope in the linear model.
Predict Output
advanced
2:00remaining
Output of polynomial evaluation after polyfit
What is the output of this MATLAB code?
x = 1:4; y = [1 8 27 64]; p = polyfit(x,y,3); val = polyval(p, 2.5); disp(round(val,1))
MATLAB
x = 1:4;
y = [1 8 27 64];
p = polyfit(x,y,3);
val = polyval(p, 2.5);
disp(round(val,1))
A15.6
B14.5
C16.0
D15.0
Attempts:
2 left
💡 Hint
The data is cubic: y = x^3, so polyval should approximate 2.5^3.
Predict Output
advanced
2:00remaining
Error type from incorrect fit model syntax
What error does this MATLAB code produce?
x = (1:3)'; y = [2 4 6]'; ft = fittype('a*x^2 + b'); f = fit(x,y,ft);
MATLAB
x = (1:3)';
y = [2 4 6]';
ft = fittype('a*x^2 + b');
f = fit(x,y,ft);
AError: Undefined function or variable 'a'
BNo error, fit succeeds
CError: Invalid model expression
DError: Fit cannot converge
Attempts:
2 left
💡 Hint
Check if the model string syntax is valid for fittype.
🧠 Conceptual
expert
2:00remaining
Choosing polynomial degree with polyfit
Given noisy data points, which polynomial degree chosen with polyfit is most likely to cause overfitting?
ADegree 10 (very high)
BDegree 2 (quadratic)
CDegree 1 (linear)
DDegree 3 (cubic)
Attempts:
2 left
💡 Hint
Higher degree polynomials can fit noise, not just trend.