Week 1: MATLAB Arrays, Scripts, and Plots

Today’s Question

How does a mathematical object become a computational object?

Week 1 Plan

  • Meeting 1: arrays, scripts, and first plots
  • Meeting 2: validation, measured data, and AI critique

Core Ideas

  • Vectors represent ordered values.
  • Matrices represent tabular or linear-algebra objects.
  • Scripts preserve a computation.
  • Plots reveal whether output makes sense.

Meeting 1 Question

What does an array mean?

Vector Pattern

x = linspace(0, 2*pi, 200);
y = sin(x);
plot(x, y)
xlabel("x")
ylabel("sin(x)")

Matrix Pattern

A = [1 2 3; 4 5 6];
size(A)
A(2, 3)
A(:, 2)

Colon And Transpose

row2 = A(2, :);
col3 = A(:, 3);
v = [1; 2; 3];
v'

The colon means “all rows” or “all columns” in this context.

Matrix Or Elementwise?

B = [1 2; 3 4];
C = [10 20; 30 40];

linear_algebra = B*C;
pointwise = B.*C;

Same arrays, different mathematical meanings.

Linear System Pattern

A = [2 1; 1 3];
b = [1; 2];

x = A\b;
residual = A*x - b;
norm(residual)

For solving A*x = b, prefer A\b over inv(A)*b.

Script Habit

A reproducible script should:

  • begin from a clean workspace;
  • define all needed variables;
  • run from top to bottom;
  • label its output;
  • include at least one check.

Meeting 1 Activity

Change:

x = linspace(0, 2*pi, 200);

to a different domain and sample count. Predict what changes before running the script.

Meeting 2 Question

How do we know a figure is trustworthy?

Validation Checklist

  • Are dimensions correct?
  • Are units and labels clear?
  • Is the sampling dense enough?
  • Can the script run from a clean workspace?
  • Does a numerical check support the visual output?

Identity Check

identity_values = sin(x).^2 + cos(x).^2;
identity_error = max(abs(identity_values - 1));

The plot is useful. The numerical check is evidence.

AI Critique

Ask an LLM for plotting code. Your job is to decide whether the code is runnable, labeled, and mathematically appropriate.

Plausible But Weak Code

x = 0:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(y1, y2)
title("Sine and cosine")

Critique Questions

  • Is x sampled well?
  • What is actually on the horizontal axis?
  • Are the axes labeled?
  • Would this be acceptable in a report?
  • What check would reveal the problem?

Exit Ticket

Code that runs is not enough because …