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
- Open week01_arrays_plotting.m.
- Run the script.
- In comments at the bottom of the file, answer:
- What does
xrepresent? - What do
y1andy2represent? - Why do
y1andy2have the same length asx?
- What does
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).^2Then 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:
- Plot temperature against time.
- Plot humidity against time.
- Compute the mean temperature.
- Find the maximum humidity.
- Add labels and a legend.
Part F: Matrix Operations And Systems
Open week01_matrix_systems.m.
Run the script, then answer:
- What is the difference between
B*CandB.*C? - What does
A\bcompute? - Why is
norm(A*x - b)a useful check? - 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
xrepresent? - Why did we use elementwise operations?
- Why is
A\bpreferable to forminginv(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.