Week 1 Exercises

These exercises are short enough for practice but important enough to own without AI.

Exercise 1: Predict Dimensions

For each MATLAB command, predict the size before running it.

a = [1 2 3 4];
b = [1; 2; 3; 4];
C = [1 2 3; 4 5 6];
d = linspace(0, 1, 11);

Then run:

size(a)
size(b)
size(C)
size(d)

Explain any prediction that was wrong.

Exercise 2: Indexing

Let:

A = [2 4 6; 1 3 5; 10 20 30];

Find MATLAB commands for:

  1. the element in row 2, column 3;
  2. the whole second row;
  3. the whole third column;
  4. the sum of each column;
  5. the largest value in the matrix.

Exercise 3: Elementwise Operations

Create:

x = linspace(-2, 2, 100);

Then compute and plot:

y = x.^2 - 1;

Explain why .^ is needed instead of ^.

Exercise 4: Matrix Operations

Let:

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

Compute:

  1. B*C;
  2. B.*C;
  3. the transpose of B;
  4. the second column of C;
  5. eye(2)*B.

In one sentence, explain why the first two answers are different.

Exercise 5: A Small Linear System

Solve:

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

Use A\b, then compute:

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

Explain what the residual tells you.

Exercise 6: Plot Quality

Create a plot of:

f(x) = exp(-x) * sin(4*x)

on [0, 4].

Your plot must include:

  • at least 300 sample points;
  • labeled axes;
  • a title;
  • a grid;
  • a line width greater than the default.

Exercise 7: Validation

Use MATLAB to check:

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

on [0, 2*pi].

Report:

  • the maximum absolute error from 1;
  • whether the error is exactly zero;
  • why numerical error may appear even for a true identity.

Exit Ticket

Write 3-5 sentences answering:

What is the difference between a MATLAB script that produces a figure and a computational result that you trust?