Lab 1: MATLAB Arrays, Scripts, and Plots

Goal

Create a reproducible MATLAB script that samples, plots, and checks a mathematical function.

Setup

Work in a folder called week01. Save every script before running it. A script should run correctly from a clean workspace, so begin with:

clear; clc; close all;

Part A: Run And Explain

  1. Open week01_arrays_plotting.m.
  2. Run the script.
  3. In comments at the bottom of the file, answer:
    • What does x represent?
    • What do y1 and y2 represent?
    • Why do y1 and y2 have the same length as x?

Part B: Modify The Domain

Change the domain from [0, 2*pi] to [-pi, pi].

Then answer:

  • Did the numerical values change?
  • Did the shape of the plotted functions change?
  • Why is this still the same mathematical function?

Part C: Add A Check

Add a plot of:

sin(x).^2 + cos(x).^2

Then check numerically that the identity is close to 1 for all sampled points:

identity_values = y1.^2 + y2.^2;
identity_error = max(abs(identity_values - 1));

Interpret the value of identity_error.

Part D: Sample Count Experiment

Run the script with:

  • 10 sample points;
  • 50 sample points;
  • 200 sample points;
  • 1000 sample points.

For each run, record whether the plot looks smooth and whether the identity check changes.

Part E: Measured Data

Open week01_temperature_matrix.m.

The matrix stores time, temperature, and humidity measurements. Modify the script to:

  1. Plot temperature against time.
  2. Plot humidity against time.
  3. Compute the mean temperature.
  4. Find the maximum humidity.
  5. Add labels and a legend.

Part F: Matrix Operations And Systems

Open week01_matrix_systems.m.

Run the script, then answer:

  1. What is the difference between B*C and B.*C?
  2. What does A\b compute?
  3. Why is norm(A*x - b) a useful check?
  4. What row or column is selected by each colon expression in the script?

Deliverable

Submit one MATLAB script and a short note containing:

  • the final plot;
  • the value of identity_error;
  • one sentence explaining why elementwise operations were needed;
  • one sentence explaining how you checked a linear-system result;
  • one sentence explaining how you know the script is reproducible.

Reflection

Answer in a short paragraph:

  • What did the array x represent?
  • Why did we use elementwise operations?
  • Why is A\b preferable to forming inv(A) when solving a system?
  • What validation check convinced you that the identity was computed correctly?
  • What mistake would be easiest to miss if you only looked at whether MATLAB produced a figure?

AI Use

You may ask an LLM for help with MATLAB plotting syntax. If you do, record:

  • the prompt;
  • the useful suggestion;
  • the check you performed before trusting it.