Week 3 Exercises
These exercises practice polynomial representation, fitting, residuals, interpolation, and AI critique. Use AI only after you have written a first attempt yourself.
Exercise 1: Read A Polynomial Vector
Given:
p = [3 0 -2 5];Answer:
- What polynomial does
prepresent? - What is the degree?
- Why is the zero coefficient necessary?
- What MATLAB command evaluates the polynomial at
x = 4? - What MATLAB command evaluates it on
x = -2:0.5:2?
Exercise 2: Evaluate And Plot
Write MATLAB code that:
- Creates
x = linspace(-3, 3, 200). - Represents the polynomial
f(x) = x^3 - 4*x. - Computes
ywithpolyval. - Plots
xversusy. - Adds axis labels and a title.
Exercise 3: Polynomial Tools
Write MATLAB code that:
- creates a polynomial with roots
1,2, and3usingpoly; - verifies the roots using
roots; - builds the same polynomial by multiplying factors with
conv; - computes the derivative with
polyder; - evaluates the derivative at
x = 2.
Explain what information is stored in each coefficient vector.
Exercise 4: Fit And Check Residuals
For the data:
x = [0 1 2 3 4 5 6];
y = [1.1 2.2 2.9 4.1 5.0 5.8 7.2];Write code to:
- Fit a degree 1 polynomial.
- Fit a degree 2 polynomial.
- Compute residuals for each fit.
- Compute RMSE for each fit.
- Plot the two fitted curves with the data.
Which model would you report? Give a reason based on both the plot and the residuals.
Exercise 5: Interpolate
For:
time = [0 2 4 6 8];
temp = [18 21 24 23 20];Use interp1 to estimate the temperature at:
time = 3;time = 5;time = 7.
Compare "linear" and "pchip". Which estimate would you use if the measurements are trusted but sparse?
Before interpolating, check:
issorted(time)Then explain why asking for time = 10 would be a different kind of claim.
Exercise 6: Critique Generated Code
An LLM produced:
x = [0 1 2 3 4 5 6 7 8];
y = [2.0 2.7 3.5 5.0 6.8 8.0 9.1 9.8 10.1];
p = polyfit(x, y, length(x)-1);
prediction = polyval(p, 12);
disp(prediction)Find at least four issues or missing checks. Your answer should mention:
- degree choice;
- residuals;
- extrapolation;
- whether the word “prediction” is justified.
Exit Ticket
Write 3-5 sentences answering:
How do you decide whether to interpolate, fit a model, or reject the question?