Week 1: MATLAB Arrays, Scripts, and 2D Plotting

Official Topic

MATLAB arrays, script files, and 2D plotting.

Problem Focus

How does a mathematical object become a computational object?

Students use vectors and matrices to represent data, functions, and sampled values, then use scripts and plots to make the computation reproducible.

Learning Goals

By the end of the week, students should be able to:

  • create and inspect row vectors, column vectors, and matrices;
  • explain what each array represents in a mathematical or data context;
  • use elementwise operations correctly;
  • distinguish matrix multiplication from elementwise multiplication;
  • solve a small linear system with A\b;
  • use :, transpose, zeros, ones, and eye when building or inspecting arrays;
  • write a script that runs from a clean workspace;
  • make a labeled 2D plot;
  • add simple numerical checks before trusting a plot;
  • critique AI-generated MATLAB code for correctness and clarity.

Meeting 1: Arrays As Representations

Question: What does an array mean?

Suggested 75-minute rhythm:

Time Activity
0-10 min Course framing: computation is not just syntax
10-25 min Live coding: vectors, matrices, indexing, and dimensions
25-37 min Matrix operations: *, .*, transpose, and A\b
37-50 min Script habit: move command-window work into a reproducible file
50-62 min Plot sampled functions with labels and legends
62-72 min Student modification: change domain, sample count, and plotted function
72-75 min Exit ticket: what did x, y, and the plot represent?

Core MATLAB Patterns

x = linspace(0, 2*pi, 200);
y = sin(x);
plot(x, y)
A = [1 2 3; 4 5 6];
size(A)
A(2, 3)
A(:, 2)
A = [2 1; 1 3];
b = [1; 2];
x = A\b;
residual = A*x - b;

Meeting 2: Plots, Checks, And AI Critique

Question: How do we know that code which runs is actually answering the question?

Suggested 75-minute rhythm:

Time Activity
0-8 min Warm-up: predict dimensions and plot shape
8-25 min Review common plotting mistakes
25-45 min AI critique activity: inspect a plausible but weak script
45-65 min Lab work: trigonometric identity and measurement data
65-72 min Pair discussion: what checks caught mistakes?
72-75 min Exit ticket: one validation check you now trust

In-Class Checks

  • Create row vectors, column vectors, and matrices.
  • Use indexing to inspect and modify data.
  • Use : to select rows or columns.
  • Decide when A*B and A.*B mean different things.
  • Solve A*x = b with A\b and check the residual.
  • Plot sampled functions and measured data.
  • Distinguish a command-window experiment from a reproducible script.
  • Use size, length, max, and simple identity checks to validate output.

AI-Aware Task

Ask an LLM for MATLAB code that plots sin(x) and cos(x) on the same figure. Then check:

  • Does the code create an appropriate domain?
  • Are labels and legend present?
  • Is the sampling dense enough?
  • Does the code run in a fresh MATLAB session?
  • Does the plot show both functions against x, or does it accidentally plot one function against the other?

Survival Checklist

These are the Week 1 MATLAB habits you should be able to recognize, test, and repair without relying on AI.

Commands To Own

Command What you should know
clear Removes variables from the workspace so old values do not hide mistakes.
clc Clears the command window.
close all Closes open figures before rerunning a script.
size(A) Reports the number of rows and columns.
length(x) Reports the length of a vector.
A(:,j), A(i,:) Selects a column or row using the colon operator.
A' Transposes a vector or matrix.
zeros, ones, eye Creates common arrays with known structure.
linspace(a,b,n) Creates n evenly spaced values from a to b.
A*B Matrix multiplication when dimensions are compatible.
A.*B Elementwise multiplication for arrays of matching size.
A\b Solves the linear system A*x = b without forming inv(A).
plot(x,y) Plots values of y against values of x.
xlabel, ylabel, title, legend Make a figure interpretable.

Mistakes To Catch

  • Using ^ when the computation needs .^.
  • Using * when the computation needs .*, or the reverse.
  • Solving A*x = b with inv(A)*b instead of A\b.
  • Plotting plot(y1, y2) when the intended plot is plot(x, y1, x, y2).
  • Using too few sample points for a rapidly changing function.
  • Forgetting to label axes or include units.
  • Trusting a script that only works because old variables are still in the workspace.
  • Looking only at a figure and never checking dimensions or numerical values.

Checks Before Trusting Output

  • Run the script from a clean workspace.
  • Check size or length for important arrays.
  • For a linear system, check norm(A*x - b).
  • Use a known value, identity, or simple case.
  • Ask whether the horizontal and vertical axes mean what the title claims.
  • Explain in one sentence what each array represents.

Exercises

Complete the Week 1 exercises after the lab.

Materials