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:

  1. What polynomial does p represent?
  2. What is the degree?
  3. Why is the zero coefficient necessary?
  4. What MATLAB command evaluates the polynomial at x = 4?
  5. What MATLAB command evaluates it on x = -2:0.5:2?

Exercise 2: Evaluate And Plot

Write MATLAB code that:

  1. Creates x = linspace(-3, 3, 200).
  2. Represents the polynomial f(x) = x^3 - 4*x.
  3. Computes y with polyval.
  4. Plots x versus y.
  5. Adds axis labels and a title.

Exercise 3: Polynomial Tools

Write MATLAB code that:

  1. creates a polynomial with roots 1, 2, and 3 using poly;
  2. verifies the roots using roots;
  3. builds the same polynomial by multiplying factors with conv;
  4. computes the derivative with polyder;
  5. 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:

  1. Fit a degree 1 polynomial.
  2. Fit a degree 2 polynomial.
  3. Compute residuals for each fit.
  4. Compute RMSE for each fit.
  5. 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?